Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to prevent right mouse clicking to select item in QTableView?
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 7.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      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);
      }
      }
      @

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vittalonline
        wrote on last edited by
        #3
            view->setSelectionBehavior(QAbstractItemView::SelectRows);
            view->setSelectionMode(QAbstractItemView::SingleSelection);
        
        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved