Results 1 to 9 of 9

Thread: How to expose QMap to QML?

  1. #1
    Join Date
    Oct 2011
    Posts
    35
    Thanked 9 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default How to expose QMap to QML?

    Hi,

    I've been able to use QDeclarativeListProperty to expose a simple list to QML. For instance
    Qt Code:
    1. Q_PROPERTY(QDeclarativeListProperty<MyQObjectDerivedClass> myprop READ myprop NOTIFY mypropChanged)
    To copy to clipboard, switch view to plain text mode 

    However, it would be convenient to be able to expose a QMap<QString, MyQObjectDerivedClass*> model to QML instead so that in QML I can access the elements of the model by name (i.e., something like _mymap["Toyota"].year in QML).

    How can I go about this? Do I have to derive a class from QAbstractItemModel to achieve this, or is there a simpler approach?

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to expose QMap to QML?

    There are many possible solutions. One would be to wrap your map into a QObject and expose all the fields of the map as properties of that object.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2011
    Posts
    35
    Thanked 9 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to expose QMap to QML?

    Thanks wysota. I've attempted this approach (i.e., wrapping a QHash in a QObject-derived class), but the QML side does not seem to be updated when a value changes in the QHash data member. Here are some code snippets:

    Qt Code:
    1. class BaseApplication : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. Q_PROPERTY(ParameterContainer* parameters READ parameters NOTIFY parametersChanged)
    6. public:
    7. explicit BaseApplication(QObject *parent = 0);
    8. ParameterContainer* parameters() const;
    9.  
    10. signals:
    11. void parametersChanged(ParameterContainer* arg);
    12.  
    13. private:
    14. ParameterContainer* m_parameters;
    15. };
    To copy to clipboard, switch view to plain text mode 

    When the m_parameters data member is updated in the .cpp file, I emit the parametersChanged signal to update QML, but it doesn't update.

    Qt Code:
    1. class ParameterContainer : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit ParameterContainer(QObject *parent = 0);
    6. void setParameterDisplayValue(const QString name, const QString value);
    7. Q_INVOKABLE QString getParameterDisplayValue(const QString name) const;
    8.  
    9. private:
    10. QHash<QString, QString> m_parameters;
    11. };
    To copy to clipboard, switch view to plain text mode 

    Assuming the parameters property of the BaseApplication class is exposed as a context property QML, the QML code looks like this:

    Qt Code:
    1. Text
    2. {
    3. text: "Param value: " + "<b>" + _parameters.getParameterDisplayValue("param1") + "</b>"
    4. }
    To copy to clipboard, switch view to plain text mode 

    Is the QML side not being updated because I'm trying to bind to a Q_INVOKABLE rather than a Q_PROPERTY? If so, how could I work around this issue?

    Thanks in advance!

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to expose QMap to QML?

    That's not what I meant. Each parameter should be a separate property.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Oct 2011
    Posts
    35
    Thanked 9 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to expose QMap to QML?

    I thought of doing it that way but that doesn't scale well. This map could grow to be 1000s of items and I'd rather not expose a property for each one. How can I get around this limitation?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to expose QMap to QML?

    You can't expect QML to monitor every possible key of your map/hash.

    I can only offer a solution where the whole map is an opaque qml property accompanied by a signal emited when any value in the map changes and there is a function that accepts the map object and the key argument that returns the value of the key in the map. Then QML will be able to monitor your map for changes and when the signal is emitted, it will recalculate every binding containing a call to a function that takes this map as an argument. However that doesn't scale well either since if you have 1000 keys and only one of them changes, all occurences of all keys will be recalculated. Exposing such a map to QML is probably not a very good idea at all. What is the intention behind it?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Oct 2011
    Posts
    35
    Thanked 9 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to expose QMap to QML?

    For my particular application, the Qt Quick GUI is responsible for displaying possibly100s of different settings; these settings can be modified by the user as well. The list of settings is likely to grow in the future, hence the need for it to scale well. One of my goals in the design of this is to allow new settings to be added from a resource file without the need to recompile. For this reason I was hoping I could expose a QMap to QML which gets dynamically populated from a resource file at application startup, but by the sounds of it this is perhaps a poor approach. Is a Q_PROPERTY-based approach the only true alternative?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to expose QMap to QML?

    I would expose your settings as a real object with separate properties. Even if you add a new parameter, it's not that much work to add a Q_PROPERTY for it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Sep 2013
    Posts
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Android Maemo/MeeGo

    Default Re: How to expose QMap to QML?

    Hi, sorry to bring this up if its no longer relevant.

    I encountered a similar challenge and solved it by creating a MySettingProperty for individual elements, which has a value-property. Then I made a QMap containing those settings, individually recognized by an enumeration (extensible from the QML-side). Then again I made a sorted list of the same map, which I exposed as QQmlListProperty. The MySettingProperty has a property for getting its individual type and my delegate has a loader with a selector function to create a correct delegate for each individual setting item.

Similar Threads

  1. Expose C++ enum to QML
    By OnlyK in forum Qt Quick
    Replies: 3
    Last Post: 3rd August 2011, 07:43
  2. Replies: 1
    Last Post: 24th June 2011, 00:09
  3. How to expose c++ plugins to Qml/QtCreator
    By dwalker0044 in forum Qt Quick
    Replies: 8
    Last Post: 21st June 2011, 09:39
  4. QMap
    By sophister in forum Qt Programming
    Replies: 5
    Last Post: 25th May 2009, 11:05
  5. Qmap
    By phillip_Qt in forum Qt Programming
    Replies: 3
    Last Post: 23rd November 2007, 11:43

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.