Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Can I have a window that uses Qt::WA_DeleteOnClose and has a taskbar button at the same time?



  • The question might sound weird, but bear with me.

    1. In order for Qt::WA_DeleteOnClose to work the window (be it QWidget or QMainWindow; happens to be a QMainWindow in my case) must have a non-null parent.
    2. In order for a window to have a taskbar button, at least on Windows, it must have a nullptr parent.

    Quite a dilemma. Looks like I must implement deletion on close manually, but I would prefer a standard solution.


  • Moderators

    @Violet-Giraffe said:

    In order for Qt::WA_DeleteOnClose to work the window must have a non-null parent.

    Where did you get that idea from? That's not true at all.



  • From my experiments. I'm certain of it, triple-checked.


  • Moderators

    I'm afraid you're gonna have to try it the fourth time then ;)
    I suggest the following example for that:

    #include <QApplication>
    #include <QWidget>
    #include <QDebug>
    
    struct Widget : public QWidget {
        ~Widget() { qDebug() << "deleted!"; }
    };
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        auto w = new Widget();
        w->setAttribute(Qt::WA_DeleteOnClose);
        w->show();
    
        return a.exec();
    }
    

    Widget obviously has no parent and the output is "deleted!", as expected.

    So you're doing something wrong. What problem are you trying to solve?



  • Yep, this works. But my window still doesn't. Weird.


  • Moderators

    If you provide us with that window's code we might be able to pinpoint the problem.


Log in to reply