QWidgetAction在trigger()后仍然可见

3 投票
1 回答
1006 浏览
提问于 2025-04-15 22:56

我有一个QWidgetAction,它里面包含了一个QWidget,这个QWidget由一个QLineEdit和一个QPushButton组成。当用户按下按钮时,QWidgetAction会调用一个触发的槽函数。

现在我有一个QMenu,我用exec来激活它。问题是,虽然触发函数被调用了(我还把它连接到一个打印函数来检查),但是菜单却不会关闭。

普通的QActions工作得很好。

有人知道为什么会这样吗?

附言:我在网上查这个问题时,发现了一些人也遇到过类似的问题,但没有找到解决办法。

1 个回答

0

这个问题虽然已经有几年了,但我还是有个答案,希望能帮到大家!

我将描述我的完整解决方案,这个方案不仅能隐藏菜单,还能管理视觉效果。

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

撰写回答