Solved QDockWidget and QToolButton interaction
-
Hi!
This is a tricky question (at least for me):
Currently I am working on a QMainWindow class which has a QDockWidget object and a QToolButton within. Both objects are connected, I mean If I click on the QToolButton, it gets checked/unchecked then the QDockWidget shows up/hide (that part works perfectly).Now, my QDockWidget has a little close button in the top-right corner (as usual), if I click on it the QDockWidget hides normally but I need to "uncheck" the QToolButton and here is where my problem starts. I was trying something like this:
void MyDockWidget::closeEvent(QCloseEvent *event) { myToolButton->setChecked(false); QDockWidget::closeEvent(event); }
And it didn't work, neither using
QToolButton::click()
and other dirty experiments I tried, so I decided to come here to look for the best approach to handle this kind of interactions.I appreciate any recommendation about my issue. Thanks! :)
-
Hi,
You should use the toggleViewAction to do that
-
@SGaist First at all, thanks for the hint! :)
I started to look for any code example about how to use the toggleViewAction method and I found this article:
http://ariya.ofilabs.com/2007/04/custom-toggle-action-for-qdockwidget.htmlUnfortunately, the examples I tried from there didn't work for me. My question is: your suggestion was the same that is described in that article?
Thanks!
-
I was rather thinking about using the action to create the button from the QToolBar but I just saw that you have the button inside the QDockWidget ?
-
@SGaist I created a reference of the button inside the QDockWidget class just to have access to the object, but I have access to it from the QMainWindow class too. Is there either any implication or relevance about it? (Just learning)
-
Yes: tight coupling. That's bad. It's not your QDockWidget's role to know that there's a button somewhere that tries to change its visibility.
Implication example: you change your QToolButton for a QCheckBox, In how many places do you have to propagate that modification ?
-
@SGaist Thank you for the comments, this is the way to learn better practices and you are right. The first thing I am going to do is to refactor my code to make something more clean and class-independent and then I get back to the problem :)
-
@SGaist Offtopic: Is there a way to disable or hide the maximize/close buttons of the QDockWidget? I was trying to use setWindowFlags() but I couldn't find the right combination of parameters. Thanks.
-
@xtingray said:
@SGaist Offtopic: Is there a way to disable or hide the maximize/close buttons of the QDockWidget? I was trying to use setWindowFlags() but I couldn't find the right combination of parameters. Thanks.
Check
void QDockWidget::setFeatures ( DockWidgetFeatures features )
This way you would be able to get rid of close button.as for visibility change Main window already has actions which allow to toggle visibility of its toolbars /dock widget. If you have menu bar right click on it and you will see popup menu with such,. Your might want to place these commands in a View menu.
-
@alex_malyu Thanks! for the info! :)