有没有一种方法可以将QWidget添加到QtCreator中的QMenu [英] Is there a way to add a QWidget to a QMenu in QtCreator

查看:168
本文介绍了有没有一种方法可以将QWidget添加到QtCreator中的QMenu的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个文本编辑器,我想将QComboBox放在QMenu中.我没有在QMenu内找到处理此类问题的任何方法.最接近的是QMenu::addAction().我想知道如何克服这个障碍.

I'm creating a text editor and I'd like to put the QComboBox in the QMenu. I didn't find any method inside the QMenu that handled such a thing. The closest is QMenu::addAction(). I was wondering of getting around this hurdle.

谢谢!

推荐答案

您必须继承 QWidgetAction ,然后只需调用 addAction 转到菜单.

You have to subclass QWidgetAction and then simply call the addAction to your menu.

带有标签的旋转框操作的示例代码

Example code for Spin Box Action with a label

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;
};

现在只需创建它并将其添加到菜单中

Now simply create it and add it to your menu

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);

这篇关于有没有一种方法可以将QWidget添加到QtCreator中的QMenu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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