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?Thanks.
-
@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 http://doc.qt.io/qt-5/qstandarditemmodel.html for your model and http://doc.qt.io/qt-5/qtreeview.html for your view, or use http://doc.qt.io/qt-5/qtreewidget.html 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-simplerQTreeWidget
.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 (https://forum.qt.io/topic/83616/using-qsqlquerymodel-instead-of-qstandarditemmodel/3), 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.