Important: Please read the Qt Code of Conduct -

Model View for a list and table

  • Hi,

    I am quite new to thema model-view and I want to use it in my project. I have a list, consisting of name, page, address, value columns (all strings). I want to show them in a table and I want to add write protect (checkbox), write value (string) columns in the table but not in data model. Further, I want to collect rows that write value columns has changed.
    All example projects are only for string tables or very basic things and I could not go further with those examples. Maybe my requirement is a little bit compilcated but how can I do that?


  • @kahlenberg
    Let's start with: are your data strings already in memory (or perhaps a text file), or are these rows & columns in an actual database?

  • Ok,
    Data is read from a text file as string and stored in a QVector (QVector<QStringList> *registers;). One row has 3 columns (name, page, address) as StringList.

  • @kahlenberg
    So as your starting point I would think you want for your model and for your view, or use and accept its inbuilt model.

    I would get that working before you come to add extra columns. In that light you may need the extra flexibility of QStandardItemModel + QTreeView rather than the initially-simpler QTreeWidget.

    IIRC, Qt is not that happy with attempts to have columns in the view which are not in the model. I was advised to use a third-party library when I originally asked about this (, but I haven't gotten around to implementing.

  • OK Thanks.
    Why shall I use QTreeView instead of QTableView?
    By the way, model is not restricted to have only three columns. I cad add also more columns with different datatype to model. For example I can make a struct and a QList for data:

    struct s_Register {
                QString name;
                quint16 page;
                quint16 address;
                quint32 currentValue;
                bool writeProtect;
                quint32 writeValue;

    With this struct we can generate a list: QList<s_Register> *Registers; and use it as data.

Log in to reply