Show/hide QDockWidget?
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. "
Related videos on Youtube
Comments
-
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 theconnect
above)? -
mpen over 14 yearsHah! Brilliant. Knew there had to be a better way to do this. Thank you so much! :)
-
Timmmm over 11 yearsAny way to do this in the designer? Currently I just use
void MainWindow::on_dockWindow_visibilityChanged(bool visible) { ui->actionDockWindowToggle->setChecked(visible); }
and thensetShown()
in the actionon_toggled()
slot. It works well enough and is only two lines of code, but if there's a way to usetoggleViewAction()
in the designer that'd be nice! -
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 about 3 yearsI was struggling with this today, this is such a simple solution, thanks!