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. QTreeView on_item_clicked and on_item_doubleclicked
Forum Updated to NodeBB v4.3 + New Features

QTreeView on_item_clicked and on_item_doubleclicked

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 909 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.
  • C Offline
    C Offline
    Chrisw01
    wrote on last edited by Chrisw01
    #1

    Hello,

    I have a QTreeView widget that is populated by a database. I have a couple of functions defined to handle the items single and double click. Only problem is, they are triggered by any button on the mouse. Right, Left, and Scroll wheel will fire the appropriate code. My question is, how can I determine within the on_xxx slot which button fired the event, Right, Left or Center. Is this possible without a massive amount of recoding?

    void amsSupport::on_amsSupportTreeViewWidget_itemClicked(QTreeWidgetItem *item, int column) {
    
       Q_UNUSED(column)
    
       if(item->parent() != nullptr) {
           ui->amsSupportTreeViewWidget->setSelectionMode(QAbstractItemView::MultiSelection);
           ui->amsSupportReturnButton->setEnabled(true);
           ui->amsSupportEditButton->setEnabled(false);
           ui->amsSupportDeleteButton->setEnabled(false);
           ui->amsSupportPrintButton->setEnabled(false);
           ui->amsSupportEmailButton->setEnabled(false);
       }
       else {
           ui->amsSupportTreeViewWidget->setSelectionMode(QAbstractItemView::SingleSelection);
           ui->amsSupportTreeViewWidget->selectionModel()->clearSelection();
           ui->amsSupportEditButton->setEnabled(true);
           ui->amsSupportDeleteButton->setEnabled(true);
           ui->amsSupportPrintButton->setEnabled(true);
           ui->amsSupportReturnButton->setEnabled(false);
           ui->amsSupportEmailButton->setEnabled(true);
           item->setSelected(true);
       }
    }
    

    What I am trying to accomplish is only selecting the appropriate line if the Left mouse button was pressed.

    Using Qt 6.4.3 on Windows 10 mingw compiler.

    Thanks in advance.

    Axel SpoerlA JonBJ Christian EhrlicherC 3 Replies Last reply
    0
    • C Chrisw01

      Hello,

      I have a QTreeView widget that is populated by a database. I have a couple of functions defined to handle the items single and double click. Only problem is, they are triggered by any button on the mouse. Right, Left, and Scroll wheel will fire the appropriate code. My question is, how can I determine within the on_xxx slot which button fired the event, Right, Left or Center. Is this possible without a massive amount of recoding?

      void amsSupport::on_amsSupportTreeViewWidget_itemClicked(QTreeWidgetItem *item, int column) {
      
         Q_UNUSED(column)
      
         if(item->parent() != nullptr) {
             ui->amsSupportTreeViewWidget->setSelectionMode(QAbstractItemView::MultiSelection);
             ui->amsSupportReturnButton->setEnabled(true);
             ui->amsSupportEditButton->setEnabled(false);
             ui->amsSupportDeleteButton->setEnabled(false);
             ui->amsSupportPrintButton->setEnabled(false);
             ui->amsSupportEmailButton->setEnabled(false);
         }
         else {
             ui->amsSupportTreeViewWidget->setSelectionMode(QAbstractItemView::SingleSelection);
             ui->amsSupportTreeViewWidget->selectionModel()->clearSelection();
             ui->amsSupportEditButton->setEnabled(true);
             ui->amsSupportDeleteButton->setEnabled(true);
             ui->amsSupportPrintButton->setEnabled(true);
             ui->amsSupportReturnButton->setEnabled(false);
             ui->amsSupportEmailButton->setEnabled(true);
             item->setSelected(true);
         }
      }
      

      What I am trying to accomplish is only selecting the appropriate line if the Left mouse button was pressed.

      Using Qt 6.4.3 on Windows 10 mingw compiler.

      Thanks in advance.

      Axel SpoerlA Offline
      Axel SpoerlA Offline
      Axel Spoerl
      Moderators
      wrote on last edited by
      #2

      @Chrisw01
      The short answer is: No, the slot doesn't know which button has caused the signal to be fired.
      But you could install an event filter and ignore the unwanted buttons, even depending on the mouse position.

      Software Engineer
      The Qt Company, Oslo

      1 Reply Last reply
      1
      • C Chrisw01

        Hello,

        I have a QTreeView widget that is populated by a database. I have a couple of functions defined to handle the items single and double click. Only problem is, they are triggered by any button on the mouse. Right, Left, and Scroll wheel will fire the appropriate code. My question is, how can I determine within the on_xxx slot which button fired the event, Right, Left or Center. Is this possible without a massive amount of recoding?

        void amsSupport::on_amsSupportTreeViewWidget_itemClicked(QTreeWidgetItem *item, int column) {
        
           Q_UNUSED(column)
        
           if(item->parent() != nullptr) {
               ui->amsSupportTreeViewWidget->setSelectionMode(QAbstractItemView::MultiSelection);
               ui->amsSupportReturnButton->setEnabled(true);
               ui->amsSupportEditButton->setEnabled(false);
               ui->amsSupportDeleteButton->setEnabled(false);
               ui->amsSupportPrintButton->setEnabled(false);
               ui->amsSupportEmailButton->setEnabled(false);
           }
           else {
               ui->amsSupportTreeViewWidget->setSelectionMode(QAbstractItemView::SingleSelection);
               ui->amsSupportTreeViewWidget->selectionModel()->clearSelection();
               ui->amsSupportEditButton->setEnabled(true);
               ui->amsSupportDeleteButton->setEnabled(true);
               ui->amsSupportPrintButton->setEnabled(true);
               ui->amsSupportReturnButton->setEnabled(false);
               ui->amsSupportEmailButton->setEnabled(true);
               item->setSelected(true);
           }
        }
        

        What I am trying to accomplish is only selecting the appropriate line if the Left mouse button was pressed.

        Using Qt 6.4.3 on Windows 10 mingw compiler.

        Thanks in advance.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @Chrisw01
        Additional to @Axel-Spoerl and alternative to an event filter, if you wanted to know what button was clicked I think you would have to make use of QTreeView::mousePress/ReleaseEvent() to inspect the QMouseEvent rather then just the clicked signal.

        C 1 Reply Last reply
        0
        • JonBJ JonB

          @Chrisw01
          Additional to @Axel-Spoerl and alternative to an event filter, if you wanted to know what button was clicked I think you would have to make use of QTreeView::mousePress/ReleaseEvent() to inspect the QMouseEvent rather then just the clicked signal.

          C Offline
          C Offline
          Chrisw01
          wrote on last edited by Chrisw01
          #4

          Hi and thanks for the replies.

          I was afraid of that. I added some debug code to the function and its kind of weird. The on_clicked seams to be working fine. It will fire on the press of the left mouse button but not the center/roller or right button. I'd say that is normal behavior. But the on_doubleclicked function will fire on a double click of any of the mouse buttons. Not sure if that is right or not. What my issue is, my context menu code will not fire on a right click. I thought that perhaps the on_clicked/doubleclicked was intercepting the right click. Back to the books!

          [EDIT]
          One other thing I noticed just now is if you Left click an item followed by a Right click it will fire the on_doubleClicked code as well, I don't think that is proper either.

          Thanks again...

          1 Reply Last reply
          0
          • C Chrisw01

            Hello,

            I have a QTreeView widget that is populated by a database. I have a couple of functions defined to handle the items single and double click. Only problem is, they are triggered by any button on the mouse. Right, Left, and Scroll wheel will fire the appropriate code. My question is, how can I determine within the on_xxx slot which button fired the event, Right, Left or Center. Is this possible without a massive amount of recoding?

            void amsSupport::on_amsSupportTreeViewWidget_itemClicked(QTreeWidgetItem *item, int column) {
            
               Q_UNUSED(column)
            
               if(item->parent() != nullptr) {
                   ui->amsSupportTreeViewWidget->setSelectionMode(QAbstractItemView::MultiSelection);
                   ui->amsSupportReturnButton->setEnabled(true);
                   ui->amsSupportEditButton->setEnabled(false);
                   ui->amsSupportDeleteButton->setEnabled(false);
                   ui->amsSupportPrintButton->setEnabled(false);
                   ui->amsSupportEmailButton->setEnabled(false);
               }
               else {
                   ui->amsSupportTreeViewWidget->setSelectionMode(QAbstractItemView::SingleSelection);
                   ui->amsSupportTreeViewWidget->selectionModel()->clearSelection();
                   ui->amsSupportEditButton->setEnabled(true);
                   ui->amsSupportDeleteButton->setEnabled(true);
                   ui->amsSupportPrintButton->setEnabled(true);
                   ui->amsSupportReturnButton->setEnabled(false);
                   ui->amsSupportEmailButton->setEnabled(true);
                   item->setSelected(true);
               }
            }
            

            What I am trying to accomplish is only selecting the appropriate line if the Left mouse button was pressed.

            Using Qt 6.4.3 on Windows 10 mingw compiler.

            Thanks in advance.

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Chrisw01 said in QTreeView on_item_clicked and on_item_doubleclicked:

            My question is, how can I determine within the on_xxx slot which button fired the event, Right, Left or Center.

            See QGuiApplication::mouseButtons() - it will work as long as you don't use a QueuedConnection.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            JonBJ C 2 Replies Last reply
            2
            • Christian EhrlicherC Christian Ehrlicher

              @Chrisw01 said in QTreeView on_item_clicked and on_item_doubleclicked:

              My question is, how can I determine within the on_xxx slot which button fired the event, Right, Left or Center.

              See QGuiApplication::mouseButtons() - it will work as long as you don't use a QueuedConnection.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @Christian-Ehrlicher said in QTreeView on_item_clicked and on_item_doubleclicked:

              See QGuiApplication::mouseButtons()

              Wow, never knew about that one!!

              1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @Chrisw01 said in QTreeView on_item_clicked and on_item_doubleclicked:

                My question is, how can I determine within the on_xxx slot which button fired the event, Right, Left or Center.

                See QGuiApplication::mouseButtons() - it will work as long as you don't use a QueuedConnection.

                C Offline
                C Offline
                Chrisw01
                wrote on last edited by
                #7

                @Christian-Ehrlicher

                Thanks, for that information, that (although I still think the on_doubleClick function should operate the same as the on_click function), provided me a way of making sure it was the left button firing the event.

                Thanks everyone for your input..

                1 Reply Last reply
                0
                • C Chrisw01 has marked this topic as solved on

                • Login

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