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
Forum Updated to NodeBB v4.3 + New Features

How to create a tree widget from QList

Scheduled Pinned Locked Moved Solved General and Desktop
qt5tree widget
17 Posts 2 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 deleted286

    @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));
    
    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on 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
    0
    • jsulmJ jsulm

      @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 last edited by
      #9

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

      jsulmJ 1 Reply Last reply
      0
      • D deleted286

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

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on 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
        0
        • jsulmJ jsulm

          @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 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);
          
          jsulmJ 1 Reply Last reply
          0
          • D deleted286

            @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);
            
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on 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
            0
            • jsulmJ jsulm

              @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 last edited by
              #13

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

              D 1 Reply Last reply
              0
              • D deleted286

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

                D Offline
                D Offline
                deleted286
                wrote on 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?

                jsulmJ 1 Reply Last reply
                0
                • D deleted286

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

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 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
                  0
                  • jsulmJ jsulm

                    @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 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
                    0
                    • D deleted286

                      @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 last edited by
                      #17

                      @suslucoder yes it works. thank you

                      1 Reply Last reply
                      0

                      • Login

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