Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Connecting a dynamic QMap to Qml

Connecting a dynamic QMap to Qml

Scheduled Pinned Locked Moved General and Desktop
qmlqmapqqmllistpropert
8 Posts 2 Posters 8.4k Views
  • 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.
  • M Offline
    M Offline
    Maxim DC
    wrote on 6 Aug 2015, 08:38 last edited by Maxim DC 8 Jun 2015, 08:41
    #1

    Let's say i have a QMap<MyKey*, MyObject*> myobjects, it it possible to connect this to qml through QQmlListProperty<MyObject>

    PS: MyKey is not needed in qml, wich can be thrown away with myobjects.values() but that returns a pointer to a QList<MyObjects> but that throws an segmentation error when creating qqmllistproperty

    P 1 Reply Last reply 6 Aug 2015, 09:53
    0
    • M Maxim DC
      6 Aug 2015, 08:38

      Let's say i have a QMap<MyKey*, MyObject*> myobjects, it it possible to connect this to qml through QQmlListProperty<MyObject>

      PS: MyKey is not needed in qml, wich can be thrown away with myobjects.values() but that returns a pointer to a QList<MyObjects> but that throws an segmentation error when creating qqmllistproperty

      P Offline
      P Offline
      p3c0
      Moderators
      wrote on 6 Aug 2015, 09:53 last edited by p3c0 8 Jun 2015, 09:54
      #2

      @Maxim-DC If you don't need MyKey* in QML then you can use QVariantMap with custom objects as values. The custom object will need to be registered first using qmlRegisterType. Also since QVariantMap uses QVariant as 2nd parameter you will need to make that custom object QVariant. To sum up,
      For eg.

      //register MyClass before using
      qmlRegisterType<MyClass>("MyClass",1,0,"MyClass");
      
      MyClass *myclass = new MyClass;
      myclass->setText("MyClass Item"); //some method in MyClass
      
      //add it to QVariant
      QVariant var = QVariant::fromValue(myclass);
      
      //add it to QVariantMap
      QVariantMap map;
      map.insert("myclass",var);
      
      //set the map as context property
      engine.rootContext()->setContextProperty("mapObj",QVariant::fromValue(map));
      
      //access from QML
      import MyClass 1.0
      console.log(mapObj["myclass"])
      console.log(mapObj["myclass"].text()) //text() is a public slot in MyClass
      

      Also make sure MyClass inherits QObject

      157

      M 1 Reply Last reply 6 Aug 2015, 10:10
      0
      • P p3c0
        6 Aug 2015, 09:53

        @Maxim-DC If you don't need MyKey* in QML then you can use QVariantMap with custom objects as values. The custom object will need to be registered first using qmlRegisterType. Also since QVariantMap uses QVariant as 2nd parameter you will need to make that custom object QVariant. To sum up,
        For eg.

        //register MyClass before using
        qmlRegisterType<MyClass>("MyClass",1,0,"MyClass");
        
        MyClass *myclass = new MyClass;
        myclass->setText("MyClass Item"); //some method in MyClass
        
        //add it to QVariant
        QVariant var = QVariant::fromValue(myclass);
        
        //add it to QVariantMap
        QVariantMap map;
        map.insert("myclass",var);
        
        //set the map as context property
        engine.rootContext()->setContextProperty("mapObj",QVariant::fromValue(map));
        
        //access from QML
        import MyClass 1.0
        console.log(mapObj["myclass"])
        console.log(mapObj["myclass"].text()) //text() is a public slot in MyClass
        

        Also make sure MyClass inherits QObject

        M Offline
        M Offline
        Maxim DC
        wrote on 6 Aug 2015, 10:10 last edited by
        #3

        @p3c0 Ok i got it but then i have to specify the key in qml, thats not what i want, i want to make a listmodel of the values of the qmap

        P 1 Reply Last reply 6 Aug 2015, 10:19
        0
        • M Maxim DC
          6 Aug 2015, 10:10

          @p3c0 Ok i got it but then i have to specify the key in qml, thats not what i want, i want to make a listmodel of the values of the qmap

          P Offline
          P Offline
          p3c0
          Moderators
          wrote on 6 Aug 2015, 10:19 last edited by
          #4

          @Maxim-DC

          i want to make a listmodel of the values of the qmap

          What do you mean by that ?

          157

          M 1 Reply Last reply 6 Aug 2015, 11:14
          0
          • P p3c0
            6 Aug 2015, 10:19

            @Maxim-DC

            i want to make a listmodel of the values of the qmap

            What do you mean by that ?

            M Offline
            M Offline
            Maxim DC
            wrote on 6 Aug 2015, 11:14 last edited by Maxim DC 8 Jun 2015, 11:16
            #5

            @p3c0 For example the qmap consists of 3 objects, <mykey1*, myobject1*> <mykey2*, myobject2*> <mykey3*, myobject3*> i want to show a list with properties, textinitem1: myobject1.somevar, textinitem2: myobject2.somevar, ....

            P 2 Replies Last reply 6 Aug 2015, 11:57
            0
            • M Maxim DC
              6 Aug 2015, 11:14

              @p3c0 For example the qmap consists of 3 objects, <mykey1*, myobject1*> <mykey2*, myobject2*> <mykey3*, myobject3*> i want to show a list with properties, textinitem1: myobject1.somevar, textinitem2: myobject2.somevar, ....

              P Offline
              P Offline
              p3c0
              Moderators
              wrote on 6 Aug 2015, 11:57 last edited by
              #6

              @Maxim-DC I doubt that you can assign a QMap as a model to ListView.

              157

              M 1 Reply Last reply 6 Aug 2015, 12:01
              0
              • P p3c0
                6 Aug 2015, 11:57

                @Maxim-DC I doubt that you can assign a QMap as a model to ListView.

                M Offline
                M Offline
                Maxim DC
                wrote on 6 Aug 2015, 12:01 last edited by
                #7

                @p3c0 You can do it with a QList so why not with QMap::values(), because that returns a list

                1 Reply Last reply
                0
                • M Maxim DC
                  6 Aug 2015, 11:14

                  @p3c0 For example the qmap consists of 3 objects, <mykey1*, myobject1*> <mykey2*, myobject2*> <mykey3*, myobject3*> i want to show a list with properties, textinitem1: myobject1.somevar, textinitem2: myobject2.somevar, ....

                  P Offline
                  P Offline
                  p3c0
                  Moderators
                  wrote on 6 Aug 2015, 12:01 last edited by
                  #8

                  @Maxim-DC If you want to reuse your existing QMap then it would be better to create a new class based on QAbstractIItemModel. In there you can re-implement data() to return your data from QMap.

                  157

                  1 Reply Last reply
                  0

                  7/8

                  6 Aug 2015, 12:01

                  • Login

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