gpt4 book ai didi

c++ - 有没有办法在 QtCreator 中将 QWidget 添加到 QMenu

转载 作者:可可西里 更新时间:2023-11-01 15:40:20 25 4
gpt4 key购买 nike

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

谢谢!

最佳答案

你必须继承QWidgetAction然后简单地调用 addAction到您的菜单。

带有标签的旋转框 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);

关于c++ - 有没有办法在 QtCreator 中将 QWidget 添加到 QMenu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8358965/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com