Results 1 to 4 of 4

Thread: QML Dynamic object binding

Hybrid View

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

    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.

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

    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
  •  
Qt is a trademark of The Qt Company.