yonutix
yonutix

Reputation: 2439

Qt press left click twice for context menu to disappear

I'm trying to use a context menu from Qt when I press right click.

Here is what I've tried:

connect(mtreeView, SIGNAL(customContextMenuRequested(const QPoint&)),
        this, SLOT(showContextMenu(const QPoint&)));

Then
void MainTreeViewController::showContextMenu(const QPoint& pos)
{
    QPoint globalPos = mtreeView->mapToGlobal(pos);
    QMenu rightClickMenu;
    rightClickMenu.addAction(QString("Option"));
    rightClickMenu.exec(globalPos);
}

When I press right click, the menu appears. Then if I press left click outside of it nothing happens. I must press left click twice in order to make the menu disappear.

Why does that happen? Thanks!

Upvotes: 1

Views: 667

Answers (1)

Lahiru Chandima
Lahiru Chandima

Reputation: 24068

This can happen if showContextMenu is called twice for a single right click. You can verify by setting a breakpoint in showContextMenu and checking whether it is called twice.

Probably your signal slot connection is created twice, which can be the reason behind this. You can verify by setting a breakpoint to the line where signal slot connection is made.

Upvotes: 3

Related Questions