Important: Please read the Qt Code of Conduct -

Failing to add topleveitem to QTreeWidget in QtCreator

  • I am facing an weird problem in QT programming using QT Creator.

    Basically I have a class of following structure which is a Tree apparently.

    class TestResult
        QString m_testID;
        QTreeWidgetItem* m_widgetItem;
        QList<TestResult*> m_Children;

    During developing the object of the tree (using a different method) I have handled the QTreeWidgetItem members also (i.e. Initialized it using new, added QTreeWidgetItem members of the children (m_Children) of the TestResultobject to it etc.)

    Now when I trying to add the QTreeWidgetItem memeber of root TestResultobject as a top level item of a QTreeWidget, it isn't getting added. I am doing this as following.

    TestResult *m_TestResultTree = new TestResult();
    // This following method call perfectly develop the tree. It populate QTreeWidgetItem members also to make a corresponding tree-like straucture.
    // I have a form whose object is uiMainWin and it contains a QTreeWidget namely treeResults  

    But, the QTreeWidgetItem is not getting added to the QTreeWidget. The call topLevelItemCount to the QTreeWidget simply returns 0.

    Can someone point out what I am doing wrong here ? Am I missing some property of QTreeWidget here ? Thanks in advance.

  • Moderators

    @surajeet Can you show us some more code ?

  • Moderators

    @surajeet Have you called setColumnCount on your tree widget yet? Nothing will show up in the tree if you haven't set your column count.

    Also the way you have made the TestResult class is the job of the tree itself. You don't need to track each widgetItem since the QTreeWidget already does that for you. You can even set custom user data, in your case m_testID in the tree items.

    You have an entire layer of obfuscation you really don't need. It's harder to use and harder to support later on.

    Also can you show us the code for uiMainWin? I have a feeling your tree isn't set up properly or you are using 2 different trees.

Log in to reply