Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

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

    General and Desktop
    5
    9
    42734
    Loading More Posts
    • 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
      KA51O last edited by p3c0

      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 Reply Quote 0
      • V
        vsorokin last edited by p3c0

        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 Reply Quote 0
        • K
          KA51O last edited by p3c0

          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 Reply Quote 0
          • B
            broadpeak last edited by p3c0

            As I know this set the item as selected:

            setCurrentIndex(indexAt(event->pos()));
            or
            treeView->setCurrentIndex(somemodelindex);
            
            1 Reply Last reply Reply Quote 1
            • K
              KA51O last edited by p3c0

              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 Reply Quote 0
              • K
                KA51O last edited by

                @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 Reply Quote 0
                • B
                  broadpeak last edited by

                  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 Reply Quote 0
                  • Q
                    QtTester @KA51O last edited by

                    @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()

                    jsulm 1 Reply Last reply Reply Quote 0
                    • jsulm
                      jsulm Lifetime Qt Champion @QtTester last edited by

                      @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 Reply Quote 1
                      • First post
                        Last post