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 create a tree widget from QList

How to create a tree widget from QList

Scheduled Pinned Locked Moved Solved General and Desktop
qt5tree widget
17 Posts 2 Posters 1.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.
  • D Offline
    D Offline
    deleted286
    wrote on 29 Mar 2021, 06:06 last edited by
    #1

    I have a table widget and im adding my datas into table like

    gpsList << inner_data[2].toString();
                gpsbrmList << inner_data[3].toString();
            for (int i=0; i<gpsList.size(); i++ )
            {
                ui->tableWidget->setItem(i, 0, new QTableWidgetItem(gpsList.at(i)));
                ui->tableWidget->setItem(i, 1, new QTableWidgetItem(gpsbrmList.at(i)));
            }
    

    How can i create a tree widget using the same list, which is gpsList?

    J 1 Reply Last reply 29 Mar 2021, 06:09
    0
    • D deleted286
      29 Mar 2021, 06:06

      I have a table widget and im adding my datas into table like

      gpsList << inner_data[2].toString();
                  gpsbrmList << inner_data[3].toString();
              for (int i=0; i<gpsList.size(); i++ )
              {
                  ui->tableWidget->setItem(i, 0, new QTableWidgetItem(gpsList.at(i)));
                  ui->tableWidget->setItem(i, 1, new QTableWidgetItem(gpsbrmList.at(i)));
              }
      

      How can i create a tree widget using the same list, which is gpsList?

      J Online
      J Online
      jsulm
      Lifetime Qt Champion
      wrote on 29 Mar 2021, 06:09 last edited by
      #2

      @suslucoder said in How to create a tree widget from QList:

      How can i create a tree widget using the same list, which is gpsList?

      Using https://doc.qt.io/qt-5/qtreewidget.html (there is even an example using a list!) I guess? Or what is your exact question?

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

      D 1 Reply Last reply 29 Mar 2021, 06:11
      0
      • J jsulm
        29 Mar 2021, 06:09

        @suslucoder said in How to create a tree widget from QList:

        How can i create a tree widget using the same list, which is gpsList?

        Using https://doc.qt.io/qt-5/qtreewidget.html (there is even an example using a list!) I guess? Or what is your exact question?

        D Offline
        D Offline
        deleted286
        wrote on 29 Mar 2021, 06:11 last edited by
        #3

        @jsulm I've tried it but it doesnt work

        J 1 Reply Last reply 29 Mar 2021, 06:13
        0
        • D deleted286
          29 Mar 2021, 06:11

          @jsulm I've tried it but it doesnt work

          J Online
          J Online
          jsulm
          Lifetime Qt Champion
          wrote on 29 Mar 2021, 06:13 last edited by
          #4

          @suslucoder said in How to create a tree widget from QList:

          I've tried it but it doesnt work

          You should already know that saying "I've tried it but it doesnt work" without providing any details is not helpful!
          Can you please show what you tried and also tell what the result was? Or should we guess?

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

          D 1 Reply Last reply 29 Mar 2021, 06:17
          1
          • J jsulm
            29 Mar 2021, 06:13

            @suslucoder said in How to create a tree widget from QList:

            I've tried it but it doesnt work

            You should already know that saying "I've tried it but it doesnt work" without providing any details is not helpful!
            Can you please show what you tried and also tell what the result was? Or should we guess?

            D Offline
            D Offline
            deleted286
            wrote on 29 Mar 2021, 06:17 last edited by
            #5

            @jsulm i tried this, it says that no viable conversion from 'const qstring' to 'const qlist tree widget item

            treeWidget->insertTopLevelItems(0, gpsList.at(i));
            
            J 1 Reply Last reply 29 Mar 2021, 06:24
            0
            • D deleted286
              29 Mar 2021, 06:17

              @jsulm i tried this, it says that no viable conversion from 'const qstring' to 'const qlist tree widget item

              treeWidget->insertTopLevelItems(0, gpsList.at(i));
              
              J Online
              J Online
              jsulm
              Lifetime Qt Champion
              wrote on 29 Mar 2021, 06:24 last edited by
              #6

              @suslucoder If you would have read documentation (https://doc.qt.io/qt-5/qtreewidget.html#insertTopLevelItems) you would know that insertTopLevelItems expects a list and not single value...
              Also, you're supposed to add QTreeWidgetItem to the tree and not QString.

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

              D 1 Reply Last reply 29 Mar 2021, 06:53
              0
              • J jsulm
                29 Mar 2021, 06:24

                @suslucoder If you would have read documentation (https://doc.qt.io/qt-5/qtreewidget.html#insertTopLevelItems) you would know that insertTopLevelItems expects a list and not single value...
                Also, you're supposed to add QTreeWidgetItem to the tree and not QString.

                D Offline
                D Offline
                deleted286
                wrote on 29 Mar 2021, 06:53 last edited by
                #7

                @jsulm said in How to create a tree widget from QList:

                Also, you're supposed to add QTreeWidgetItem to the tree and not QString.

                ok. I did it like this way, but it added the first row always, or should i say column i guess. I want to see my datas like in table widget

                ```
                    QTreeWidgetItem *gps = new QTreeWidgetItem(ui->treeWidget2);
                   gps->setText(0, tr("GPS"));
                
                QTreeWidgetItem *gpsChild = new QTreeWidgetItem(gps);
                gpsChild->setText(0, gpsList.at(0));
                 gpsChild->setText(1, gpsList.at(1));
                
                J 1 Reply Last reply 29 Mar 2021, 07:01
                0
                • D deleted286
                  29 Mar 2021, 06:53

                  @jsulm said in How to create a tree widget from QList:

                  Also, you're supposed to add QTreeWidgetItem to the tree and not QString.

                  ok. I did it like this way, but it added the first row always, or should i say column i guess. I want to see my datas like in table widget

                  ```
                      QTreeWidgetItem *gps = new QTreeWidgetItem(ui->treeWidget2);
                     gps->setText(0, tr("GPS"));
                  
                  QTreeWidgetItem *gpsChild = new QTreeWidgetItem(gps);
                  gpsChild->setText(0, gpsList.at(0));
                   gpsChild->setText(1, gpsList.at(1));
                  
                  J Online
                  J Online
                  jsulm
                  Lifetime Qt Champion
                  wrote on 29 Mar 2021, 07:01 last edited by
                  #8

                  @suslucoder said in How to create a tree widget from QList:

                  but it added the first row always, or should i say column i guess

                  Please use correct wording, else it is hard to understand.
                  Do you mean you only got one row with all elements of gpsList as columns?
                  This is exactly what your code is doing.
                  I ask you again to take a look at the link I posted, especially this part:

                  QTreeWidget *treeWidget = new QTreeWidget();
                  treeWidget->setColumnCount(1);
                  QList<QTreeWidgetItem *> items;
                  for (int i = 0; i < 10; ++i)
                      items.append(new QTreeWidgetItem(static_cast<QTreeWidget *>(nullptr), QStringList(QString("item: %1").arg(i))));
                  treeWidget->insertTopLevelItems(0, items);
                  

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

                  D 1 Reply Last reply 29 Mar 2021, 07:10
                  0
                  • J jsulm
                    29 Mar 2021, 07:01

                    @suslucoder said in How to create a tree widget from QList:

                    but it added the first row always, or should i say column i guess

                    Please use correct wording, else it is hard to understand.
                    Do you mean you only got one row with all elements of gpsList as columns?
                    This is exactly what your code is doing.
                    I ask you again to take a look at the link I posted, especially this part:

                    QTreeWidget *treeWidget = new QTreeWidget();
                    treeWidget->setColumnCount(1);
                    QList<QTreeWidgetItem *> items;
                    for (int i = 0; i < 10; ++i)
                        items.append(new QTreeWidgetItem(static_cast<QTreeWidget *>(nullptr), QStringList(QString("item: %1").arg(i))));
                    treeWidget->insertTopLevelItems(0, items);
                    
                    D Offline
                    D Offline
                    deleted286
                    wrote on 29 Mar 2021, 07:10 last edited by
                    #9

                    @jsulm ı understand it, but i dont understand how can i append my QList instead of items

                    J 1 Reply Last reply 29 Mar 2021, 07:14
                    0
                    • D deleted286
                      29 Mar 2021, 07:10

                      @jsulm ı understand it, but i dont understand how can i append my QList instead of items

                      J Online
                      J Online
                      jsulm
                      Lifetime Qt Champion
                      wrote on 29 Mar 2021, 07:14 last edited by jsulm
                      #10

                      @suslucoder

                      treeWidget->setColumnCount(1);
                      QList<QTreeWidgetItem *> items;
                      for (int i = 0; i < gpsList.size(); ++i)
                          items.append(new QTreeWidgetItem(static_cast<QTreeWidget *>(nullptr), QStringList() << gpsList.at(i)));
                      treeWidget->insertTopLevelItems(0, items);
                      

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

                      D 1 Reply Last reply 29 Mar 2021, 07:26
                      0
                      • J jsulm
                        29 Mar 2021, 07:14

                        @suslucoder

                        treeWidget->setColumnCount(1);
                        QList<QTreeWidgetItem *> items;
                        for (int i = 0; i < gpsList.size(); ++i)
                            items.append(new QTreeWidgetItem(static_cast<QTreeWidget *>(nullptr), QStringList() << gpsList.at(i)));
                        treeWidget->insertTopLevelItems(0, items);
                        
                        D Offline
                        D Offline
                        deleted286
                        wrote on 29 Mar 2021, 07:26 last edited by
                        #11

                        @jsulm I did it like that, is there anything seems wrong?

                        ```
                           QTreeWidgetItem *imu = new QTreeWidgetItem(ui->treeWidget2);
                          imu->setText(0, tr("IMU"));
                        
                        QList<QTreeWidgetItem *> imu_items;
                        for (int i = 0; i<imuList.size(); ++i)
                        {
                            imu_items.append(new QTreeWidgetItem(static_cast<QTreeWidget *>(nullptr), QStringList(imuList.at(i))));
                        }
                        imu->addChildren(imu_items);
                        
                        J 1 Reply Last reply 29 Mar 2021, 07:28
                        0
                        • D deleted286
                          29 Mar 2021, 07:26

                          @jsulm I did it like that, is there anything seems wrong?

                          ```
                             QTreeWidgetItem *imu = new QTreeWidgetItem(ui->treeWidget2);
                            imu->setText(0, tr("IMU"));
                          
                          QList<QTreeWidgetItem *> imu_items;
                          for (int i = 0; i<imuList.size(); ++i)
                          {
                              imu_items.append(new QTreeWidgetItem(static_cast<QTreeWidget *>(nullptr), QStringList(imuList.at(i))));
                          }
                          imu->addChildren(imu_items);
                          
                          J Online
                          J Online
                          jsulm
                          Lifetime Qt Champion
                          wrote on 29 Mar 2021, 07:28 last edited by
                          #12

                          @suslucoder said in How to create a tree widget from QList:

                          is there anything seems wrong?

                          No.
                          Does it work?

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

                          D 1 Reply Last reply 29 Mar 2021, 07:29
                          0
                          • J jsulm
                            29 Mar 2021, 07:28

                            @suslucoder said in How to create a tree widget from QList:

                            is there anything seems wrong?

                            No.
                            Does it work?

                            D Offline
                            D Offline
                            deleted286
                            wrote on 29 Mar 2021, 07:29 last edited by
                            #13

                            @jsulm yes, but i wonder is there any logical error of my code.
                            Thank you

                            D 1 Reply Last reply 29 Mar 2021, 07:39
                            0
                            • D deleted286
                              29 Mar 2021, 07:29

                              @jsulm yes, but i wonder is there any logical error of my code.
                              Thank you

                              D Offline
                              D Offline
                              deleted286
                              wrote on 29 Mar 2021, 07:39 last edited by
                              #14

                              @suslucoder How i have second column, and i want to add something to my child object.
                              Should i create new tree widget items?

                              J 1 Reply Last reply 29 Mar 2021, 08:01
                              0
                              • D deleted286
                                29 Mar 2021, 07:39

                                @suslucoder How i have second column, and i want to add something to my child object.
                                Should i create new tree widget items?

                                J Online
                                J Online
                                jsulm
                                Lifetime Qt Champion
                                wrote on 29 Mar 2021, 08:01 last edited by
                                #15

                                @suslucoder Change treeWidget->setColumnCount(1); to treeWidget->setColumnCount(2); And then add your second column in each item:

                                imu_items.append(new QTreeWidgetItem(static_cast<QTreeWidget *>(nullptr), QStringList(imuList.at(i) << "SECOND_COLUMN_VALUE")));
                                

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

                                D 1 Reply Last reply 29 Mar 2021, 08:15
                                0
                                • J jsulm
                                  29 Mar 2021, 08:01

                                  @suslucoder Change treeWidget->setColumnCount(1); to treeWidget->setColumnCount(2); And then add your second column in each item:

                                  imu_items.append(new QTreeWidgetItem(static_cast<QTreeWidget *>(nullptr), QStringList(imuList.at(i) << "SECOND_COLUMN_VALUE")));
                                  
                                  D Offline
                                  D Offline
                                  deleted286
                                  wrote on 29 Mar 2021, 08:15 last edited by
                                  #16

                                  @jsulm my second column value is coming from a list again, does the code work that you suggest me in that case

                                  D 1 Reply Last reply 29 Mar 2021, 08:16
                                  0
                                  • D deleted286
                                    29 Mar 2021, 08:15

                                    @jsulm my second column value is coming from a list again, does the code work that you suggest me in that case

                                    D Offline
                                    D Offline
                                    deleted286
                                    wrote on 29 Mar 2021, 08:16 last edited by
                                    #17

                                    @suslucoder yes it works. thank you

                                    1 Reply Last reply
                                    0

                                    5/17

                                    29 Mar 2021, 06:17

                                    topic:navigator.unread, 12
                                    • Login

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