Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to access a root context from QObject?
Forum Updated to NodeBB v4.3 + New Features

How to access a root context from QObject?

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 3 Posters 2.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alexey
    wrote on last edited by
    #1

    Hi everybody!

    I've created my QObject-derived subclass and registered it with the QML type system. Now I need to access the root context from my object's method. How can I do it?

    Thank you!

    1 Reply Last reply
    0
    • C Offline
      C Offline
      chrisadams
      wrote on last edited by
      #2

      See the qmlContext() and qmlEngine() documentation. You may need to look at the qqmlengine.cpp source code directly to see that documentation, as qdoc is a broken mess.

      Cheers,
      Chris.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        cpbotha
        wrote on last edited by
        #3

        Here's the full answer to the question. Inside your QObject derived subclass, you can do the following:

        @
        // get rootContext() and required contextProperty as QVariant
        QVariant myobject_v = qmlEngine(this)->rootContext()->contextProperty("myname");
        // cast QVariant first to QObject* and then to my desired ObjectClass*
        ObjectClass myobject = qobject_cast<ObjectClass>(qvariant_cast<QObject*>(myobject_v));
        @

        http://stackoverflow.com/a/3918893/532513 showed how to cast from QVariant to ObjectClass*, the rest was found through snippets of documentation scattered all over the place. :|

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved