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. [SOLVED] How to set an item in a QTreeView as selected?

[SOLVED] How to set an item in a QTreeView as selected?

Scheduled Pinned Locked Moved General and Desktop
9 Posts 5 Posters 46.3k Views
  • 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.
  • K Offline
    K Offline
    KA51O
    wrote on last edited by p3c0
    #1

    Hi,

    I'm having this function to open a context menu in a QTreeView:

    void MyTreeView::mousePressEvent(QMouseEvent* event)
    {
       // get the buttons type
       Qt::MouseButtons mouseButton = event->buttons();
       // only the right mouse buton
       if( mouseButton == Qt::RightButton )
       {
          //remember previous selection and then clear selection
          QPersistentModelIndex prevIndex = selectedIndexes()[0];
          clearSelection();
          //select item at cursor position
          QPersistentModelIndex nextIndex = indexAt(QCursor::pos());
          QItemSelection prevSelection (prevIndex, prevIndex);
          QItemSelection nextSelection (nextIndex, nextIndex);
          selectionChanged(prevSelection, nextSelection);
          // start the context menu
          QModelIndexList sel (selectedIndexes());
          if ( sel.size() > 0 && sel[0].isValid() )
          {
             m_pContextMenu->exec(QCursor::pos());
          }
       }
       else
       {
          //call the parents function
          QTreeView::mousePressEvent(event);
       }
    }
    

    I've tried various other methods to set the item at the cursor position to be selected but have yet failed to achieve my goal.

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vsorokin
      wrote on last edited by p3c0
      #2

      Look to

      QItemSelectionModel * QAbstractItemView::selectionModel () const
      

      and _QItemSelectionModel_ methods

      virtual void select ( const QModelIndex & index, QItemSelectionModel::SelectionFlags command )
      virtual void select ( const QItemSelection & selection, QItemSelectionModel::SelectionFlags command )
      

      --
      Vasiliy

      1 Reply Last reply
      0
      • K Offline
        K Offline
        KA51O
        wrote on last edited by p3c0
        #3

        Thanks for the answer Vass. But i've also tried that before. See below.

        void MyTreeView::mousePressEvent(QMouseEvent* event)
        {
           // get the buttons type
           Qt::MouseButtons mouseButton = event->buttons();
           // only the right mouse buton
           if( mouseButton == Qt::RightButton )
           {
              //select item at cursor position
              QPersistentModelIndex nextIndex = indexAt(QCursor::pos());
              selectionModel()->select(nextIndex, QItemSelectionModel::SelectCurrent);
              // start the context menu
              QModelIndexList sel (selectedIndexes());
              if ( sel.size() > 0 && sel[0].isValid() )
              {
                 m_pContextMenu->exec(QCursor::pos());
              }
           }
           else
           {
              //call the parents function
              QTreeView::mousePressEvent(event);
           }
        }
        

        The result is that the previously selected item is deselected but the item at the cursor position is not selected and since no item is selected the context menu doesn't open.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          broadpeak
          wrote on last edited by p3c0
          #4

          As I know this set the item as selected:

          setCurrentIndex(indexAt(event->pos()));
          or
          treeView->setCurrentIndex(somemodelindex);
          
          1 Reply Last reply
          1
          • K Offline
            K Offline
            KA51O
            wrote on last edited by p3c0
            #5

            Thanks broadpeak @setCurrentIndex(indexAt(event->pos()));@ worked.
            I tried this

            QPersistentModelIndex nextIndex = indexAt(QCursor::pos());
            setCurrentIndex(nextIndex);
            

            before but that didn't work. Any ideas why?

            Q 1 Reply Last reply
            0
            • K Offline
              K Offline
              KA51O
              wrote on last edited by
              #6

              @Vass
              Your suggestion also worked when I used event->pos() instead of QCursor::pos().

              Guess I forgot to map the Cursor position from global to widget coordinates. Stupid me.

              1 Reply Last reply
              0
              • B Offline
                B Offline
                broadpeak
                wrote on last edited by
                #7

                before but that didn’t work. Any ideas why?

                Because the index comes from the model and the mousePressEvent comes from the event object (through view).
                The two is not the same.

                1 Reply Last reply
                0
                • K KA51O

                  Thanks broadpeak @setCurrentIndex(indexAt(event->pos()));@ worked.
                  I tried this

                  QPersistentModelIndex nextIndex = indexAt(QCursor::pos());
                  setCurrentIndex(nextIndex);
                  

                  before but that didn't work. Any ideas why?

                  Q Offline
                  Q Offline
                  QtTester
                  wrote on last edited by
                  #8

                  @KA51O Hi ka. I have a treewidget upon a mainwindow ,how to select the first item when app starts to run?
                  in mainwindow::() constructed function I don't have event->pos()

                  jsulmJ 1 Reply Last reply
                  0
                  • Q QtTester

                    @KA51O Hi ka. I have a treewidget upon a mainwindow ,how to select the first item when app starts to run?
                    in mainwindow::() constructed function I don't have event->pos()

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @QtTester What about https://doc.qt.io/qt-5/qtreewidget.html#setCurrentItem ?

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1

                    • Login

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