How to set a push button switch inside a QMenu or QAction controller?

I need to put a QPushButton inside QMenu . Is it possible, and if so, how?

I want to achieve something like this:

image

+6
source share
2 answers

QWidgetAction is what you are looking for. This is what is on qt docs

The QWidgetAction class extends the QAction interface for inserting custom widgets into containers based on actions

So basically this gives the user interface for QAction according to what QWidget you pass to it. I used QWidgetAction to show the checkbox as QMenu elements.

 QCheckBox *chkBox = new QCheckBox(menu); chkBox ->setText("MyCheckBox"); QWidgetAction *chkBoxAction= new QWidgetAction(menu); chkBoxAction->setDefaultWidget(chkBox); menu->addAction(chkBoxAction); 

Then you can process the signals from the checkbox.

+9
source

If you want the menu item to have a state, you can use the Checkable property for QAction:

 rotateAct = new QAction(QIcon(":/images/Mouse/Rotate.png"), tr("&Rotate"), this); rotateAct->setCheckable(true); 
0
source

Source: https://habr.com/ru/post/904938/


All Articles