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; 10th April 2015 at 23:24.

  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: 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,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 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, 11:53
  2. Replies: 0
    Last Post: 23rd May 2014, 18:01
  3. Access instantiated objects from C++
    By Sunny31 in forum Qt Quick
    Replies: 3
    Last Post: 5th December 2010, 17: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, 10:54
  5. Replies: 0
    Last Post: 16th April 2007, 12: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.