c++ - QWidgetAction 在 trigger() 之后保持可见

标签 c++ python qt pyqt

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

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

常规 QActions 效果很好。

知道为什么吗?

附言谷歌这个问题我来了across people with同样的问题,但没有解决方案。

最佳答案

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

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

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

关于c++ - QWidgetAction 在 trigger() 之后保持可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2873106/

相关文章:

python - 消费者之后的 RabbitMQ 清理

python - Numpy 和 += 效果

c++ - QtCreator在Linux上找不到lastfm库文件liblastfm.so.1

c++ - Qt 选择现有文件

python - 400(错误请求)和无效 session

C++ Qt 基类虚析构函数

c++ - 哪种方法最适合从光驱中快速读取文件?

c++ - 分配大量内存会出错?为什么?

c++ - QSplitter 中的固定宽度小部件

c++ - 使用模板化仿函数segfaults调用printf(仅64位,在32位中使用valgrind clean)