How to put pushbutton inside the QMenu or QAction control?

前端 未结 2 1146
南笙
南笙 2021-01-18 04:01

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

I want to achieve something like this:

2条回答
  •  别那么骄傲
    2021-01-18 04:27

    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.

提交回复
热议问题