What is the recommended way of working with QActions in multiple level hierarchy of widgets?
I'm plannin开发者_如何学编程g on using qActions for use globally in my application. The idea is to have an action to be available at any level in the widget parent/child hierarchy.
Let's say I have the following GUI:
+--------------+
| +----------+ |
| | +----+ | |
| | | W2 | | |
| | +----+ | |
| | W1 | |
| +----------+ |
| MainWindow |
+--------------+
W2 has a QPushButton which I want to use with a QAction defined in MainWindow. So the question is: What is the recommended way of working with QActions in multiple level hierarchy of widgets?
Here are my approaches:
Define the actions in a MainWindow singleton, and access it from W2 to add the qaction to the QPushButton.
Define the actions in MainWindow and add them to W1, which in turn would add the qaction to W2, which in turn would add the action to the QPushButton (I don't like this one).
Create a Delegate singleton class to hold qactions and access it from MainWindow to make the global connections of qactions' triggers to the backend (model), and also access it from W2 to add the action wanted to the QPushButton.
Thanks
Personnally, I'd go with a modified version of the second option, in the other way, because it keeps the hierarchy of your program. I say the other way because I would make the w2 pushButton propagate the signal up to the mainWindow. An advantage to this option is that if you add another w2 in w1, you simply have to connect it to w1, your mainWindow will stay exactly the same (encapsulation). Same thing, if you add a w1 in your mainWindow, you only have to connect the new w1 to your mainWindow and you don't have to care with the pushButton in the w2 of the new w1.
Here is the code to make this solution works:
In your w2 class constructor:
connect(pushButton, SIGNAL(clicked(bool)), this, SIGNAL(buttonClicked(bool)));
In your w1 class constructor:
connect(widget2, SIGNAL(buttonClicked(bool)), this, SIGNAL(buttonClicked(bool)));
In your mainWindow class constructor:
connect(widget1, SIGNAL(buttonClicked(bool)), anyAction, SIGNAL(triggered(bool)));
connect(anyAction, SIGNAL(triggered(bool)), this, SLOT(onActionTriggered(bool)));
Obvisouly, you must define the correct signals/slot in your class definitions and be sure that whatever you connect has already been created.
Hope this helps.
精彩评论