Results 1 to 11 of 11

Thread: Run time generated property set in C++ exposed to QML

  1. #1
    Join Date
    Aug 2014
    Posts
    16
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Run time generated property set in C++ exposed to QML

    I would like to set up the following object hierarchy:

    Foo has children of type Child named Child1 and Child2. The Child type has fixed properties : prop1 and prop 2 for example. I can see how to create this object hierarchy in C++ using the Q_PROPERTY syntax and then push then into the QML context such that I can property bind to Foo.Child1.prop1. The problem I am trying to solve is that the number of Child objects and their names is determined at runtime. I don't see how I can create a Foo object that exposes a dynamic set of Child objects that still supports property binding.

    Docs for Qt Script seems to say that if I parent the child QObjects to Foo and give them names I should be able to access them that way. But that seems to be only for Qt Script. I can't get it to work in QML.

    QScriptValue looked promising as well. Since you can create the object dynamically and add properties to it. But once again that seems to be only for Qt Script.

    Is there some other way to do it?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Run time generated property set in C++ exposed to QML


  3. #3
    Join Date
    Aug 2014
    Posts
    16
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Run time generated property set in C++ exposed to QML

    Seems so close! But from looking at the callbacks I need to provide it seems like I could only access the list using the syntax Foo[1].prop1. I want to be able to access the child properties by name: Foo.Child.prop1. Is that incorrect?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Run time generated property set in C++ exposed to QML

    Hmm. Can you describe your actual use case?

    Cheers,
    _

  5. #5
    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: Run time generated property set in C++ exposed to QML

    Quote Originally Posted by don@thegagnes.com View Post
    I want to be able to access the child properties by name: Foo.Child.prop1.
    Give the child an id and invoke it by that id: "childId.prop1"
    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.


  6. #6
    Join Date
    Aug 2014
    Posts
    16
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Run time generated property set in C++ exposed to QML

    Question on using "id:" to identify the properties dynamically: How do I set "id" programmatically in C++ code? Is "id" a dynamic property on the object?

    Here is the use case: I'm connecting to a piece of hardware. That hardware is configured through a set of parameters. The parameters are named with strings. The parameter set differs depending on what firmware you have installed. I want to expose those parameters as properties so I can create QML ui to edit them.

    How I currently do it:
    - I have a Parameters object which has a Q_PROPERTY in it for each parameter which is named the parameter string name.
    - The Q_PROPERTY exposes a Fact object which has a value property as well as meta data such as min/max ranges, user visible descriptions and so forth.
    - I can then bind to the parameter in QML like this: parameters.PROP_1.value
    - The top level Parameters object exists as a container for the properties because there will be additional sets of information like this. There is also telemetry data which comes back from the board, which I want to also expose as Facts. By using the top level object it removes possible collision on the namespace between the two sets of values.

    What I would like to do:
    - I can query the hardware for all the parameter names. Using that I'd like to attach the parameter Fact properties to the Parameters object dynamically at run time.
    - This way if the client code gets out of sync with the firmware parameter set it will still work since the property list is added at runtime.

    Also, thanks a ton for all the help.

  7. #7
    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: Run time generated property set in C++ exposed to QML

    Quote Originally Posted by don@thegagnes.com View Post
    Question on using "id:" to identify the properties dynamically: How do I set "id" programmatically in C++ code?
    You don't. Why would you want to do that? You said you wanted to access the object in QML.

    Is "id" a dynamic property on the object?
    No, id is not reflected on the C++ side.

    Here is the use case: I'm connecting to a piece of hardware. That hardware is configured through a set of parameters. The parameters are named with strings. The parameter set differs depending on what firmware you have installed. I want to expose those parameters as properties so I can create QML ui to edit them.

    How I currently do it:
    - I have a Parameters object which has a Q_PROPERTY in it for each parameter which is named the parameter string name.
    - The Q_PROPERTY exposes a Fact object which has a value property as well as meta data such as min/max ranges, user visible descriptions and so forth.
    - I can then bind to the parameter in QML like this: parameters.PROP_1.value
    - The top level Parameters object exists as a container for the properties because there will be additional sets of information like this. There is also telemetry data which comes back from the board, which I want to also expose as Facts. By using the top level object it removes possible collision on the namespace between the two sets of values.

    What I would like to do:
    - I can query the hardware for all the parameter names. Using that I'd like to attach the parameter Fact properties to the Parameters object dynamically at run time.
    - This way if the client code gets out of sync with the firmware parameter set it will still work since the property list is added at runtime.
    In my opinion you should expose the data as a plain-old QAbstractListModel model. You can then connect to that model on QML side using a view (e.g. ListView) or a custom UI.
    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.


  8. #8
    Join Date
    Aug 2014
    Posts
    16
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Run time generated property set in C++ exposed to QML

    As far as setting an id from the C++ side I was just trying to figure out how to use your example. The objects are all created in C++ and then pushed into a QML context. Given that, using an id won't work.

    If I use a list model I won't be able to do something like this in QML will I?

    TextInput {
    text: parameters.PROP_1.value
    }

  9. #9
    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: Run time generated property set in C++ exposed to QML

    Quote Originally Posted by don@thegagnes.com View Post
    As far as setting an id from the C++ side I was just trying to figure out how to use your example. The objects are all created in C++ and then pushed into a QML context. Given that, using an id won't work.
    You surely have a QML declaration somewhere which refers to a particular parameter (and you can do that via id), otherwise your QML code wouldn't know how to "call" a particular parameter as it might not exist.

    If I use a list model I won't be able to do something like this in QML will I?

    TextInput {
    text: parameters.PROP_1.value
    }
    If you put that in a delegate referencing a particular model entry then it would be simpler:

    javascript Code:
    1. ListView {
    2. id: view
    3. model: myModel
    4. // ...
    5.  
    6. Row {
    7. width: view.width
    8. Text { text: model.name }
    9. Slider {
    10. minimumValue: model.minimum
    11. maximumValue: model.maximum
    12. value: model.value
    13. onValueChanged: model.value = value
    14. }
    15. Text { text: model.value }
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    You could even have different types of parameters and construct a proper delegate depending on the type (e.g. putting a slider for numeric parameters and text field for textual ones).
    Last edited by wysota; 7th December 2014 at 22:26.
    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.


  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Run time generated property set in C++ exposed to QML

    What you could try is a map

    Qt Code:
    1. class Properties : public QObject
    2. {
    3. Q_OBJECT
    4. Q_PROPERTY(QVariantMap facts .... )
    5. };
    To copy to clipboard, switch view to plain text mode 
    where the key is the property name and the value is a Fact object

    Or exposing the map directly as a context property (if it does not change during runtime).

    Cheers,
    _

  11. #11
    Join Date
    Aug 2014
    Posts
    16
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Run time generated property set in C++ exposed to QML

    QVariantMap - That look like exactly what I need. Thank you very much.

Similar Threads

  1. Replies: 1
    Last Post: 24th November 2014, 08:28
  2. qtablewidget exposed lines
    By tuli in forum Newbie
    Replies: 1
    Last Post: 25th August 2014, 12:47
  3. Accessing C++ Element exposed for QML in C++
    By Rocken in forum Qt Quick
    Replies: 1
    Last Post: 22nd February 2012, 13:12
  4. Know Number of Property Exposed by the Widget
    By Ashutosh2k1 in forum Qt Programming
    Replies: 4
    Last Post: 29th June 2011, 12:12

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.