![](/img/trans.png)
[英]Add QWidgetAction to QMenu through qtcreator/qtdesigner
[英]Is there a way to add a QWidget to a QMenu in QtCreator
我正在创建一个文本编辑器,我想把QComboBox
放在QMenu
。 我没有在QMenu
中找到任何处理这种事情的方法。 最接近的是QMenu::addAction()
。 我想知道绕过这个障碍。
谢谢!
您必须QWidgetAction
,然后只需将addAction
调用到您的菜单中。
具有标签的Spin Box Action的示例代码
class SpinBoxAction : public QWidgetAction {
public:
SpinBoxAction (const QString& title) :
QWidgetAction (NULL) {
QWidget* pWidget = new QWidget (NULL);
QHBoxLayout* pLayout = new QHBoxLayout();
QLabel* pLabel = new QLabel (title); //bug fixed here, pointer was missing
pLayout->addWidget (pLabel);
pSpinBox = new QSpinBox(NULL);
pLayout->addWidget (pSpinBox);
pWidget->setLayout (pLayout);
setDefaultWidget(pWidget);
}
QSpinBox * spinBox () {
return pSpinBox;
}
private:
QSpinBox * pSpinBox;
};
现在只需创建它并将其添加到菜单中
SpinBoxAction * spinBoxAction = new SpinBoxAction(tr("Action Title"));
// make a connection
connect(spinBoxAction ->spinBox(), SIGNAL(valueChanged(int)),
this, SLOT(spinboxValueChanged(int)));
// add it to your menu
menu->addAction(spinBoxAction);
QWidgetAction
是一个包含QWidget
的QAction
。 您可以使用它来封装QComboBox
并通过QMenu::addAction
将其添加到菜单中。
您可以随时使用QWidget
或QFrame
作为菜单小部件,然后在其上QHBoxLayout
,并将QWidgets
插入QWidgets
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.