Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Many different items to show in QML GridView.
Forum Updated to NodeBB v4.3 + New Features

Many different items to show in QML GridView.

Scheduled Pinned Locked Moved Solved QML and Qt Quick
8 Posts 4 Posters 830 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GoRo3
    wrote on last edited by
    #1

    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!

    J.HilkJ Gojir4G 2 Replies Last reply
    0
    • G GoRo3

      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!

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      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
        }
      }
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      G 1 Reply Last reply
      0
      • G GoRo3

        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!

        Gojir4G Offline
        Gojir4G Offline
        Gojir4
        wrote on last edited by
        #3

        Hi @goro3,

        As you already have a BaseDevice class inherited by all your device classes, you can probably use a single model of BaseDevice objects and then display them on QML according to their types using a Loader.

        1 Reply Last reply
        0
        • J.HilkJ J.Hilk

          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
            }
          }
          
          G Offline
          G Offline
          GoRo3
          wrote on last edited by GoRo3
          #4

          @j-hilk Jest, but in this case can i use two or three models at one time?

          @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??

          J.HilkJ Gojir4G 2 Replies Last reply
          0
          • G GoRo3

            @j-hilk Jest, but in this case can i use two or three models at one time?

            @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??

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @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.


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            0
            • G GoRo3

              @j-hilk Jest, but in this case can i use two or three models at one time?

              @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??

              Gojir4G Offline
              Gojir4G Offline
              Gojir4
              wrote on last edited by
              #6

              @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
                              
                          }
                      }
                  }
              }
              
              1 Reply Last reply
              3
              • IntruderExcluderI Offline
                IntruderExcluderI Offline
                IntruderExcluder
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                2
                • G Offline
                  G Offline
                  GoRo3
                  wrote on last edited by
                  #8

                  @Gojir4 , @IntruderExcluder Thank you for help. I will try this solution!

                  1 Reply Last reply
                  1

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved