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. Newbie and the QTreeWidget... Not getting what I think should be ?
QtWS25 Last Chance

Newbie and the QTreeWidget... Not getting what I think should be ?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 924 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.
  • G Offline
    G Offline
    grpace
    wrote on last edited by
    #1

    This is likely something simple that I'm overlooking. I'm not having success getting a child QTreeWidgetItem to display as a child item.

    I have two functions: one for adding a parent item and one for adding a child item:

    void window_Main::treeAddParent(QString name) {
        QTreeWidgetItem *treeitemParent = new QTreeWidgetItem(ui->treeWidget);
    
        treeitemParent->setText(0, name);
        ui->treeWidget->addTopLevelItem(treeitemParent);
    }
    
    void window_Main::treeAddChild(QTreeWidgetItem treeitemParent, QString name) {
        QTreeWidgetItem *treeitemChild = new QTreeWidgetItem(treeitemParent);
    
        treeitemChild->setText(0, name);
        treeitemParent.addChild(treeitemChild);
    }
    

    I'm calling these functions in this generic manner (strTemp is a QString containing their names):

    strTemp = "parent";
    treeitemParent.setText(0, strTemp);
    treeAddParent(strTemp);
    
    strTemp="child";
    treeitemChild.setText(0, strTemp);
    treeAddChild(treeitemParent, strTemp);
    

    The Parent item displays... The Child item does not.
    I'm not getting errors, just no child element displaying.

    Could someone explain what I'm missing ?
    Thanks !

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      2 mistakes:

      • treeitemParent.setText(0, strTemp); is not the same as QTreeWidgetItem *treeitemParent = new QTreeWidgetItem(ui->treeWidget); so the first treeitemParent is actually an item with no relation to your widget
      • You are passing the parent by value, that will create a copy.

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      1
      • G Offline
        G Offline
        grpace
        wrote on last edited by
        #3

        Yes, I understand your first point... They are completely separate, only with the same name to try to avoid confusing myself. The

        QTreeWidgetItem *treeitemParent = new QTreeWidgetItem(ui->treeWidget)
        

        is local within the AddParent function only.

        I have tried prototyping the AddChild function and using it like this:

        void treeAddChild(QTreeWidgetItem *parent, QString name);
        

        But so far I haven't found a way to get a pointer to the parent QTreeWidgetItem without errors. Again, I'm brand new to Qt.

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4
          QTreeWidgetItem* window_Main::treeAddParent(QString name) {
              QTreeWidgetItem *treeitemParent = new QTreeWidgetItem(ui->treeWidget);
          
              treeitemParent->setText(0, name);
              ui->treeWidget->addTopLevelItem(treeitemParent);
          return treeitemParent;
          }
          
          void window_Main::treeAddChild(QTreeWidgetItem* treeitemParent, QString name) {
              QTreeWidgetItem *treeitemChild = new QTreeWidgetItem(treeitemParent);
          
              treeitemChild->setText(0, name);
              treeitemParent->addChild(treeitemChild);
          }
          
          strTemp = "parent";
          QTreeWidgetItem * treeitemParent = treeAddParent(strTemp);
          strTemp="child";
          treeAddChild(treeitemParent, strTemp);
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          3
          • G Offline
            G Offline
            grpace
            wrote on last edited by
            #5

            Thank You !

            It occurred to me to try returning the created object from the AddParent function, and was hacking at it. I'll try your suggestion. Will let you know.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              grpace
              wrote on last edited by
              #6

              @VRonin ...
              Worked like a charm !!

              Many of the video tutorials on YouTube show guys adding the child nodes inside the function that added the parent nodes (example here, at around 9:06 in the video).

              I knew that wouldn't work for my needs. My opinion is that example is not good design. For at least the last 3 evenings, I've looked for a better approach, but didn't find one. Qt docs really don't explain it, either.

              Thank you so much for your help !
              You are the man !!
              :)

              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