【问题标题】:QWidgetAction stays visible after trigger()在 trigger() 之后 QWidgetAction 保持可见
【发布时间】:2011-02-21 19:40:48
【问题描述】:

我有一个 QWidgetAction,它包含一个由 QLineEdit 和 QPushButton 组成的 QWidget。一旦用户按下按钮,QWidgetAction 就会调用触发槽。

现在我有一个用 exec 激活的 QMenu。问题是即使调用了触发器(我已将其连接到打印功能以进行检查),菜单也不会关闭。

常规 QActions 效果很好。

知道为什么吗?

附:谷歌搜索这个问题我来了acrosspeoplewith同样的问题,但没有解决方案。

【问题讨论】:

  • 示例代码,也许说明您在哪个平台上遇到此问题会有所帮助。
  • 我在此处发布了显示问题的工作代码:qtcentre.org/threads/…。滚动到最后一篇文章。谢谢。
  • 在Qt 5.8 6年后这个问题仍然存在(你解决了吗?

标签: c++ python qt pyqt


【解决方案1】:

多年前的问题,但我仍然有答案,希望它可以帮助任何人!

我将描述我的完整解决方案,它不仅隐藏菜单,还管理视觉表示。

QWidgetAction 子类:MyClass.h

class MyClass : public QWidgetAction {
    Q_OBJECT
public:
    MyClass(QObject* parent);
    bool eventFilter(QObject*, QEvent*) override;

signals:
    void mouseInside();
    void mouseOutside();

protected:
    QWidget* createWidget(QWidget* parent) override;

private:
    QWidget* w;
    QWidget* border;
    QFormLayout *form;
    QHBoxLayout *mainLayout;
}

QWidgetAction 子类 MyClass.cpp

QWidget* MyClass::createWidget(QWidget* parent) {
    w          = new QWidget(parent);
    border     = new QWidget(parent);
    mainLayout = new QHBoxLayout(w);
    layout     = new QFormLayout();
    border->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Maximum));
    border->setMaximumWidth(10);
    border->setMinimumWidth(10);
    border->setMaximumHeight(1000); //Anything will do but it needs a height
    mainLayout->setContentsMargins(0, 0, 0, 0);
    mainLayout->setSpacing(0);
    w->setLayout(mainLayout);
    mainLayout->addWidget(border);
    mainLayout->addLayout(layout);
    layout->setContentsMargins(6, 11, 11, 11);
    layout->setSpacing(6);
    // Insert your widgets here, I used a QFormLayout
    QLineEdit *l = new QLineEdit(w);
    form->addRow("Test", l);
    // I added a button to accept input
    QPushButton* b = new QPushButton("Send", w);
    connect(b, SIGNAL(clicked()), this, SLOT(trigger()));
    layout->addWidget(b);
    w->installEventFilter(this); // This is to avoid non button clicks to close the menu
    return w;
}

bool MyClass::eventFilter(QObject*, QEvent* ev) {
    if (ev->type() == QEvent::MouseButtonPress
        || ev->type() == QEvent::MouseButtonDblClick
        || ev->type() == QEvent::MouseButtonRelease) {
        return true;
    } else if (ev->type() == QEvent::Enter) {
        border->setStyleSheet("background-color: #90c8f6;");
        emit mouseInside();
    } else if (ev->type() == QEvent::Leave) {
        border->setStyleSheet("");
        emit mouseOutside();
    }
    return false;
}

最后要在菜单中插入 QWidgetAction,在您的代码中添加以下内容:

QMenu *m = new QMenu(this);
MyClass *item = new MyClass(m);
connect(item, &QAction::triggered, [=] { m->hide(); YOUR CODE HERE}); // Add your action here

// This is to give a visual cue to your item, while deselecting the stuck 
// action which was previously selected
connect(item, &MyClass::mouseInside, [=] { m->setActiveAction(nullptr); }); 
ui->yourButton->setMenu(m);
m->addAction(item);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多