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

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

问题描述

我需要在 QMenu 中放置一个 QPushButton.有可能吗,如果有,那又如何?

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

我想实现这样的目标:

推荐答案

QWidgetAction 正是您要找的.这是 qt 文档中的内容

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

QWidgetAction 类通过一个接口扩展了 QAction,用于将自定义小部件插入到基于动作的容器中

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

所以基本上它会根据您传递给它的 QWidget 为 QAction 提供自定义 UI.我使用 QWidgetAction 将复选框显示为 QMenu 项.

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天全站免登陆