QTableView and list of structs
-
I see https://doc.qt.io/qt-5/sql-presenting.html but is SQL. I search maximal simple model with list of structs.
-
Hi,
Create a subclass of QAbstractTableModel.
The row count being the length of your list
The column count the number of elements in your struct
In the data method return the various elements of your struct based on the column number. The struct itself being selected based on the row number. -
I have created simple model:
https://gist.github.com/borneq/89673fb3d8603b35117232bd9ae078e0
how to simply expand functionality , to user can:
have list of any struct
QList<structA> listA;
BTableModel model
model.setList(listA)I must use templates, like:
BTableModel<structA> model ? -
Hi
Even if you use template to let model accept any struct/type.
How will it know which member variables to to use for row/col values ?say we have
struct MyData {
string name;
int age;
}the model cannot know it should use name / age so
templates alone cannot make it very generic. -
Hi
yes that could work.
Option 3 would to expect the struct to have a certain interface.
Since ultimately any data from model is QVariant, you could make it a bit generic
withstruct MyData { string name; int age; QVariant getData(int col); }
and model would use getData to well get the data for a given col.
-
Next problem:
I have intereface:#pragma once #include <QVariant> class IVariantable { public: virtual QVariant getData(int col) = 0; };
and
struct Mystruct: public IVariantable
but I want list.
For QList<IVariantable> will be error. Must be QList<IVariantable*> and I must create new Mystruct and add to list? -
@AndrzejB
Hi
yes you showed
QList<structA> listA;
model.setList(listA)
so i assumed Qlist would be used.Well if you want polymorph function and use an abstract base class, yes
it has to be pointers. Its more flexible but care must be taken with
life span of the Mystruct in the list. -
https://gist.github.com/borneq/a953f6019c6694dfe66451adcfc29914
But is both: interface and template. Without template I can't do vector QVector<IVariantable*>
Is solution: use only list or vector <IVariantable*>, not vector of derived pointers -
@AndrzejB said in QTableView and list of structs:
use only list or vector <IVariantable*>, not vector of derived pointers
I'm not sure I understand: it's actually the same.