有一种方法来添加一个Widget到QtCreator中的QMenu [英] Is there a way to add a Widget to a QMenu in QtCreator

查看:1545
本文介绍了有一种方法来添加一个Widget到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.

谢谢!

推荐答案

http://doc.qt.nokia.com/latest/qwidgetaction.html> QWidgetAction ,然后只需调用 addAction 到您的菜单。

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

Spin Box动作的示例代码

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

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

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