+ Reply to Thread
Results 1 to 11 of 11

Thread: Getting root object from c++

  1. #1
    Join Date
    Apr 2009
    Posts
    205
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Getting root object from c++

    Hello freinds,

    I have a question. Say I have this

    Qt Code:
    1. ....
    2. Rectangle{
    3. id::root
    4. property string mystring:""
    5.  
    6. MyPlugin{
    7.  
    8. Component.onCompleted: {
    9. setString("Hello");
    10. }
    11. }
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 

    How can i access my string mystring from my plugins method say componentComplete(), which is an implementation from QQmlParserStatus, or from Q_INVOKABLE MyPlugin::setString Q_INVOKABLE ???

  2. #2
    Join Date
    Apr 2009
    Posts
    205
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: Getting root object from c++

    Seems to be a very stupid question from me?!?!?!

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

    Default Re: Getting root object from c++

    Seems to be a very stupid question from me?!?!?!
    Well, lets say no one will argue with the above statement ;-)
    Its all in the docs:
    http://doc.qt.io/qt-5/qtqml-cppinteg...mlfromcpp.html
    (see Accessing Members of a QML Object Type from C++)
    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)

  4. The following user says thank you to high_flyer for this useful post:

    codeman (13th July 2017)

  5. #4
    Join Date
    Apr 2009
    Posts
    205
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: Getting root object from c++

    Well,

    first of all thank you for reply me. I read the docs. But I dont find the place where it describes
    accessing some property of the whatever.qml file from MyPlugin{}, which is loaded from qml.exe by the way.
    The docs describes accessing it from main.cpp where I load the whatever.qml in fix manner. I dont provide a main.cpp. Thats my problem.
    Perhaps I overseen something....

  6. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,469
    Thanks
    21
    Thanked 390 Times in 384 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Getting root object from c++

    Accessing QML properties from C++ is the same no matter where you are in your C++ code, main or not.
    Notice that the heading of that segment reads "Accessing Members of a QML Object Type from C++" and not "Accessing Members of a QML Object Type from main.cpp".
    In addition, in that section of the documentation main is not mentioned at all, the code examples there are totally agnostic to any scope in the code.
    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)

  7. #6
    Join Date
    Apr 2009
    Posts
    205
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: Getting root object from c++

    Hello,

    ok I make it clearer.
    Qt Code:
    1. Rectangle{
    2. id::root
    3. property string mystring:"Hello"
    4.  
    5. Button{
    6. id: btnTest
    7. width:140
    8. height: 30
    9. text: "btnTest"
    10. onClicked: {
    11. console.log(root.mystring)
    12.  
    13. }
    14.  
    15. MyPlugin{
    16. id:plugin1
    17. Component.onCompleted: {
    18.  
    19. }
    20. }
    21.  
    22. }
    To copy to clipboard, switch view to plain text mode 
    When I load my qml file through qml.exe it loads the qml file and my property string mystring is equal to "Hello".
    Then in MyPlugin::componentComplete I make this:
    Qt Code:
    1. QQmlEngine *mrtEngine = qmlEngine(this);
    2. QQmlComponent component(mrtEngine, "path/to/file.qml");
    3. QObject *object = component.create();
    4. qCritical() << "Property value:" << QQmlProperty::read(object, "mystring").toString();
    5. QQmlProperty::write(object, "mystring", "Hello QtCentre");
    6. delete object;
    To copy to clipboard, switch view to plain text mode 

    So now when I click my Button mystring is equal to "Hello" and not "Hello QtCentre"?!?

  8. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,469
    Thanks
    21
    Thanked 390 Times in 384 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Getting root object from c++

    Which qml file are you loading with 'path/to/file.qml'?
    The QML file posted above with the root object?
    And you are doing it from the plugin code?
    If so this makes no sense to me, as your plugin creates a new root object where itself is also initialized??
    otherwise please elaborate more.
    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)

  9. #8
    Join Date
    Apr 2009
    Posts
    205
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: Getting root object from c++

    this is my "path/to/file.qml"
    Qt Code:
    1. Rectangle{
    2. id::root
    3. property string mystring:"Hello"
    4.  
    5. Button{
    6. id: btnTest
    7. width:140
    8. height: 30
    9. text: "btnTest"
    10. onClicked: {
    11. console.log(root.mystring)
    12.  
    13. }
    14.  
    15. MyPlugin{
    16. id:plugin1
    17. Component.onCompleted: {
    18.  
    19. }
    20. }
    21.  
    22. }
    To copy to clipboard, switch view to plain text mode 

    Yes you are right. The qml.exe is loading 'path/to/file.qml' and then my plugin is also handling the 'path/to/file.qml' file.
    My goal is to modify loaded qml file through the plugins in it...

  10. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,469
    Thanks
    21
    Thanked 390 Times in 384 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Getting root object from c++

    The C++ code you posted, is it in your plugin or application?
    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)

  11. #10
    Join Date
    Apr 2009
    Posts
    205
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: Getting root object from c++

    It is in my Plugin as I said
    Then in MyPlugin::componentComplete I make this:
    Qt Code:
    1. QQmlEngine *mrtEngine = qmlEngine(this);
    2. QQmlComponent component(mrtEngine, "path/to/file.qml");
    3. QObject *object = component.create();
    4. qCritical() << "Property value:" << QQmlProperty::read(object, "mystring").toString();
    5. QQmlProperty::write(object, "mystring", "Hello QtCentre");
    6. delete object;
    To copy to clipboard, switch view to plain text mode 

  12. #11
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,469
    Thanks
    21
    Thanked 390 Times in 384 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Getting root object from c++

    At least for me it hard to follow.
    It seems something in your setup, is wrong, but its hard to say what without seeing the full picture.
    Try uploading your project as a zip here and we can then take a better look.
    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. Replies: 3
    Last Post: 10th February 2014, 18:46
  2. TypeError: Object [object Object] has no method 'sendData'
    By TheIndependentAquarius in forum Qt Quick
    Replies: 2
    Last Post: 30th November 2013, 05:54
  3. debugging as root
    By weaver4 in forum Qt Programming
    Replies: 0
    Last Post: 13th February 2012, 21:10
  4. Replies: 1
    Last Post: 8th November 2011, 22:27
  5. Replies: 4
    Last Post: 15th July 2011, 18:31

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