How to handle a mouse Right-click in Qt

I am using QListView to display a list of friend names. When I click on the name, he should select the name and show the information associated with the profile, and by right-clicking he needs to display the context menu without selecting the name and not displaying the profile information. The problem I am facing is right-clicking on the name and also displaying a context menu. I do not want the name to be selected by right-clicking, and only the context menu should be displayed. I am using Qt contextmenuevent as:

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

This does not work, and this slot is never called.

+4
source share
1 answer

mousePressEvent , .

void QkFriendsListView::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::RightButton)
    {
        emit customContextMenuRequested(event->pos());
    }
    else
        QListView::mousePressEvent(event)
}
+4

All Articles