Nominate our 2022 Qt Champions!

How to Choose between QtreeView(QAbstractItemModel) vs QTreeWidget

  • I have application in which currently the data displayed in is in multiple table format

    Table1 : header (Report1)
    column1 column2 column3
    A 3 4
    B 3 4
    C 3 4

    Table 2 : Report2
    column0 column1 column3
    E 3 3
    F 3 4
    G 3 4

    The New View has be in Tree format
    +Report1 (21)
    A (7)
    B (7)
    C (7)
    + Report2(21)
    E (7)
    G (7)

    What should be considered for New View QTreeView or QTreeWidget (with QAbstractItemModel) item

  • It all depends on your application. If you will be using the data stored in the model and not only displayed in the tree, you should use the QTreeView (And model). If you want to display data only, that is better to use the QTreeWidget

  • Currently the Table view is in model format . but the model stores data for table view but do coverting it into tree view will require another model creation

  • If I have a application that will can add upton maximum 500 elements IN QtreeWidget will it lead to performance issue in QtreeWidget

  • Lifetime Qt Champion

    What will they contain ? Will you be doing any processing on them ? Special visualization ?

    500 is not necessarily a huge amount of items.

  • They will contain string . The string , if select the row in QtreeWidget on left , then it should fill the QTableView at the right

  • QTreeWidget, QTableWidget, QListWidget are just convenience classes (for quick start of sorts. Documentation advises to use Model-View approach) which use Model-View architecture on low levels in any case. Meanwhile QTreeView-QAbstractItemModel approach gives you more flexibility during application developing despite it can look difficult first time.

    Let's consider an example like this: you displayed your information with QTreeWidget. Everything was looking good, but later your boss (or customer) asked you to show data as a table. You did similar steps for filling your newly created QTableWidget with data (QTreeWidget was deleted meanwhile so you had lost your previous results) and everything seems fine again. But later your boss asked you to show data both as a tree and as a table in different places of application. So you need to create tree widget from scratch again. Moreover you have duplicated data now so if it changes you need to be aware that you apply these changes to both widgets.

    With Model-View approach your actions for that scenario could be these: fill model with data and show it like tree, then just change QTreeView to QTableView without model refilling and then just create another QTreeView and connect it to the same model. In that case you need to modify data once in model, and all connected views (maybe one, maybe two, maybe ten) will know about it and modify displayed content. So use Model-View approach everywhere instead of convenience widgets if you want to maintain your applications without bloody tears :-)

  • I want to hide headers in QtreeWidget . Can someone guide me how to do it.

    Also I have a QtreeView with multiple parent items . Can some one guide me a sample example for the same . How to create a QtreeView with multiple parents

  • Lifetime Qt Champion

    QTreeWidget's detailed documentation shows how to add one top level item. Adding several uses the exact same technique.

    As for hiding the headers. Just call hide on both horizontal and vertical header views.

  • @Qt-Enthusiast said:

    Just wanted to clarify , I am asking for QtreeView with a custom model and multiple items can be as root node abd can have children

    --- B

    • C
      -- E

    here A and D are at same level and B C and children of A and E and F are children D

  • Lifetime Qt Champion

    Take a look at the documentation of QStandardItemModel

Log in to reply