Important: Please read the Qt Code of Conduct -

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 call show() 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 the QPlainTextEdit 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 that close is doing more than just calling hide.

    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;
        QObject::connect(&button, &QPushButton::clicked, &edit, [&edit](bool checked) {
        return 0;

    Steps to reproduce the issue:

    1. Click the "SHOW" button.
    2. Close the newly opened window containing QPlainTextEdit.
    3. Click the "SHOW" button again.
    4. 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

  • Lifetime Qt Champion

    Known bug, please update to 5.12.5

Log in to reply