QWidgetAction 在触发器()之后保持可见

发布于 2024-09-02 05:33:20 字数 637 浏览 2 评论 0原文

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

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

定期的 QActions 效果很好。

知道为什么吗?

PS谷歌搜索这个问题我来到 人们 同样的问题,但没有解决方案。

I have a a QWidgetAction which holds a QWidget composed of a QLineEdit and a QPushButton. Once the user press the button the QWidgetAction call the trigger slot.

Now I have a QMenu which I activate with exec. The problem is that even though trigger is called (I've connected it to a print function as well to check) the menu won't close.

Regular QActions works well.

Any idea why?

P.S. Googling this issue I came across people with the same problem, but no solutions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诠释孤独 2024-09-09 05:33:20

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

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

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

Years old question, but still I have an answer, hope it helps anybody!

I will describe my complete solution which not only hides the menu but also manages the visual representation.

QWidgetAction subclass: 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 subclass 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;
}

Finally to insert the QWidgetAction in a menu, in your code add the following:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文