Important: Please read the Qt Code of Conduct -

closeEvent with Multiple QDockWidgets

  • Hi! My first post here, but not my first time lurking. I've looked around for a similar thread, but haven't found anything pertaining to this issue:

    I have two DockWidgets in my AppWindow. One is a Log window, the other has some buttons and a slider. The issue is that when one of them is closed, I need to know which one was closed so I can enable re-opening it. In my code, the same closeEvent function gets called when either one is closed.
    void QDockWidget::closeEvent(QCloseEvent* event)

    Inside, closeEvent, how can I tell which DockWidget was closed?


    [Moved to General and Desktop ~kshegunov]

  • @DanM
    void QDockWidget::closeEvent(QCloseEvent* event)

    This is called on the particular QDockWidget which is being closed (only), so you don't need to "tell which DockWidget was closed" (it's this)? You could override it if you want to store some permanent information on closing the dock widget.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    No need to go for such complex things.

    Create a View menu and add the QDockWidget::toggleViewAction of your dock widgets.

    That way everything is done for you without hassle.

  • I appreciate the help so far, and please forgive my ignorance since I've been working with Qt for about a month now, updating an existing app. There's currently a toolbar icon to open up the second dockwidget. The first one opens when the app starts up. A View menu is overkill for just one dockwidget.

    It's not possible for the closeEvent to know which dockwidget was closed?

  • Moderators

    @DanM said in closeEvent with Multiple QDockWidgets:

    It's not possible for the closeEvent to know which dockwidget was closed?

    Not really, but there are (dirty) hacks. You can install an event filter to the docks to intercept from your main window/controller the close event before it's processed

  • Lifetime Qt Champion

    Well... Why not add the action to the toolbar ? That will make both dock widgets available from there. You can then toggle them as you wish.

Log in to reply