How can I implement the button, that holds an action (QAction) and can connect with one in designtime in QtDesigner?

Maxim Popravko picture Maxim Popravko · Nov 10, 2010 · Viewed 9.2k times · Source

I want to use an approach, when most of application commands hold in QActions, so that I could drag actions easily into menu, or toolbar, or button, or anything. So, I need to implement such a button. It is easy to write some class, that will hold it, take icon, text, shortcut and tooltip from it and connect clicked() to triggered(). But I can't even force my button's 'action' property into designer. Seems, that only QVariant holdable types may appear in it's property editor.

BUT! Trolls did it somehow, so the task should be achievable. So, any suggestions?

Answer

Jérôme picture Jérôme · Nov 11, 2010

I'm not sure, but I understand that you have an action (created with QtDesigner) and you want to associate this action with a menu, a toolbar button, and a normal button as well.

With QtDesigner, it is easy to use a QAction as a menu-item and as a toolbar button.

If you want to use this QAction with a normal button as well, I guess you cannot do it only with Qt Designer.

My suggestion is to add on your form, with QtDesigner a QToolButton.

In you class constructor, you can then tell the QToolButton that it is connected to your QAction with setDefaultAction().

ui->toolButton->setDefaultAction(ui->actionHello);

You might have to adjust accordingly the geometry of the QToolButton.

Now, if you click on it, the action actionHello with be triggered.