1

I am trying to implement QTreeWidget in my application. I am having problem in adding items in the QTreeWidget. The code below only display the number of columns in the tree (see the image below) but not the items that I have added.

enter image description here

Could anybody tell me what am I doing wrong with the following code?

    ui->treeWidget_BE->setColumnCount(2);

    QTreeWidgetItem *itemHW = new QTreeWidgetItem();
    itemHW->setText(0,"Hello");
    itemHW->setText(1,"World");
    ui->treeWidget_BE->addTopLevelItem(itemHW);

    QTreeWidgetItem *item = new QTreeWidgetItem();
    item->setText(0,"toto");
    item->setText(0,"jojo");
    itemHW->addChild(item);;

I have also tried the first 4 lines with the following code

    QTreeWidgetItem *itemHW = new QTreeWidgetItem(ui->treeWidget_BE);
    itemHW->setText(0,"Hello");
    itemHW->setText(1,"World");

Thank you

PS: I am using Qt 4.8.3, Qt Designer and Visual Studio 2008

Community
  • 1
  • 1
user1703942
  • 317
  • 3
  • 15
  • The code you have shown works, apart from the error in the creation child item which overwrites "toto" with "jojo" because you haven't changed the column. Because the code as shown works, the problem is elsewhere. https://stackoverflow.com/help/mcve –  Feb 28 '19 at 19:34
  • Where is the code that adds the item to the QTreeWidget? (see: [https://doc.qt.io/archives/qt-4.8/qtreewidget.html]) – jwernerny Feb 28 '19 at 20:55
  • @jwernerny, it is the line `ui->treeWidget_BE->addTopLevelItem(itemHW);` –  Feb 28 '19 at 23:27
  • @replete Okay, I missed it the first time. – jwernerny Mar 01 '19 at 14:23
  • Actually i was modifying the existing ui file. I dont know how and why, but after reverting all the modifications in the ui files, and then adding again the qtreeWidget, the above code started to work. i do not know the reason at all. – user1703942 Mar 08 '19 at 09:23

0 Answers0