Important: Please read the Qt Code of Conduct -

[SOLVED] QTreeView Basic understanding

  • Hi

    I can't understand the QtreeView Model handling. Could someone please explain it with my example?

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)

    QList<int> sample;
    //11 is child of 10
    //13 is child of 12
    //build treeModel of QAbstractItemModel
    //change in TreeView??


    delete ui;

  • Qt Champions 2017

    Did you look at QStandardItemModel and QStandardItem to build your tree model ? I'm sure you will good examples in Qt Assistant and google. Also what is the explanation you are expecting ? Any change in model will be reflected in View. I just made sample which is very trivial.

    @QTreeView view;
    QStringListModel list1;
    QStringList list;


    Here whenever you change the list1, it should get reflected in view. Hope this helps.

  • i have various types of data(strings doubles and int) and i thought you need to specify the type in the model.

    when i change your example it doesnt work with integers!

  • Qt Champions 2017

    U can't set integers directly. There is no integer model like stringlistmodel. U need to list model with integers.

  • How this is exactly my question?

  • Lifetime Qt Champion


    By using your model's setData method

  • I dont know how to create the model this was my first question!

  • Qt Champions 2017

    I suggest you read the ModelView Programming in Qt Assistant. It clearly describes about different models, views. Try to understand how you can create different models. Qt Demo/Examples in your installation also contain many examples. Simple google hit also gave me many links with example. Enjoy model view programming.

  • Moderators

    Here is an official tutorial for model/view programming:

    Here is a simple example that uses QStandardItemModel with QTreeView to visualize integers:
    // Create model
    auto model = new QStandardItemModel;

    // Add data to model
    QList<QStandardItem*> list;
    for (int i = 0; i < 10; ++i)
    auto item = new QStandardItem;
    item->setData(i); // Set integer data
    list << item;

    // Create view to visualize the model
    auto view = new QTreeView;

    Note that there are many different models and many different views you can choose from. If you only want a list, you should use QListView instead of QTreeView.

    Good luck.

  • thank you this will help me

Log in to reply