Results 1 to 5 of 5

Thread: How to access root context property from instantiated C++ QObject

  1. #1
    Join Date
    Aug 2014
    Posts
    16
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default How to access root context property from instantiated C++ QObject

    On the C++ side I do this:
    Qt Code:
    1. rootContext()->setContextProperty("foo", fooObject);
    To copy to clipboard, switch view to plain text mode 

    Then in Qml I do this:
    Qt Code:
    1. Item {
    2. MyCPlusPlusObject { }
    3. Text {
    4. text: foo.someTextProperty
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 
    Where MyCPlusPlusObject is a simple QObject based C++ object that is registered with qmlRegisterType. The question is how can I get a hold of the "foo" context property from inside the implementation of MyCPlusPlusObject. I need to somehow get a hold of the root context to get the property out of it.
    Last edited by don@thegagnes.com; 11th April 2015 at 00:24.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to access root context property from instantiated C++ QObject

    If you need access to something, why not pass it to the object explicitly?

    Cheers,
    _

  3. #3
    Join Date
    Aug 2014
    Posts
    16
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to access root context property from instantiated C++ QObject

    I"m trying to make it be more automatic. There can be many of these cpp objects in the qml. Having to add the property to every one of them is a lot of additional stuff. But if it's the only option then it may be required.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to access root context property from instantiated C++ QObject

    Quote Originally Posted by don@thegagnes.com View Post
    I"m trying to make it be more automatic. There can be many of these cpp objects in the qml. Having to add the property to every one of them is a lot of additional stuff. But if it's the only option then it may be required.
    What you want breaks encapsulation so it's best if you manage to avoid such constructs.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to access root context property from instantiated C++ QObject

    Quote Originally Posted by don@thegagnes.com View Post
    I"m trying to make it be more automatic. There can be many of these cpp objects in the qml. Having to add the property to every one of them is a lot of additional stuff. But if it's the only option then it may be required.
    Well, aside from the obvious option of making foo a singleton and accessing it through that mechanism independent of its role as a context property, you can easily retrieve the QQmlEngine that has instantiated the instance of your type, see the non-member function documentation that is part of the QQmlEngine ones.

    Cheers,
    _

Similar Threads

  1. how to access QAxObject property?
    By 0BLACK0 in forum Qt Programming
    Replies: 0
    Last Post: 17th February 2015, 12:53
  2. Replies: 0
    Last Post: 23rd May 2014, 19:01
  3. Access instantiated objects from C++
    By Sunny31 in forum Qt Quick
    Replies: 3
    Last Post: 5th December 2010, 18:25
  4. How to get a pointer to a QObject's property ?
    By HiJack in forum Qt Programming
    Replies: 7
    Last Post: 28th August 2010, 11:54
  5. Replies: 0
    Last Post: 16th April 2007, 13:18

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.