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. Whats the best way to access objects out of a QList<Object> from QML
Forum Updated to NodeBB v4.3 + New Features

Whats the best way to access objects out of a QList<Object> from QML

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 1.9k 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.
  • S Offline
    S Offline
    Slash200
    wrote on last edited by
    #1

    Hello,

    I have a Qlist<QObject> that gets its objects during runtime on programmstartup.
    In my example the program is a Car Dashboarding Software, where I want to select Objects in the QML GUI out of that QList and create Gauges with the value from this objects.

    I tried QAbstractListModels, but if I understood it right, they are more for working with the whole list in QML, rather than working with single objects out of a collection and notify the gui for single objects.

    I don't really know which approach to take.

    If somehone has Ideas and examples it would be great

    regards
    Bastian

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Disclaimer:
      I dont know if the best way but
      http://doc.qt.io/qt-5/qqmllistproperty.html#details
      seems to work for that.

      http://doc.qt.io/qt-5/qtqml-referenceexamples-properties-example.html

      also this post
      https://forum.qt.io/topic/25662/qml-and-qlist-c/20
      seems to expose a custom class like you want.
      (StateDevice)

      S 1 Reply Last reply
      0
      • mrjjM mrjj

        Hi
        Disclaimer:
        I dont know if the best way but
        http://doc.qt.io/qt-5/qqmllistproperty.html#details
        seems to work for that.

        http://doc.qt.io/qt-5/qtqml-referenceexamples-properties-example.html

        also this post
        https://forum.qt.io/topic/25662/qml-and-qlist-c/20
        seems to expose a custom class like you want.
        (StateDevice)

        S Offline
        S Offline
        Slash200
        wrote on last edited by
        #3

        @mrjj
        Thank you!
        So there is now way to use QAbstractListModel to assign a objects property to a QML Item or so?
        Is there no "standard" way to work with a collection of c++ Objects in QML?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MrShawn
          wrote on last edited by
          #4

          I spent some time with QQmlListProperty and it works... kinda. I got it so that I could get to the "list" in QML but was not sure how to take it farther. Let me explain that the birthday party example is helpful but it my case it was instantiating the values in the QML side. I instantiated the objects in C++ and wanted to access the list at some int. I was not able to figure out how to get from the QMLListProperty in QML to what i was looking for. Instead I eventually created an invokable method that returned my object that took a key parameter.

          It depends on what you're looking to achieve with your list. If you're looking to use at as a model for some view then it may work better for your application. In my case I wanted to access properties of my object and my QML looked like this.

          BridgeObject.getMyObject(2).getPropertyOrInvokeMethod

          The getMyObject method provided me the functionality I was looking for with my "list" which was not to create distinctly labeled instances of my object like myObj1, myObj2,...,myObjN. and have N getters, setters, and Q_PROPERTY macros.

          1 Reply Last reply
          1
          • fcarneyF Offline
            fcarneyF Offline
            fcarney
            wrote on last edited by fcarney
            #5

            @Slash200 said in Whats the best way to access objects out of a QList<Object> from QML:

            working with single objects out of a collection and notify the gui for single objects.

            Please define this interaction. What exactly do you want to do to "single objects" and what/how do you want to "notify the gui"?

            QAbstractListModel - provides a standardized way to present a list to qml objects that use them and to qt widget objects that use them. It does not define how to interact with a QObject.
            QObject - is a base class meant to be derived from. Any specific interaction beyond the default (very limited) needs to be added via macros that expose properties and functions. Notification of data updates and events are exposed via signal/slots. Signal/slots work between C++ and QML.

            Edit:
            There are also functions that expose QObject classes to QML to be used as a QML type.

            C++ is a perfectly valid school of magic.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MrShawn
              wrote on last edited by
              #6

              @fcarney said in Whats the best way to access objects out of a QList<Object> from QML:

              There are also functions that expose QObject classes to QML to be used as a QML type.

              Could you possible point me in this direction? I use Q_INVOKABLE for methods and Q_PROPERTY but I noticed that Q_PROPERTY has some issues with QObject inheriting classes since it looks to copy the objects when sending it to the QML layer. To get around this I use pointer but if there is another approach that will allow me to not use pointers I would also appreciate that.

              1 Reply Last reply
              0
              • fcarneyF Offline
                fcarneyF Offline
                fcarney
                wrote on last edited by fcarney
                #7

                qmlRegisterType

                It still requires all the macros to expose properties/functions. It just creates a type based on C++ that you can use in your QML code. So it might not buy you anything.

                Edit:
                I take it back. It should allow treating each QObject type you expose that way as a QML object. Maybe that is what you wanted in the first place? So now those objects from that list should be interactive with all the properties/methods you exposed.

                C++ is a perfectly valid school of magic.

                1 Reply Last reply
                1
                • fcarneyF Offline
                  fcarneyF Offline
                  fcarney
                  wrote on last edited by
                  #8

                  Heh, I found this:
                  qml/c++ integration

                  C++ is a perfectly valid school of magic.

                  1 Reply Last reply
                  2
                  • M Offline
                    M Offline
                    MrShawn
                    wrote on last edited by
                    #9

                    @fcarney said in Whats the best way to access objects out of a QList<Object> from QML:

                    Heh, I found this:
                    qml/c++ integration

                    Wow nice find... I looked at the qmlRegisterType... I have been getting away with just regular qRegisterMetaType. For my application I don't want any objects created in QML just want to reference them from C++. I will look into this find more and hopefully it will help OP too. xD

                    1 Reply Last reply
                    0

                    • Login

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