+ Reply to Thread
Results 1 to 3 of 3

Thread: Binding properties of dynamically created QML objects

  1. #1
    Join Date
    Aug 2017
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Binding properties of dynamically created QML objects

    Hi all!

    I have some objects that I create dynamically in QML, and I would like to bind some of these objects' properties to properties of static objects.

    I have used a ListModel to manage the dynamically created objects as suggested here.

    Using this ListModel I have no problem looping through all the objects and accessing them, but I can't figure out how to bind their properties to other objects.

    The same site suggests using the Binding component, but I do not understand how to use it.

    Without the Binding component, I have tried this:

    Qt Code:
    1. for (var i = 0; i < listModel.count; ++i)
    2. {
    3. var dynObj = listModel.get(i).obj // Gives me access to the object
    4. dynObj.someProperty = Qt.binding(function() {return staticObject.otherProperty}) // I want this to work
    5. }
    To copy to clipboard, switch view to plain text mode 

    This sets the value of dynObj.someProperty to staticObject.otherProperty at the time this call is made, but when otherProperty changes later on QML complains about "Something related to the property not being available, I dont' remember of the top of my head, beacuse I'm writing this at home and I do not have access to the code at this moment."


    Any ideas?
    Last edited by Obi-Wan; 5th August 2017 at 12:44.

  2. #2
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    283
    Thanks
    22
    Thanked 30 Times in 26 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Binding properties of dynamically created QML objects

    I do it just like you did, and it works fine for me. It would help if you could post the exact message and the snippet of code. Is that a error or warning message, the bindind still works after that ?
    _______________________________________________
    Math Graphica - scientific calculator with graphs 2D / 3D / 4D, user defined functions, system of equations, formulas and integrals
    Qt Ambassador

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,620
    Thanks
    21
    Thanked 405 Times in 399 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Binding properties of dynamically created QML objects

    As I am a C++ guy, I might be barking on the wrong tree here but what happens if you do this:
    Qt Code:
    1. for (var i = 0; i < listModel.count; ++i)
    2. {
    3. listModel.get(i).obj.someProperty = Qt.binding(function() {return staticObject.otherProperty})
    4. }
    To copy to clipboard, switch view to plain text mode 

    The reason for this is that I think that your local 'dynObj' might be destroyed at the end of the for loop.
    Again, I don't know if this is true for JS, but I'd at least check it out.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
    Watch these videos, and read his books they are must if you want to write clean code:
    https://sites.google.com/site/unclebobconsultingllc/

    Recommended off-line reading Qt:
    The Foundations of Qt Development (ISBN: 1-59059-831-8)

+ Reply to Thread

Similar Threads

  1. QObject dynamic properties and QML binding
    By ugluk in forum Qt Quick
    Replies: 5
    Last Post: 22nd December 2012, 00:43
  2. Replies: 3
    Last Post: 9th February 2011, 12:07
  3. Dynamically created buttons.
    By Tomasz in forum Newbie
    Replies: 26
    Last Post: 2nd December 2010, 10:40
  4. Connecting dynamically added properties
    By stefkeB in forum Qt Programming
    Replies: 1
    Last Post: 5th December 2008, 16:19
  5. Replies: 7
    Last Post: 18th July 2006, 22:33

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