Important: Please read the Qt Code of Conduct -

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?



  • 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) {
    } else {

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

Log in to reply