3
votes

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.

1
Example code and perhaps saying which platform you are experiencing this on would be helpful.Brian Roach
I have posted working code that shows the problem here: qtcentre.org/threads/…. Scroll to the last post. Thanks.Dave Berk
after 6 years in Qt 5.8 this problem still exists ( have you solved it?Rinat

1 Answers

0
votes

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