Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put pushbutton inside the QMenu or QAction control?

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

I want to achieve something like this:

image

like image 562
liri2006 Avatar asked Jan 02 '12 19:01

liri2006


2 Answers

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

The QWidgetAction class extends QAction by an interface for inserting custom widgets into action based containers

So basically it gives a custom UI to QAction according to what QWidget you pass to it. I have used QWidgetAction to show checkbox as QMenu items.

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

You can then handle signals from checkbox accordingly.

like image 157
dev Avatar answered Oct 03 '22 22:10

dev


If you only want a menu item to have a state, you may use Checkable property of QAction:

rotateAct = new QAction(QIcon(":/images/Mouse/Rotate.png"), tr("&Rotate"), this);
rotateAct->setCheckable(true);
like image 20
Vladimir Shutow Avatar answered Oct 03 '22 22:10

Vladimir Shutow