如何将按钮放在 QMenu 或 QAction 控件内? [英] How to put pushbutton inside the QMenu or QAction control?

查看:12
本文介绍了如何将按钮放在 QMenu 或 QAction 控件内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to achieve something like this:

解决方案

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.

这篇关于如何将按钮放在 QMenu 或 QAction 控件内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆