How to prevent right mouse clicking to select item in QTableView?



  • Hi guys,

    I am using QTableView in my project, and i implemented a context menu in the QTableView.
    the problem is that: when i clicked right mouse button to pop up context menu, the item in the table will be selected too.
    i don't wanna item is selected when i clicked right mouse button.
    so, is there any swither or something else to do it?

    Thanks.

    -Landy



  • Subclass QTableView and reimplement mousePressEvent so that when right click event happens you just stop. Something like (might not be all correctly, it's just a sketch):

    @
    void MyClass::mousePressEvent(QMouseEvent *e) {
    if(e->button() == Qt::RightMouseButton) {
    return;
    } else {
    QTableView::mousePressEvent(e);
    }
    }
    @



  •     view->setSelectionBehavior(QAbstractItemView::SelectRows);
        view->setSelectionMode(QAbstractItemView::SingleSelection);

Log in to reply
 

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