Results 1 to 4 of 4

Thread: QML Dynamic object binding

  1. #1
    Join Date
    Apr 2015
    Posts
    27
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Smile QML Dynamic object binding

    Hello
    I'm working on a project that requires creation and management of rectangles, that can be change throughout the course of time, according to some data received by a web socket.
    What matters here, is that I want the QMLObjects to be fully "mirrored" in C++ classes, so we only change one object and those changes reflect to the qml rectangles.

    So, according to the official QML documentation(http://doc.qt.io/qt-5/qtqml-javascri...tcreation.html), to dynamically create a rectangle I'm using this code:
    Qt Code:
    1. function buildZone() {
    2. var component;
    3. var zone;
    4. component = Qt.createComponent("Zone.qml");
    5. zone = component.createObject(layout, {
    6. "x": zoneMapper.x,
    7. "y": zoneMapper.getY()+30,
    8. "width": zoneMapper.getWidth(),
    9. "height": zoneMapper.getHeight(),
    10. "objectName": zoneMapper.getId(),
    11. "color":"red"});
    12. zoneMappers.push(zoneMapper);
    13. }
    To copy to clipboard, switch view to plain text mode 

    This "zoneMapper" is an object that it is instantiated in the main.cpp of a custom class, that has x with a property as follows:
    Q_PROPERTY(int x READ getX WRITE setX NOTIFY xChanged)

    So when you change "x", it calls a setter in the C++ class, and everything is in sync.
    Problem is, when I use a button to change zonneMapper.x, like this:
    Qt Code:
    1. Button{
    2. id:button1
    3. objectName:"Button1"
    4. text:"Button1Text"
    5. onClicked: {
    6. zoneMapper.x +=50;
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    The dynamically created rectangle doesn't move, although the "setter" and "getter" in the class are called (I can check this with breakpoints)

    I noticed though, if I do this, with a static rectangle:
    Qt Code:
    1. Rectangle{
    2. id: staticRect
    3. x:zoneMapper.x
    4. y:30
    5. width:50
    6. height:50
    7. color:"yellow"
    8. }
    To copy to clipboard, switch view to plain text mode 

    It moves!!!
    I have another problem here, regarding the "mapping" of several rectangles, but's that's stuff for another episode
    So, is there a way to bind an object in C++ to a dinamically created qml object?

    Thank you

  2. #2
    Join Date
    Jan 2012
    Location
    Dortmund, Germany
    Posts
    159
    Thanks
    69
    Thanked 10 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: QML Dynamic object binding

    As you want ro achieve a simple direct binding, you could try changing
    zone = component.createObject(layout, {
    "x": zoneMapper.x,
    "y": zoneMapper.getY()+30,[...]
    into
    zone = component.createObject(layout, {
    "x": Qt.binding(function() { return zoneMapper.x })
    "y": Qt.binding(function() { return zoneMapper.getY()+30}),[...]
    Does this work as expected?

  3. #3
    Join Date
    Apr 2015
    Posts
    27
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QML Dynamic object binding

    Well, thank you for your reply. I solved the problem with another solution though, and now I'm to far ahead that it would be a pain to test your solution. I used the function createQmlObject which uses a string as the "builder", and, somehow, the binding is never lost.

    But I have a new problem, regarding some data view model guided software.
    I want to manipulate an array of "zoneMappers"

    Notice the following code:

    Qt Code:
    1. function buildZone(buildString) {
    2.  
    3. var zone;
    4.  
    5. zone = Qt.createQmlObject(buildString, layout);
    6. zoneMappers.push(currZoneMapper);
    7. }
    To copy to clipboard, switch view to plain text mode 

    The "buildString" contains a QML file as a string with a ever incrementing number (increases in main.cpp every time you call buildZone) in one of the properties that binds the C++ object to a property in qml with setContextProperty (the only way I know to share an object with qml).
    The QMLObject "zone" is not even used anymore, because I want to create a unique access point to both QML and C++ in order to change the visual components.

    In main I have the following code:
    Qt Code:
    1. //zoneMapperList as a QList with several ZoneMapper instances
    2. for(int i = 0; i< zoneMapperList.count(); i++){
    3. QString zoneMapperId(QString("zoneMapper%1").arg(i));
    4. engine.rootContext()->setContextProperty(zoneMapperId, zoneMapperList[i]);
    5. engine.rootContext()->setContextProperty("currZoneMapper", zoneMapperList[i]);
    6. zoneMapperList[i]->setViewId(zoneMapperId);
    7. if(!QMetaObject::invokeMethod(layoutObject, "buildZone", Q_ARG(QVariant, zoneMapperList[i]->getZoneBuilderString()) )){
    8. zoneMapperList[i]->setViewId("");
    9. }
    10. else{
    11. zoneMapperList[i]->setViewObject(layoutObject->findChild<QObject*>(zoneMapperList[i]->getViewId()));
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    You can see the "not so fancy" solution I used: "currZoneMapper". I use this "currZoneMapper" because I have no other way of adding a new C+ object and add it to the array.
    The first string "zoneMapperID", which is something like "zoneMapper4" is used so QML gets different objects as different properties (with setContextProperty) and stores it in an array, so it can manipulate them as C+ objects.

    Is there a better way to bind dynamic objects to dynamic qmlcomponents and manipulate them as one entity only?
    Thank you!
    Last edited by Wer_Bn; 30th October 2017 at 18:43.

  4. #4
    Join Date
    Apr 2015
    Posts
    27
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QML Dynamic object binding

    I found a fancy solution which uses a QAbstractListModel that handles everything. This subclassed entity is bind to a repeater.
    Check this thread if you want more info on this;
    https://forum.qt.io/topic/82000/bind...qml-objects/25

Similar Threads

  1. Dynamic object's method.
    By kalwi in forum Newbie
    Replies: 6
    Last Post: 31st July 2013, 12:23
  2. QObject dynamic properties and QML binding
    By ugluk in forum Qt Quick
    Replies: 5
    Last Post: 22nd December 2012, 00:43
  3. Dynamic QML Property Binding
    By EMCEE in forum Qt Quick
    Replies: 1
    Last Post: 30th August 2012, 12:51
  4. dynamic object id
    By jovin in forum Qt Quick
    Replies: 2
    Last Post: 1st May 2012, 03:19
  5. How to generate dynamic object
    By tuxit in forum Qt Quick
    Replies: 0
    Last Post: 17th August 2011, 14:49

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.