Results 1 to 4 of 4

Thread: Can not read qml property from C++ when property under binding

  1. #1
    Join Date
    Apr 2014
    Posts
    125
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Android Maemo/MeeGo

    Question Can not read qml property from C++ when property under binding

    ---------- Mainitem.qml
    Qt Code:
    Switch view
    Qt Code:
    1. import QtQuick 1.0
    2.  
    3. Item1 {
    4. width: 100; height: 100
    5.  
    6. property string mrprefcolor: itemB.mypreferredcolor // Binding
    7.  
    8. ItemA1 {
    9. id: square
    10.  
    11. Rectangle {
    12. color: "red"
    13. width: 10
    14. height: 10
    15. }
    16. }
    17.  
    18. itemB {}
    19. }
    To copy to clipboard, switch view to plain text mode 
    To copy to clipboard, switch view to plain text mode
    ---------- itemB.qml -------------
    Qt Code:
    Switch view
    Qt Code:
    1. import QtQuick 1.0
    2.  
    3. property string mypreferredcolor: "red"
    4.  
    5. Item2 {
    6. property length: 6
    7.  
    8. ItemB1 {
    9. id: idc
    10.  
    11. Rectangle {
    12. color: "red"
    13. width: 10
    14. height: 10
    15. property rectype: 0
    16. }
    17. }
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 
    Hello

    I'm trying to read the Mainitem.qml mypref property from C++, but it does not work. Is it because the property is binded?
    I have tried the following (obj being the current *QObject):

    metaObject->property(i).read(obj).toString(); // i is the index of mrprefcolor

    and

    = obj->property("mrprefcolor").toString();

    Any thoughts

    Thanks;

    Mut.
    Last edited by anda_skoa; 1st April 2016 at 08:47.

  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: Can not read qml property from C++ when property under binding

    Aside from this not being valid QML, why do you want to do that?
    It is most often a bad ideal to access QML instantiated objects from C++.

    Cheers,
    _

  3. #3
    Join Date
    Apr 2014
    Posts
    125
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Android Maemo/MeeGo

    Default Re: Can not read qml property from C++ when property under binding

    Thanks for the reply. Yes, this is a bad qml, just put it there to illustrate my issue.
    I need to access the QML properties from within C++ due to internal needs of my application.

  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: Can not read qml property from C++ when property under binding

    If you can't get it to work you might have to reevaluate the internal needs of your application.

    Would be very easy to bind to the property of a custom object.

    Cheers,
    _

Similar Threads

  1. property binding in a repeater
    By sedi in forum Qt Quick
    Replies: 6
    Last Post: 2nd April 2015, 07:45
  2. Replies: 1
    Last Post: 24th November 2014, 08:28
  3. Dynamic QML Property Binding
    By EMCEE in forum Qt Quick
    Replies: 1
    Last Post: 30th August 2012, 11:51
  4. QML Property Binding update does not propagate
    By bluestreak in forum Qt Quick
    Replies: 0
    Last Post: 21st August 2012, 19:24
  5. Replies: 0
    Last Post: 17th October 2011, 13:07

Tags for this Thread

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.