Important: Please read the Qt Code of Conduct -

QTableView, QStandardItemModel and underlying data in a QList

  • Hi Qt fans,

    I am new to QT. I want to use my own objects in my model, and present them in a QTableView. But I cannot find out how to connect underlying data with my model / view (especially vice versa). Maybe I am not that far away, but after weeks of reading about model/view-things, reading here and Stackoverflow, I am totally confused. So I dare to present my solution here, and be curious about yours.

    I am using QStandardItem model, and I want at first an easy solution. If possible, no QAbstractItemModel, reimplementing or so.
    I have objects of my struct "person" stored in a QList. I just want that a person (and its attributes) is represented by a row in that table. Even when you sort all person by an attribute, and then you click on that row, you get data back from the underyling object in the Qlist. Not only item data, I want to get back whole object, or data that is not shown in tableview!
    Persons are organised in an object of class "club". Club is a widget containing my QTableView. A person has three members: an ID, name and family name.

    Here is my struct person:

    // person.h
    struct person 
        int id;
        QString name;
        QString family;

    Here is my club:

    // here is my external list for persons (because needed by other classes)
    QList<person> list_of_people;
    // Constructor
    Club::Club(QWidget *parent) : QWidget(parent), uiclub(new Ui::Club)
        club_model = new QStandardItemModel();
        QStringList header;
        header << "ID" << "Name" << "Family Name";
    // function to create a person and add to list_of_people
    void Club::create_person_from_lineEdits()
       Person &newPerson = *(new Person());
       // fill data from LineEdits... = list_of_people.count(); = uiclub->lineEdit1.text;
    // Create Items from objects and append as a row to model
    void Club::show_person_in_tableView(Person &obj)
        QStandardItem *item_id = new QStandardItem(;
        QStandardItem *item_name = new QStandardItem(;
        QStandardItem *item_family = new QStandardItem(;
        QList<QStandardItem*> items;
    // Click, enter or whatever on an item in qtableview
    void Club::on_tableView_activated(const QModelIndex &index)
        qDebug() << "user clicks on index row" << index.row();

    Here you see the problem: index qtableview is only the same as index of list_of_people when I do not change sorting!
    But of course I want to sort, and I want to get back the underlying object via ID or so. Here in this example ID is an item, but what if not? Is it possible to include my QList directly into my model?
    Can I solve this with my comfortable QStandardItemModel? Do I have to use my own Abstract model? Do I have to use a ProxyModel? QObject_META? Didn't understand these latter solutions, to be honest :)

    I greatly appreciate your help! If you need something more, please let me know.


    PS: why struct? because my persons just represent raw data, and have no functions at all. Why qlist and not qvector? I changed that because I need access in the middle of my list_of_people with thousand persons, and somebody in internet said lists are better for that.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Either put everything in your QStandardItemModel and share it with other classes that need to access the information contained in it or implement a QAbstractTableModel that will return your struct elements per column.

    The rowCount implementation should return the size of your list.
    The columnCount implementation should return the number of members of your struct.

  • @SGaist Thank you for your quick reply!

    But if I put everything in my QStandarditemModel, how can I change my original instance of person in my Qlist: "list_of_people"?
    E.g. if I change an item (name) via my QTableView, how can I route it further to my original object? How can I connect my data with my model?

  • Lifetime Qt Champion

    That's why I wrote "put everything in your QStandardItemModel" and you would only use that as data source.

    Where else do you use your data structure ?

    On a side note, as static variable for that is not a good idea.

  • @SGaist: Thank you for your answer, I want to be more precise:

    Imagine that I have Qlist as my data source, with 100 Persons.
    I have two different qTableViews, first with all persons, and second with persons user can decide. Thatswhy I thought that it is good idea tio have one source of truth: my Qlist. First qtable reads all persons in. You can mark a person, and then (reference) of that object is transfered to that second qtableview. Addiotioonally to that, there will be more and dynamic amount of qtableviews for that. That approach would effectively reduce my amount of memory.
    I have to think about your approach, if it is possible. I don't know yet.
    I have an external variable there, not static variable. Reason is: I have several widget classes, and I want to share that qlist with all of them by their functions. Do you have a better approach? I tried to make it "friend" once, and use a free function, but due to an error I couldn't handle that. As a noob there are so many things to learn, and sources of errors are endless :)

  • Lifetime Qt Champion

    To fix the sorting issues, you could use
    and put the Index of the person to a UserRole so that
    you can use that to look up the person it represents.
    However, that is only valid if you don't add or remove persons
    while the view is active.

    However, im not sure that a QStandardItem model will make you very happy in the long run if you
    plan to allow the user to edit a Person and need to write back the changed data to the
    Persons list. For that, a QAbstractTableModel would be much more smooth.
    You could reuse the model from
    as its very close to what you have ( Contact vs Person)

  • Hi!

    Thank you very much for your help. I thought and I want to make little steps at first, so I changed my struct:

    // person.h
    struct person 
        int id;
        QStandardItem* name;
        QStandardItem* family;

    Now I can directly use my members and append them.
    I do not need write access, but to show my objects directly in QAbstractTableModel is indeed a sexy idea. I am trying around with QModelIndex, club_model->item(), club_model->itemData() and roles, and when I am done with that, maybe I'll try my own model.

    For now, my question is solved!

    Regards Gerhard