Inserting a QAction into a QLayout
-
Hello,
I'm trying to change my app layout and while moving a bunch of buttons from one place to another, I came across aQAction
(currently located in aQToolBar
) that I would like to move to aQLayout
.
I tried to callmyLayout->insertWidget(0, ui->myToolbar->widgetForAction(ui->myAction));
The button will disappear from the toolbar but does not appear in the layout.
Is there a way to achieve this?Thanks in advance
-
An action is not a widget so you can't add it to a layout. What do you really want to do? If you want to execute an action after e.g. a button was pressed then add a button and connect the appropriate signal to the action.
-
@Christian-Ehrlicher ui->myToolbar->widgetForAction(ui->myAction) is a widget, not an action.
I would prefer to create a new widget for this action. Otherwise, the same widget will be in two layouts. -
@JoeCFD said in Inserting a QAction into a QLayout:
I would prefer to create a new widget for this action
Then do this.
-
Hi,
You can associate an action with several widgets for example you can use the same action in a toolbar and a menu.
-
@Christian-Ehrlicher said in Inserting a QAction into a QLayout:
@JoeCFD said in Inserting a QAction into a QLayout:
I would prefer to create a new widget for this action
Then do this.
He's not the original poster :)
I would like the widget to react to the QAction visible/disable state (event the tooltip text is shared accross widgets), juste like other QWidgets associated to that QAction.
As @SGaist said a QAction can be present in several toolbars and menus. -
So still don't understand the problem. Then add the action to e.g. a QPushButton or similar... and action can be associated to more than one widget: https://doc.qt.io/qt-5/qaction.html#associatedWidgets
-
@Christian-Ehrlicher said in Inserting a QAction into a QLayout:
So still don't understand the problem. Then add the action to e.g. a QPushButton or similar... and action can be associated to more than one widget: https://doc.qt.io/qt-5/qaction.html#associatedWidgets
I'm not sure I understood how I can add my own QPushButton to the associatedWidgets
-
Via
QWidget::addAction
as described in the link. But it will not help much since a QPushButton does not react onQEvent::ActionChanged
- only a QToolButton or QMenu and some others do. Therefore either derive from QPushButton and do it by your own or, as I already said in my first post: 'If you want to execute an action after e.g. a button was pressed then add a button and connect the appropriate signal to the action.' -
As you said addAction() does not help with my request. It will note even trigger the associated action
-
Why use a QPushButton if you want to move a QToolButton around ?
-
@SGaist what are you suggesting? Is there a way I can easily insert a QAction in a layout as a QToolButton?
If I need to create my own toolbutton, connect all the signals and icon/title changes myself, then it defeats the whole purpose of using a QAction -
Add a QToolButton to the layout and add the action with QWidget::addAction() as I already told you and the documentation tells you.
-
I have added a QToolBar in my layout, and add the actions to the toolbar, it works and I think that is the right way to do this.
-
@Christian-Ehrlicher said in Inserting a QAction into a QLayout:
Add a QToolButton to the layout and add the action with QWidget::addAction() as I already told you and the documentation tells you.
This doesn't work. It will add a dropdown icon suggesting the toolbutton has a menu and nothing else
-
Then use signals/slots