how to handle mouse Right click in Qt

20,795

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)
}
Share:
20,795
Admin
Author by

Admin

Updated on August 07, 2022

Comments

  • Admin
    Admin almost 2 years

    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.

  • Yousuf Azad
    Yousuf Azad over 8 years
    From the name I am guessing QkFriendsListView inherits the Qt's ListView, is that correct?