繁体   English   中英

有没有办法将QWidget添加到QtCreator中的QMenu

[英]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是一个包含QWidgetQAction 您可以使用它来封装QComboBox并通过QMenu::addAction将其添加到菜单中。

您可以随时使用QWidgetQFrame作为菜单小部件,然后在其上QHBoxLayout ,并将QWidgets插入QWidgets

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM