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 add children to the QTree Widget
Qt 6.11 is out! See what's new in the release blog

how to add children to the QTree Widget

Scheduled Pinned Locked Moved Solved General and Desktop
67 Posts 3 Posters 28.4k Views 1 Watching
  • 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.
  • ManiRonM Offline
    ManiRonM Offline
    ManiRon
    wrote on last edited by
    #1

    I am trying to add children to the QTree widget . but i am facing some problems.

    My code:

    // Create new item (top level item)
    QTreeWidgetItem *topLevelItem = new QTreeWidgetItem(ui->treeWidget);

    // Set text for item
    topLevelItem->setText(0,"Item");
    
    // Add it on our tree as the top item.
    ui->treeWidget->addTopLevelItem(topLevelItem);
    
    
    // Create new item and add as child item
    

    QTreeWidgetItem *item=new QTreeWidgetItem(topLevelItem);
    // Set text for item
    //item->setText(0,"SubItem");

    QList<QString>m_Data;
    
    for(int i=1;i<3;i++)
    {
        QString Data = QString("SubItem"+QString::number(i));
        m_Data.append(Data);
    }
    
    item->addChildren(m_Data);
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      You are not really telling what kind of problems :)
      but this tutorial explain usage pretty well in my opinion

      https://www.bogotobogo.com/Qt/Qt5_QTreeWidget.php

      The main idea is either to add as a top level item or
      add to other parent to build a tree like structure.

      void Dialog::addTreeRoot(QString name, QString description)
      {
          // QTreeWidgetItem(QTreeWidget * parent, int type = Type)
          QTreeWidgetItem *treeItem = new QTreeWidgetItem(ui->treeWidget);
      
          // QTreeWidgetItem::setText(int column, const QString & text)
          treeItem->setText(0, name);
          treeItem->setText(1, description);
          addTreeChild(treeItem, name + "A", "Child_first");
          addTreeChild(treeItem, name + "B", "Child_second");
      }
      
      void Dialog::addTreeChild(QTreeWidgetItem *parent,
                        QString name, QString description)
      {
          // QTreeWidgetItem(QTreeWidget * parent, int type = Type)
          QTreeWidgetItem *treeItem = new QTreeWidgetItem();
      
          // QTreeWidgetItem::setText(int column, const QString & text)
          treeItem->setText(0, name);
          treeItem->setText(1, description);
      
          // QTreeWidgetItem::addChild(QTreeWidgetItem * child)
          parent->addChild(treeItem);
      }
      
      ManiRonM 1 Reply Last reply
      3
      • mrjjM mrjj

        Hi
        You are not really telling what kind of problems :)
        but this tutorial explain usage pretty well in my opinion

        https://www.bogotobogo.com/Qt/Qt5_QTreeWidget.php

        The main idea is either to add as a top level item or
        add to other parent to build a tree like structure.

        void Dialog::addTreeRoot(QString name, QString description)
        {
            // QTreeWidgetItem(QTreeWidget * parent, int type = Type)
            QTreeWidgetItem *treeItem = new QTreeWidgetItem(ui->treeWidget);
        
            // QTreeWidgetItem::setText(int column, const QString & text)
            treeItem->setText(0, name);
            treeItem->setText(1, description);
            addTreeChild(treeItem, name + "A", "Child_first");
            addTreeChild(treeItem, name + "B", "Child_second");
        }
        
        void Dialog::addTreeChild(QTreeWidgetItem *parent,
                          QString name, QString description)
        {
            // QTreeWidgetItem(QTreeWidget * parent, int type = Type)
            QTreeWidgetItem *treeItem = new QTreeWidgetItem();
        
            // QTreeWidgetItem::setText(int column, const QString & text)
            treeItem->setText(0, name);
            treeItem->setText(1, description);
        
            // QTreeWidgetItem::addChild(QTreeWidgetItem * child)
            parent->addChild(treeItem);
        }
        
        ManiRonM Offline
        ManiRonM Offline
        ManiRon
        wrote on last edited by ManiRon
        #3

        @mrjj
        Actually i want to add n number of children and child to that n number of children thats my question. But using the code.

        Example:
        I want to store the data in a Qlist and insert it in Qtree widget as children using the "addchildren"member function

        mrjjM 1 Reply Last reply
        0
        • ManiRonM ManiRon

          @mrjj
          Actually i want to add n number of children and child to that n number of children thats my question. But using the code.

          Example:
          I want to store the data in a Qlist and insert it in Qtree widget as children using the "addchildren"member function

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @ManiRon
          Ok, but there nothing magic to it.
          If you add it like
          QTreeWidgetItem *treeItem = new QTreeWidgetItem(ui->treeWidget);

          its a top level item
          if you use that item as parent, like

          QTreeWidgetItem *treeItem2 = new QTreeWidgetItem();
          treeItem ->addChild(treeItem2 );

          it becomes a child of that parent. and so on.

          so you simply use the last item as parent for next if u want it to go

          aaa
            bbbb
              cccc 
          
          ManiRonM 2 Replies Last reply
          1
          • mrjjM mrjj

            @ManiRon
            Ok, but there nothing magic to it.
            If you add it like
            QTreeWidgetItem *treeItem = new QTreeWidgetItem(ui->treeWidget);

            its a top level item
            if you use that item as parent, like

            QTreeWidgetItem *treeItem2 = new QTreeWidgetItem();
            treeItem ->addChild(treeItem2 );

            it becomes a child of that parent. and so on.

            so you simply use the last item as parent for next if u want it to go

            aaa
              bbbb
                cccc 
            
            ManiRonM Offline
            ManiRonM Offline
            ManiRon
            wrote on last edited by ManiRon
            #5

            @mrjj ok but if i want to add n number of children under top level item how should i do that.

            And i have another doubt .
            I am trying to see the data which i have inserted into my QList . I can see the size but i cannot see the data.

            Code:

            QList<QString> m_Data;
            for(int i=1;i<3;i++)
            {
            Data = QString("SubItem"+QString::number(i));
            m_Data.append(Data.toLatin1().constData());
            QString H = m_Data.at(i); //trying to see data
            qDebug()<<"Data"<< m_Data.at(i);
            }

            1 Reply Last reply
            0
            • mrjjM mrjj

              @ManiRon
              Ok, but there nothing magic to it.
              If you add it like
              QTreeWidgetItem *treeItem = new QTreeWidgetItem(ui->treeWidget);

              its a top level item
              if you use that item as parent, like

              QTreeWidgetItem *treeItem2 = new QTreeWidgetItem();
              treeItem ->addChild(treeItem2 );

              it becomes a child of that parent. and so on.

              so you simply use the last item as parent for next if u want it to go

              aaa
                bbbb
                  cccc 
              
              ManiRonM Offline
              ManiRonM Offline
              ManiRon
              wrote on last edited by
              #6

              @mrjj

              Sir i have a doubt

              mrjjM 1 Reply Last reply
              0
              • ManiRonM ManiRon

                @mrjj

                Sir i have a doubt

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @ManiRon
                Hi
                With ?

                ManiRonM 3 Replies Last reply
                1
                • mrjjM mrjj

                  @ManiRon
                  Hi
                  With ?

                  ManiRonM Offline
                  ManiRonM Offline
                  ManiRon
                  wrote on last edited by
                  #8

                  @mrjj

                  pro file

                  1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @ManiRon
                    Hi
                    With ?

                    ManiRonM Offline
                    ManiRonM Offline
                    ManiRon
                    wrote on last edited by
                    #9

                    @mrjj

                    Sir i am trying to add

                    uinx{
                    LIBS+= -L$$PWD/.../lib/linus/-lData
                    LIBS+= -L$$PWD/.../lib/Xenomai/-lData1 -lrtdm -lrt -lxenomai
                    }
                    win32
                    {
                    LIBS += path
                    }

                    I am adding the linux and xenomai library and trying to compile . but it throws error. As it detects the linux library and it couldnt find the xenomai library it throws error.
                    My question is i want to define all my library and based on the platform or OS i am running it should detect its particular library.
                    For example : linux, xenomai, windiows. These are the platforms i am running the application. Why i ask this is because every time i have to add the corresponding library and compile it and the run the application . So is there any solution for this?

                    mrjjM 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      @ManiRon
                      Hi
                      With ?

                      ManiRonM Offline
                      ManiRonM Offline
                      ManiRon
                      wrote on last edited by
                      #10

                      @mrjj

                      One of the person from this forum told me to use "unix" and "win32". But i asked for linux,xenomai what should i define, he told to use "unix" itself. But i have separate library for linux and xenomai. but i want to add both is there any way?

                      1 Reply Last reply
                      0
                      • ManiRonM ManiRon

                        @mrjj

                        Sir i am trying to add

                        uinx{
                        LIBS+= -L$$PWD/.../lib/linus/-lData
                        LIBS+= -L$$PWD/.../lib/Xenomai/-lData1 -lrtdm -lrt -lxenomai
                        }
                        win32
                        {
                        LIBS += path
                        }

                        I am adding the linux and xenomai library and trying to compile . but it throws error. As it detects the linux library and it couldnt find the xenomai library it throws error.
                        My question is i want to define all my library and based on the platform or OS i am running it should detect its particular library.
                        For example : linux, xenomai, windiows. These are the platforms i am running the application. Why i ask this is because every time i have to add the corresponding library and compile it and the run the application . So is there any solution for this?

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @ManiRon

                        Hi it seems ok what you did
                        except you missspelled unix (uinx)
                        and it sometimes a bit funny with scopes so make sure t to
                        unix {
                        }
                        and not
                        unix
                        {
                        }
                        http://doc.qt.io/qt-5/qmake-advanced-usage.html
                        http://doc.qt.io/qt-5/third-party-libraries.html

                        ManiRonM 1 Reply Last reply
                        1
                        • mrjjM mrjj

                          @ManiRon

                          Hi it seems ok what you did
                          except you missspelled unix (uinx)
                          and it sometimes a bit funny with scopes so make sure t to
                          unix {
                          }
                          and not
                          unix
                          {
                          }
                          http://doc.qt.io/qt-5/qmake-advanced-usage.html
                          http://doc.qt.io/qt-5/third-party-libraries.html

                          ManiRonM Offline
                          ManiRonM Offline
                          ManiRon
                          wrote on last edited by
                          #12

                          @mrjj

                          Ok Sir,

                          But my doubt is i am able to add the library.
                          But i am adding another library that supports xenomai. while running the application in linux OS it goes to the unix{
                          LIBS+= -L$$PWD/.../lib/linus/-lData
                          LIBS+= -L$$PWD/.../lib/Xenomai/-lData1 -lrtdm -lrt -lxenomai
                          }
                          part and searches for the library which is my problem, I want the application to detect the library based on the platform i run. As one person from Qt forum said unix is common for xenomai, linux. So is there any possibility i can make my application to run that particular library based on OS platform?

                          mrjjM 1 Reply Last reply
                          0
                          • ManiRonM ManiRon

                            @mrjj

                            Ok Sir,

                            But my doubt is i am able to add the library.
                            But i am adding another library that supports xenomai. while running the application in linux OS it goes to the unix{
                            LIBS+= -L$$PWD/.../lib/linus/-lData
                            LIBS+= -L$$PWD/.../lib/Xenomai/-lData1 -lrtdm -lrt -lxenomai
                            }
                            part and searches for the library which is my problem, I want the application to detect the library based on the platform i run. As one person from Qt forum said unix is common for xenomai, linux. So is there any possibility i can make my application to run that particular library based on OS platform?

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by mrjj
                            #13

                            @ManiRon
                            But is unix not linux for you ?
                            You have a unix build and a linux build?

                            I would assume you can do it right with

                            
                            win32 {
                            ..
                            }
                            
                            unix {
                              ..
                            }
                            
                            ManiRonM 2 Replies Last reply
                            1
                            • mrjjM mrjj

                              @ManiRon
                              But is unix not linux for you ?
                              You have a unix build and a linux build?

                              I would assume you can do it right with

                              
                              win32 {
                              ..
                              }
                              
                              unix {
                                ..
                              }
                              
                              ManiRonM Offline
                              ManiRonM Offline
                              ManiRon
                              wrote on last edited by
                              #14

                              @mrjj

                              its linux sir,

                              but for xenomai also it is unix i think so
                              unix{
                              LIBS+= -L$$PWD/.../lib/linus/-lData
                              LIBS+= -L$$PWD/.../lib/Xenomai/-lData1 -lrtdm -lrt -lxenomai
                              }

                              this is my problem sir,

                              I am want to run library if its linux i want linux library and if its xenomai i want xenomai library to run

                              mrjjM 1 Reply Last reply
                              0
                              • mrjjM mrjj

                                @ManiRon
                                But is unix not linux for you ?
                                You have a unix build and a linux build?

                                I would assume you can do it right with

                                
                                win32 {
                                ..
                                }
                                
                                unix {
                                  ..
                                }
                                
                                ManiRonM Offline
                                ManiRonM Offline
                                ManiRon
                                wrote on last edited by ManiRon
                                #15

                                @mrjj

                                ya i did this but for windows it works fine, and in unix if i define only one library that the platform i am running it works fine.

                                but every time i want to add that particular library compile and run the application

                                1 Reply Last reply
                                0
                                • ManiRonM ManiRon

                                  @mrjj

                                  its linux sir,

                                  but for xenomai also it is unix i think so
                                  unix{
                                  LIBS+= -L$$PWD/.../lib/linus/-lData
                                  LIBS+= -L$$PWD/.../lib/Xenomai/-lData1 -lrtdm -lrt -lxenomai
                                  }

                                  this is my problem sir,

                                  I am want to run library if its linux i want linux library and if its xenomai i want xenomai library to run

                                  mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by mrjj
                                  #16

                                  Hi
                                  Now i understand.
                                  For qmake both plain linux and xenomai will be the same
                                  as far as i know. (unix)

                                  you can call .sh script and things like that from the pro file
                                  so maybe you can detect your self if linux or xenomai ?
                                  http://doc.qt.io/archives/qt-4.8/qmake-function-reference.html#system-command

                                  Check what

                                  UNAME = $$system(uname -s)
                                  contains( UNAME, [lL]inux ):message( This looks like Linux ($$UNAME) to me )
                                  

                                  will output

                                  also did u try what been suggested in
                                  https://forum.qt.io/topic/94393/how-to-add-macro-for-the-libs-in-the-pro-file/9

                                  ManiRonM 2 Replies Last reply
                                  1
                                  • mrjjM mrjj

                                    Hi
                                    Now i understand.
                                    For qmake both plain linux and xenomai will be the same
                                    as far as i know. (unix)

                                    you can call .sh script and things like that from the pro file
                                    so maybe you can detect your self if linux or xenomai ?
                                    http://doc.qt.io/archives/qt-4.8/qmake-function-reference.html#system-command

                                    Check what

                                    UNAME = $$system(uname -s)
                                    contains( UNAME, [lL]inux ):message( This looks like Linux ($$UNAME) to me )
                                    

                                    will output

                                    also did u try what been suggested in
                                    https://forum.qt.io/topic/94393/how-to-add-macro-for-the-libs-in-the-pro-file/9

                                    ManiRonM Offline
                                    ManiRonM Offline
                                    ManiRon
                                    wrote on last edited by
                                    #17

                                    @mrjj said in how to add children to the QTree Widget:

                                    UNAME = $$system(uname -s)
                                    contains( UNAME, [lL]inux ):message( This looks like Linux ($$UNAME) to me )

                                    So thsi is for linux?
                                    UNAME = $$system(uname -s)
                                    contains( UNAME, [lL]inux ):message( This looks like Linux ($$UNAME) to me )

                                    If i want to check for xenomai what should i do sir?

                                    1 Reply Last reply
                                    0
                                    • mrjjM mrjj

                                      Hi
                                      Now i understand.
                                      For qmake both plain linux and xenomai will be the same
                                      as far as i know. (unix)

                                      you can call .sh script and things like that from the pro file
                                      so maybe you can detect your self if linux or xenomai ?
                                      http://doc.qt.io/archives/qt-4.8/qmake-function-reference.html#system-command

                                      Check what

                                      UNAME = $$system(uname -s)
                                      contains( UNAME, [lL]inux ):message( This looks like Linux ($$UNAME) to me )
                                      

                                      will output

                                      also did u try what been suggested in
                                      https://forum.qt.io/topic/94393/how-to-add-macro-for-the-libs-in-the-pro-file/9

                                      ManiRonM Offline
                                      ManiRonM Offline
                                      ManiRon
                                      wrote on last edited by
                                      #18

                                      @mrjj

                                      Is there any way sir?

                                      mrjjM 1 Reply Last reply
                                      0
                                      • ManiRonM ManiRon

                                        @mrjj

                                        Is there any way sir?

                                        mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on last edited by mrjj
                                        #19

                                        @ManiRon
                                        Hi
                                        Yes you can do it.
                                        But you need to find out how. I dont have xenomai
                                        installed so i cant say how we can spot the difference between plain linux and
                                        xenomai
                                        so try
                                        UNAME = $$system(uname -s)
                                        on both systems and see what it types.
                                        If different, you can use that as a flag

                                        ManiRonM 1 Reply Last reply
                                        1
                                        • mrjjM mrjj

                                          @ManiRon
                                          Hi
                                          Yes you can do it.
                                          But you need to find out how. I dont have xenomai
                                          installed so i cant say how we can spot the difference between plain linux and
                                          xenomai
                                          so try
                                          UNAME = $$system(uname -s)
                                          on both systems and see what it types.
                                          If different, you can use that as a flag

                                          ManiRonM Offline
                                          ManiRonM Offline
                                          ManiRon
                                          wrote on last edited by
                                          #20

                                          @mrjj

                                          contains( UNAME, [lL]inux ):message( This looks like Linux ($$UNAME) to me )

                                          This message where it will be printed sir?

                                          ManiRonM 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