how to add children to the QTree Widget


  • Qt Champions 2017

    @ManiRon
    Ok.
    Nothing odd then :)
    Then you just need to keep aaaa pointer around and
    eeee so you can add child to that.



  • @mrjj sir i tried the way u mentioned but i got like this 1_1536636757572_4.jpg 0_1536636757570_3.jpg

    my code:

    QList<QTreeWidgetItem*>m_Data;
    m_Data.append(new QTreeWidgetItem);

    QString Data = 0;
    for(int i=1;i<3;i++)
    {
    Data = QString("SubItem"+QString::number(i));
    QTreeWidgetItem* item1 =new QTreeWidgetItem(ui->treeWidget);
    item1->setText(0,"data1");
    m_Data.append(item1);
    item1->setText(0,Data.toLatin1().constData());
    m_Data.append(item1);
    item->addChild(item1);
    item1->addChildren(m_Data);
    }


  • Qt Champions 2017

    @ManiRon
    hi
    seems like blank texts. also i dont see u new item.??
    anyway, that loop looks so messy and hard to read
    if you use functions, it can look like this

    QTreeWidgetItem * addTreeRoot(QTreeWidget* treeWidget, QString name, QString description) {
        QTreeWidgetItem *treeItem = new QTreeWidgetItem(treeWidget);
        treeItem->setText(0, name);
        treeItem->setText(1, description);
        return treeItem;
    }
    
    QTreeWidgetItem * addTreeChild(QTreeWidgetItem *parent,QString name, QString description) {
        QTreeWidgetItem *treeItem = new QTreeWidgetItem();
        treeItem->setText(0, name);
        treeItem->setText(1, description);
        parent->addChild(treeItem);
        return treeItem;
    }
    --
    
    QTreeWidgetItem* root=addTreeRoot(ui->treeWidget,"ROOT","text");
    addTreeChild(root,"bbbb","text");
    addTreeChild(root,"ccc","text");
    addTreeChild(root,"dddd","text");
    QTreeWidgetItem* last=addTreeChild(root,"eeee","text");
    
    addTreeChild(last,"eeeee","text"); // notice last as parent
    
    
    

    alt text



  • @mrjj i am not getting sir,
    how i will declare the these functions?


  • Qt Champions 2017

    @ManiRon
    hi
    Just paste them into cpp file. they are just free functions.

    alt text



  • @mrjj errors came

    0_1536658393514_5.jpg



  • @ManiRon
    Look at your line #100. What in the world is the -- there?? You really ought be able to do this sort of thing for yourself....



  • @JonB I removed it sir, Same error coming



  • @ManiRon
    So after removing the -- is still warns on line #102 with specific message "expected primary-expression before "*" token", that's what you're saying, right?



  • @JonB NO sir other than that all error are there



  • @ManiRon
    Great, so I tell you one thing that is wrong, and you say it does not change the errors when in fact it does, and I have to guess....

    Look at the very first error message line. It tells you you are still inside function on_pushButton_clicked(). Do you show us that function? No. There is nothing (else) wrong with what you have pasted, but you haven't pasted it in the correct place. Nobody is going to be able to help you on that with what you show. Good luck, I'm out.



  • @JonB

    The whole code

    void MainWindow::on_pushButton_clicked()
    {
    // 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)
    
     QList<QTreeWidgetItem*>m_Data;
     m_Data.append(new QTreeWidgetItem);
    

    QString Data = 0;
    QTreeWidgetItem* item1 =new QTreeWidgetItem(ui->treeWidget);
    for(int i=1;i<3;i++)
    {

       Data = QString("SubItem"+QString::number(i));
    
       item1->setText(0,"data1");
       m_Data.append(item1);
       item1->setText(0,Data.toLatin1().constData());
       m_Data.append(item1);
    
    
    }
    

    item1->addChildren(m_Data);
    item->addChild(item1);

    QTreeWidgetItem * addTreeRoot(QTreeWidget* treeWidget, QString name,QString description)
    {
         QTreeWidgetItem *treeItem = new QTreeWidgetItem(treeWidget);
         treeItem->setText(0, name);
         treeItem->setText(1, description);
         return treeItem;
    }
    
    QTreeWidgetItem * addTreeChild(QTreeWidgetItem *parent,QString name,QString description)
    {
         QTreeWidgetItem *treeItem = new QTreeWidgetItem();
         treeItem->setText(0, name);
         treeItem->setText(1, description);
         parent->addChild(treeItem);
         return treeItem;
    }
    QTreeWidgetItem* root=addTreeRoot(ui->treeWidget,"ROOT","text");
    addTreeChild(root,"bbbb","text");
    addTreeChild(root,"ccc","text");
    addTreeChild(root,"dddd","text");
    QTreeWidgetItem* last=addTreeChild(root,"eeee","text");
    
    addTreeChild(last,"eeeee","text"); // notice last as parent
    

    }


  • Qt Champions 2017

    @ManiRon
    did u put the full functions INSIDE void MainWindow::on_pushButton_clicked() ????
    it sure seems so. And that is plain wrong.
    Should be OVER void MainWindow::on_pushButton_clicked()
    and never, ever inside.
    Look at the picture again. :)



  • @mrjj

    I put it inside void MainWindow::on_pushButton_clicked() first . Now i removed it and pasted it over that still that error remains


  • Qt Champions 2017

    @ManiRon
    please show the exact error



  • @mrjj errors solved



  • @ManiRon Thanks sir its working fine


  • Qt Champions 2017

    @ManiRon
    ok super :)
    Hard birth.
    Using functions makes it easier to read and hopefully also easier for you
    to create other tree structures.



  • @mrjj yes sir sure



  • @mrjj Working Code

    QTreeWidgetItem * addTreeRoot(QTreeWidget* treeWidget, QString name,QString description)
    {
    QTreeWidgetItem *treeItem = new QTreeWidgetItem(treeWidget);
    treeItem->setText(0, name);
    treeItem->setText(1, description);
    return treeItem;
    }

    QTreeWidgetItem * addTreeChild(QTreeWidgetItem *parent,QString name,QString description)
    {
    QTreeWidgetItem *treeItem = new QTreeWidgetItem();
    treeItem->setText(0, name);
    treeItem->setText(1, description);
    parent->addChild(treeItem);
    return treeItem;
    }

    void MainWindow::on_pushButton_clicked()
    {

    QTreeWidgetItem* root= addTreeRoot(ui->treeWidget,"ROOT","text");
    

    addTreeChild(root,"bbbb","text");
    addTreeChild(root,"ccc","text");
    addTreeChild(root,"dddd","text");
    QTreeWidgetItem* last = addTreeChild(root,"eeee","text");

    addTreeChild(last,"eeeee","text"); // notice last as parent
    

    }


Log in to reply
 

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