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



  • 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


  • Lifetime Qt Champion

    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)



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



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



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



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



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



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.