Reuse QPlainTextEdit window after it's closed
-
I'm using
QPlainTextEdit
as a view for my log. I want it to show in a separate window, so it's created without a parent and I callshow()
when I want it to appear - this part is fine.I also don't want to lose the log when the log window is closed (user clicks on 'X'), so the widget is not deleted on close, but instead I reuse it by calling
show()
again when I want it to appear next. Unfortunately, when the window is shown the second time, as soon new content is appended (via keyboard or through code) and theQPlainTextEdit
tries to scroll, the whole application crashes.It seems that the problem is in closing because everything works fine if I hide the widget instead of closing it. The problem is that the documentation states that when closed, the widget is only hidden if
Qt::WA_DeleteOnClose
attibute is not set (it isn't set in my case) and not deleted (which is true, because the destructor is not called). However, from my testing, it seems thatclose
is doing more than just callinghide
.Am I doing something wrong? Is there a proper way to reuse a widget as a top-level window? Is it supported at all? I haven't found anything in the docs that implies it isn't.
Here's a small example to reproduce the problem:
#include <QApplication> #include <QPushButton> #include <QPlainTextEdit> int main(int argc, char* argv[]) { QApplication app(argc, argv); QPushButton button; QPlainTextEdit edit; button.setText("SHOW"); QObject::connect(&button, &QPushButton::clicked, &edit, [&edit](bool checked) { edit.show(); }); button.show(); app.exec(); return 0; }
Steps to reproduce the issue:
- Click the "SHOW" button.
- Close the newly opened window containing
QPlainTextEdit
. - Click the "SHOW" button again.
- Press "Enter" inside the new window several times until the
QPlainTextEdit
needs to scroll. The application should crash.
I'm using Qt 5.12.4
-
Known bug, please update to 5.12.5