Show/hide QDockWidget?

15,524

Instead of creating a new action, simply get the action from the QDockWidget itself and use that. It'll take care of state for you:

http://qt-project.org/doc/qt-4.8/qdockwidget.html#toggleViewAction

QAction * QDockWidget::toggleViewAction () const

"Returns a checkable action that can be used to show or close this dock widget.

The action's text is set to the dock widget's window title. "

Share:
15,524

Related videos on Youtube

mpen
Author by

mpen

Updated on June 04, 2022

Comments

  • mpen
    mpen almost 2 years

    I have a dock widget, now I want to add a "Window" menu to show/hide the widget. Easy enough to do with

    showPropWinAct = new QAction(tr("&Properties"), this);
    showPropWinAct->setStatusTip(tr("Show properties window"));
    showPropWinAct->setCheckable(true);
    connect(showPropWinAct, SIGNAL(toggled(bool)), propertiesWindow, SLOT(setVisible(bool)));
    

    The problem is when the user clicks the [x] on the widget, the showPropWinAct doesn't get toggled. How can I listen for this event, and toggle the action properly, without firing off a 2nd setVisible signal (one from the close event presumably, and one from the connect above)?

  • mpen
    mpen over 14 years
    Hah! Brilliant. Knew there had to be a better way to do this. Thank you so much! :)
  • Timmmm
    Timmmm over 11 years
    Any way to do this in the designer? Currently I just use void MainWindow::on_dockWindow_visibilityChanged(bool visible) { ui->actionDockWindowToggle->setChecked(visible); } and then setShown() in the action on_toggled() slot. It works well enough and is only two lines of code, but if there's a way to use toggleViewAction() in the designer that'd be nice!
  • S818
    S818 almost 4 years
    @Timmmm Here it says how to do it in the designer. It doesn't use the QDockWidget's own toggleViewAction, though.
  • Fernando Ortega
    Fernando Ortega about 3 years
    I was struggling with this today, this is such a simple solution, thanks!