Important: Please read the Qt Code of Conduct -

Creating a window that's tied to parent QMainWindow, has a separate taskbar button and minimize button

  • I need to create a window that accepts a parent QMainWindow (so that it closes automatically when main window is closed), has a separate task bar button, minimize button, and is displayed with show(), not exec(). Should I use QDialog or QWidget, and what window flags should I set? I'm mostly talking about Windows, but need this window to look OK and have minimize button on Mac OS as well.
    Or should I maybe use a second QMainWindow for this?

  • Moderators

    just use a simple QWidget and set the parent widget to your QMainWindow.
    And just set the window flags to Qt::Window

  • There's no taskbar button in this case. I only get a button if I set parent to null, but I don't want to do that either.

  • Moderators

    ok and why not just holding a pointer to the window and deleting it in your main window destructor?

  • That's exactly what I'm trying to avoid. There can be indefinite number of these windows created during application's life, holding a pointer means not deleting the window when it's no longer needed, which in turn effectively means memory leak.

  • Moderators

    ok then the other way around:
    When you would set your mainwindow as the parent of the new subwindow you connect the mainwindow's destroyed() signal to the subwindow's deleteLater() slot instead.

  • Thanks, that didn't cross my mind. However, if I first close mainwindow and then the helper window, mainwindow's destroyed() isn't called until helper window is closed.
    I already have closeEvent() reimplemented in my mainwindow, so it will be easy to add a special signal that will be emitted in closeEvent().

  • Moderators


  • You could get the 'close()' event on the dialog and trigger a slot in the main window, so when an instance of the dialog closes, you could get the 'sender()' object in your slot and delete it.

    Otherwise, create a list of dialog's as you create them...and delete them when you close the application. You might not want to keep them all in memory though.

    There are many ways to accomplish what you are trying to do, you just have to choose which way is convenient in your code.

Log in to reply