Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to handle mouse Right click in Qt

Tags:

qt

qlistview

I am using QListView to show list of friends' names. When I click on a name it should select a name and show profile related information and on right click it needs to show context menu without selecting a name and showing profile information. The problem I am facing is on right click it is selecting the name and also shows the context menu. I don't want the name to be selected on the right click and only the context menu should be shown. I am using the Qt contextmenuevent like:

void contextMenuEvent(QContextMenuEvent *ce)
{
    QPoint pos = ce->pos();
    emit customContextMenuRequested(pos);
}   

This doesn't work and the above slot is never called.

like image 822
Sanath Reddy Avatar asked Oct 25 '13 14:10

Sanath Reddy


1 Answers

use mousePressEvent and handle the right click like the following

void QkFriendsListView::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::RightButton)
    {
        emit customContextMenuRequested(event->pos());
    }
    else
        QListView::mousePressEvent(event)
}
like image 90
Sanath Reddy Avatar answered Nov 03 '22 00:11

Sanath Reddy