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. [SOLVED] Cannot add child items in Root Tree entries
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Cannot add child items in Root Tree entries

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

    Consider the following Addroot and Addchild functions of a QTreeWidget object:

    @void Dialog::AddRoot(QString name, QString Description){

            QTreeWidgetItem *itm = new QTreeWidgetItem(ui->treeWidget);
            itm->setText(0,name);
            itm->setText(1,Description);
            ui->treeWidget->addTopLevelItem(itm);
            AddChild(itm,"One", "Hello");
            AddChild(itm,"Two", "World");
    

    }

    void Dialog::AddChild(QTreeWidgetItem *parent, QString name, QString Description){

            QTreeWidgetItem *itm = new QTreeWidgetItem(ui->treeWidget);
            itm->setText(0,name);
            itm->setText(1,Description);
            ui->treeWidget->addTopLevelItem(itm);
            parent->addChild(itm);
    

    }
    @

    Running this tree view still shows every entry as a root and not a child, no matter what.

    According to the same online tutorial, to fix this, the author merely replaces
    @QTreeWidgetItem *itm = new QTreeWidgetItem(ui->treeWidget);@ with @QTreeWidgetItem *itm = new QTreeWidgetItem(ui->treeWidget);@ (Line 13) and it works. I tried the same identical step and it still doesnt fix the problem of only roots showing. I think it has to do with Qt version, is this true? Did the recent releases change the way Tree's are modelled?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      In both Add functions, you do a ui->treeWidget->addTopLevelItem.

      Shouldn't AddChild be

      @
      QTreeWidgetItem *itm = new QTreeWidgetItem(parent);
      itm->setText(0,name);
      itm->setText(1,Description);
      @

      ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • B Offline
        B Offline
        bhamuryen
        wrote on last edited by
        #3

        Hi,

        line 13 should be
        @ QTreeWidgetItem *itm = new QTreeWidgetItem();@
        or
        @ QTreeWidgetItem *itm = new QTreeWidgetItem;@
        not
        @ QTreeWidgetItem *itm = new QTreeWidgetItem(ui->treeWidget);@

        and you need to remove line 16.

        so this code must be like this
        @
        void Dialog::AddChild(QTreeWidgetItem *parent, QString name, QString Description){

                QTreeWidgetItem *itm = new QTreeWidgetItem();
                itm->setText(0,name);
                itm->setText(1,Description);
                parent->addChild(itm);
        

        }@

        c++ Software Developer

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DeanQt
          wrote on last edited by
          #4

          Aha, it works when removing line 16.

          Thanks guys.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            You're welcome !

            Don't forget to update the thread's title to solved so other forum users may know that a solution has been found.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            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