I have a QPlainTextEdit in a modal dialog window (subclass of QDialog). Whenever I minimize the dialog and bring it back up, all the text in the QTextArea has disappeared.
How can I preserve the text in my QPlainTextEdit widget?
I've tried saving the text in the textedit widget like this:
def __init__(self):
self.text_area = self.QPlainTextEdit()
self.previous_text = ''
def hideEvent(self, event):
self.previous_text = self.text_area.toPlainText()
def showEvent(self, event):
self.text_area.setPlainText(self.previous_text)
But this approach didn't yield any appreciable results.
EDIT: I've noticed that this only happens when I can actually hide the modal dialog ... which seems to only be possible when I use Xmonad as my window manager ... whenever I try this in GNOME or on Windows, this behavior cannot be reproduced because those window managers actually prevent the modal dialog from being minimized.
EDIT:
Following @Avaris's comment below, I tried enabling minimization for the modal QDialog
like so:
def __init__(self):
...
self.setWindowFlags(QtCore.Qt.WindowMinimizeButtonHint)
With this flag set, the vanishing text behavior can be replicated on Windows, GNOME and XMonad.
Here is a visual example from my Windows machine before I minimize the window:
And here's what it looks like after I bring up the window again:
EDIT:
Text is written to the modal dialog's QPlainTextEdit
like so:
def write(self, text):
self.text_area.insertPlainText(QtCore.QString(text))
The text itself is retrieved as a python string from a system of threads whose standard out has been captured and redirected. By the time is gets to the above write()
function, the text itself is simply a python string.
EDIT:
When I modify the write
method mentioned above like so (following @ekhumoro's suggestion):
def write(self, text):
self.text_area.insertPlainText(text)
The text is still missing after hiding and showing the dialog.