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 get selected items in tree widget
Forum Updated to NodeBB v4.3 + New Features

How to get selected items in tree widget

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 3 Posters 1.9k 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.
  • D Offline
    D Offline
    deleted286
    wrote on last edited by
    #1

    I want to achieve the selected items. After i get them, i will double click and do some operations.
    My code doesnt work and I dont think my code is true

    void MainWindow::on_treeWidget2_itemDoubleClicked(QTreeWidgetItem *item, int column)
    {
    
    QList<QTreeWidgetItem *>items = treeWidget->selectedItems();
    qDebug() << items;
    
    }
    
    D 1 Reply Last reply
    0
    • D deleted286

      I want to achieve the selected items. After i get them, i will double click and do some operations.
      My code doesnt work and I dont think my code is true

      void MainWindow::on_treeWidget2_itemDoubleClicked(QTreeWidgetItem *item, int column)
      {
      
      QList<QTreeWidgetItem *>items = treeWidget->selectedItems();
      qDebug() << items;
      
      }
      
      D Offline
      D Offline
      deleted286
      wrote on last edited by
      #2

      @suslucoder it is work

      QList<QTreeWidgetItem*> slctdList = ui->treeWidget2->selectedItems();
      for(QTreeWidgetItem* item : slctdList)
      {
          scln = QString::fromStdU16String(item->text(0).toStdU16String());
          qDebug() << scln;
      
      }
      
      D 1 Reply Last reply
      0
      • D deleted286

        @suslucoder it is work

        QList<QTreeWidgetItem*> slctdList = ui->treeWidget2->selectedItems();
        for(QTreeWidgetItem* item : slctdList)
        {
            scln = QString::fromStdU16String(item->text(0).toStdU16String());
            qDebug() << scln;
        
        }
        
        D Offline
        D Offline
        deleted286
        wrote on last edited by
        #3

        @suslucoder I have another question,

        in table widget i can restrict the selected rows and do my operation like

        if(item->row() >=0 & item->row() <=10)
         { 
        ........
         }
        

        How should it be in tree widget?
        There is no row

        jsulmJ 1 Reply Last reply
        0
        • D deleted286

          @suslucoder I have another question,

          in table widget i can restrict the selected rows and do my operation like

          if(item->row() >=0 & item->row() <=10)
           { 
          ........
           }
          

          How should it be in tree widget?
          There is no row

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

          @suslucoder https://doc.qt.io/qt-5/qtreeview.html#selectedIndexes
          https://doc.qt.io/qt-5/qmodelindex.html#row

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

          D 1 Reply Last reply
          0
          • jsulmJ jsulm

            @suslucoder https://doc.qt.io/qt-5/qtreeview.html#selectedIndexes
            https://doc.qt.io/qt-5/qmodelindex.html#row

            D Offline
            D Offline
            deleted286
            wrote on last edited by deleted286
            #5

            @jsulm i can get the index of my double clicked items like that

            qDebug() << ui->treeWidget2->currentIndex().row();
            

            but how can adapt it for selected ones

            JonBJ 1 Reply Last reply
            0
            • D deleted286

              @jsulm i can get the index of my double clicked items like that

              qDebug() << ui->treeWidget2->currentIndex().row();
              

              but how can adapt it for selected ones

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

              @suslucoder
              @jsulm just referred you to QTreeView::selectedIndexes(), which returns a QModelIndexList, and to QModelIndex, which has a row() member (as you use from currentIndex()). Can you not put those two together?

              for (QModelIndex index : ui->treeWidget2->selectedIndexes())
                  qDebug() << index.row();
              

              Or do you have some other question?

              D 1 Reply Last reply
              0
              • JonBJ JonB

                @suslucoder
                @jsulm just referred you to QTreeView::selectedIndexes(), which returns a QModelIndexList, and to QModelIndex, which has a row() member (as you use from currentIndex()). Can you not put those two together?

                for (QModelIndex index : ui->treeWidget2->selectedIndexes())
                    qDebug() << index.row();
                

                Or do you have some other question?

                D Offline
                D Offline
                deleted286
                wrote on last edited by
                #7

                @JonB but tree widget doesnt have selectedIndexes()

                JonBJ 1 Reply Last reply
                0
                • D deleted286

                  @JonB but tree widget doesnt have selectedIndexes()

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

                  @suslucoder
                  But it does have selectedIndexes. @jsulm even gave you the reference. Go to List of All Members for QTreeWidget, search for selectedIndexes. Please remember about C++ inheritance when you are programming.

                  D 1 Reply Last reply
                  1
                  • JonBJ JonB

                    @suslucoder
                    But it does have selectedIndexes. @jsulm even gave you the reference. Go to List of All Members for QTreeWidget, search for selectedIndexes. Please remember about C++ inheritance when you are programming.

                    D Offline
                    D Offline
                    deleted286
                    wrote on last edited by
                    #9

                    @JonB i remember but maybe qt dont.
                    Because it gives me that error

                    JonBJ 1 Reply Last reply
                    0
                    • D deleted286

                      @JonB i remember but maybe qt dont.
                      Because it gives me that error

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

                      @suslucoder

                      i remember but maybe qt dont.

                      So Qt/C++ inheritance does not work, and the documentation is wrong, but you are correct?

                      Because it gives me that error

                      What error? You have not shown any error. Again, how many times can we ask you: if you get an error, paste the line of code and paste the error message you receive. Could you please do this each time instead of saying "something does not work"?

                      EDIT
                      Ah, now hang on. I have just noticed that QModelIndexList QTreeView::selectedIndexes() const is documented [override virtual protected]. All the more reason to show the error message instead of saying "Because it gives me that error"....

                      Because of the protected, that means you will not be able to access ui->treeWidget2->selectedIndexes() unless you have subclassed treeWidget2 from the base QTreeWidget, which I do not imagine you have done. So instead you should use ui->treeWidget2->selectionModel() to access the selections. That QItemSelectionModel has methods selectedIndexes() or even selectedRows() to access the selected rows directly, which is what you asked for: qDebug() << ui->treeWidget2->selectionModel()->selectedRows().

                      D jsulmJ 2 Replies Last reply
                      2
                      • JonBJ JonB

                        @suslucoder

                        i remember but maybe qt dont.

                        So Qt/C++ inheritance does not work, and the documentation is wrong, but you are correct?

                        Because it gives me that error

                        What error? You have not shown any error. Again, how many times can we ask you: if you get an error, paste the line of code and paste the error message you receive. Could you please do this each time instead of saying "something does not work"?

                        EDIT
                        Ah, now hang on. I have just noticed that QModelIndexList QTreeView::selectedIndexes() const is documented [override virtual protected]. All the more reason to show the error message instead of saying "Because it gives me that error"....

                        Because of the protected, that means you will not be able to access ui->treeWidget2->selectedIndexes() unless you have subclassed treeWidget2 from the base QTreeWidget, which I do not imagine you have done. So instead you should use ui->treeWidget2->selectionModel() to access the selections. That QItemSelectionModel has methods selectedIndexes() or even selectedRows() to access the selected rows directly, which is what you asked for: qDebug() << ui->treeWidget2->selectionModel()->selectedRows().

                        D Offline
                        D Offline
                        deleted286
                        wrote on last edited by
                        #11

                        @JonB Ok. Anyway. I did it

                        if(item->parent() ==  myparentitem)
                          { ....}
                        
                        1 Reply Last reply
                        0
                        • JonBJ JonB

                          @suslucoder

                          i remember but maybe qt dont.

                          So Qt/C++ inheritance does not work, and the documentation is wrong, but you are correct?

                          Because it gives me that error

                          What error? You have not shown any error. Again, how many times can we ask you: if you get an error, paste the line of code and paste the error message you receive. Could you please do this each time instead of saying "something does not work"?

                          EDIT
                          Ah, now hang on. I have just noticed that QModelIndexList QTreeView::selectedIndexes() const is documented [override virtual protected]. All the more reason to show the error message instead of saying "Because it gives me that error"....

                          Because of the protected, that means you will not be able to access ui->treeWidget2->selectedIndexes() unless you have subclassed treeWidget2 from the base QTreeWidget, which I do not imagine you have done. So instead you should use ui->treeWidget2->selectionModel() to access the selections. That QItemSelectionModel has methods selectedIndexes() or even selectedRows() to access the selected rows directly, which is what you asked for: qDebug() << ui->treeWidget2->selectionModel()->selectedRows().

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

                          @JonB said in How to get selected items in tree widget:

                          instead of saying "Because it gives me that error"....

                          You have to ask some people again and again to provide basic information...

                          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