CustomContextMenu doesn't always signal my action

  • Hi all.

    I have an oddball issue I've been hanging my head against for a few hours, I'm hoping someone can help.

    QT 5.2.0 on OSX.

    I have a form with a QTableWidget containing three rows, each with a custom single column widget spanning the entire width of the table. The table is populated once during application launch and the cells remain inside the table at all times, never destroyed.

    The custom cell (inherits from QWidget) and works well for everything I need, except that my customContextMenu associated with the cell isn't signaling my actions very reliably. For example, only 1 in 8 attempts to select a popup item from the menu results in the action firing, it's usually being ignored.

    For completeness during debugging I added signals to the contextMenu to show when I was hovering over a popup item vs triggering it. In all cases the hover signal is emitted correctly by QMenu - whether I have one or 10 entries in the menu. However, only in some random cases is the trigger signal emitted upon selection, and the action being called.

    Here's a simplified code view:

    void customTableCell::menuHovered(QAction *action)
    qDebug() << "Hovered" << action->text();

    void customTableCell::menuTriggered(QAction *action)
    qDebug() << "Triggered" << action->text();

    /* .h
    private slots:
    void menuHovered(QAction *action);
    void menuTriggered(QAction *action);
    void onMenuDoSomething(void);
    void customTableCell::onMenuDoSomething(void)
    qDebug() << "onMenuDoSomething()";

    here's my contextMenu:

    void customTableCell::on_customContextMenuRequested(const QPoint &pos)

    QMenu *contextMenu = new QMenu( this );
    Q_CHECK_PTR ( contextMenu );
    contextMenu->addAction("Do Something", this , SLOT(onMenuDoSomething(void)));
    connect(contextMenu, SIGNAL(hovered(QAction *)), this, SLOT(menuHovered(QAction *)));
    connect(contextMenu, SIGNAL(triggered(QAction *)), this, SLOT(menuTriggered(QAction *)));
    contextMenu->popup( QCursor::pos() );
    contextMenu->exec&#40; &#41;;
    delete contextMenu;
    contextMenu = 0;


    It doesn't matter whether I have a single entry in the menu, or 10 items, QT is randomly choosing to trigger my actions or not.

    In every other case where I've used customContextMenus (on different widgets) they work 100% reliably, this is the first time I've attached a menu to an object derived from QWidget and seen this behavior.

    Follow-up no shown here:

    ->exec() returns the QAaction object correctly only when the Action has truly fired. If the action never fired, even though it was selected, ->exec() returns null.

    Additionally, placing signals on the QAction itself (triggered() ) only emits when the solution works, 1 in 8 or so times.

    Feels like a bug in QMenu but I'm not sure how to work around this.

    Any ideas?


    • Steve

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Can you try with 5.2.1 to see if it still behaves incorrectly ?

    If so, you should consider reporting it on "the bug report system": .

    Please provide a minimal compilable sample program that reproduce the behavior

  • Thanks for the feedback.

    Upgrading to 5.2.1 didn't help.

    Interesting though, the same code works perfectly with 5.2.1 on Linux. 100% reliable.

    Mac bug I think.

    • Steve

  • Lifetime Qt Champion

    Sounds like it indeed, please open a report (if none already exists) with a minimal compilable example.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.