How to create a QML ListModel from a C++ structure ?
-
Hi.
I want to create a QML ListModel from a C++ structure.
There are more than 100 structure members.
I thought about adding them one by one to the QList and putting them in the ListModel,
but adding more than 100 members is a bit of a pain.Is there a way to do this that isn't too much trouble?
-
If you are using this structure in other code that you don't want to change. Then I would serialize and deserialize the list. It really depends upon how interactive you want it to be. Are you planning on having the changes update in the structure based upon actions by the user? In that case I would go for a model that allows things to be updated and immediately changed in the structure. If you want to just display and update the whole thing at once you can get by using the serialize method. QML ListModel's accept javascript structures:
{name: "year", value: 22}You can build this value as a QVariantList with each entry being QVariantMap. Then return QVariantList to QML. Or you can emit a QVariantMap for each member.
You won't be able to get around the tedious work this involves. No matter how you do this it will a lot of copy, paste, and edit.
If you are thinking of changing how you store this in your program you could store the data in a way that is easier to serialize.
-
You don't. You implement a QAbstractListModel in C++ and use that in a QML View.
https://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html
-
What does this structure look like? Is the structure itself repeated? Are you wanting each item to hold 100 properties? Or do you want to create a list that holds each property as a list item? Are the members the same or similar data types? Could they be combined into a list of similar types?
-
You don't. You implement a QAbstractListModel in C++ and use that in a QML View.
https://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html
-
What does this structure look like? Is the structure itself repeated? Are you wanting each item to hold 100 properties? Or do you want to create a list that holds each property as a list item? Are the members the same or similar data types? Could they be combined into a list of similar types?
@fcarney
Here's a sample structure.typedef struct test { unsigned char year unsigned char month unsigned char day unsigned char hour unsigned char minute unsigned char second unsigned short number unsigned long id //etc... }
There are more than 100 such various data members in a single structure
When I make a list, I'm going to use a type converter to convert it to QString, etc.
In the end, we want to have a list that can be displayed in a TableView or something similar. -
@fcarney
Here's a sample structure.typedef struct test { unsigned char year unsigned char month unsigned char day unsigned char hour unsigned char minute unsigned char second unsigned short number unsigned long id //etc... }
There are more than 100 such various data members in a single structure
When I make a list, I'm going to use a type converter to convert it to QString, etc.
In the end, we want to have a list that can be displayed in a TableView or something similar.@w-tkm said in How to create a QML ListModel from a C++ structure ?:
we want to have a list
A list of what? I see a structure, but I don't see what you want in your list? Do you have 1000 of these structures? Or do you want to see each member as a separate item in the list?
-
@w-tkm said in How to create a QML ListModel from a C++ structure ?:
we want to have a list
A list of what? I see a structure, but I don't see what you want in your list? Do you have 1000 of these structures? Or do you want to see each member as a separate item in the list?
-
If you are using this structure in other code that you don't want to change. Then I would serialize and deserialize the list. It really depends upon how interactive you want it to be. Are you planning on having the changes update in the structure based upon actions by the user? In that case I would go for a model that allows things to be updated and immediately changed in the structure. If you want to just display and update the whole thing at once you can get by using the serialize method. QML ListModel's accept javascript structures:
{name: "year", value: 22}You can build this value as a QVariantList with each entry being QVariantMap. Then return QVariantList to QML. Or you can emit a QVariantMap for each member.
You won't be able to get around the tedious work this involves. No matter how you do this it will a lot of copy, paste, and edit.
If you are thinking of changing how you store this in your program you could store the data in a way that is easier to serialize.