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



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



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


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



  • 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);
    


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



  • @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 !!
    :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.