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.
  • 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

                        16/17

                        29 Mar 2021, 08:15

                        • Login

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