Solved Many different items to show in QML GridView.
-
Hi!
I'm programming in Qt and QML about 2 years from now. It's great framework. Until now all I dealt with solving problems using Qt documentation (one of the best I know). But now I have problem and could find any good answer for it.
I'm writing my own IoT Hub for home, using Raspberry Pi and nrf24L01 radio transceiver. All logic is written in C++ and presentation in Qml. So I have Qlist with pointers to abstract class named "BaseDevice" and from this base class I have inherited new classes like TemperatureDevice, HumidityDevice, etc..
In Qml I used StackView with Page for every device category like "Temperature Devices" With GridView taking the model from C++ logic (standard model View with C++ and QML pattern).Now I seeking the solution to put all devices form C++ list and present them in one gridVeiw. I would like to have a main page with a view of all devices and sensors. But as far i know GridView or other "views" in QML could have only one model. I read about "Loader" as component creator in View's but with this object i don't know how to pass a device type to QML?
Has someone have similar problems?? Some good pattern to solving my problem would be great!
Best Regards!
-
hi @goro3 and welcome,
If I understand you correctly, you would like to assign different models to your gridview depending on specific cases ?
The easiest solution would be by using a JS-expression for your model property
GridView { width: 300; height: 200 model: { if(condition1) return model1 else return model2 } }
-
Hi @goro3,
As you already have a
BaseDevice
class inherited by all your device classes, you can probably use a single model ofBaseDevice
objects and then display them on QML according to their types using aLoader
. -
-
@goro3 said in Many different items to show in QML GridView.:
@j-hilk Jest, but in this case can i use two or three models at one time?
only one model will be active at only given time, but there's no limit on how many if/else (or a switch statement) you can have.
-
@goro3 said in Many different items to show in QML GridView.:
@Gojir4 It will be the best solution. But how could I check wat type this item is? In C++ i can use std::dynamic_cast to check inherited type, but how to do this in QML??
By adding a property in the base class which indicate the type ?
class BaseDevice: public QObject { Q_OBJECT Q_PROPERTY(int type READ type) //... }; //qml Repeater{ model: myModel Loader{ sourceComponent: { switch(type){ //Type provided by model data default: return unimplementedComponent case TempSensor: return tempSensorComponent case HumiditySensor: return humiditySensorComponent } } } }
-
Also you can set property as enum and decalre it as
Q_ENUM(enum_name)
; Then, register yor base type like:qmlRegisterUncreatableType<Base>("Test", 1, 0, "Base", "Base cannot be created at QML");
After this you can use your enum at QML like
Base.EnumValue
, instead of integer comparison. -
@Gojir4 , @IntruderExcluder Thank you for help. I will try this solution!