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. [SOLVED] How to access QQmlApplicationEngine?
Qt 6.11 is out! See what's new in the release blog

[SOLVED] How to access QQmlApplicationEngine?

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 3 Posters 3.0k Views 2 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hi,

    I set in my main method my QQmlApplicationEngine and load my main.qml. I need to access the engine in another class.

    What is the best approach to do that? Global variable? Something similar to "QSqlDatabase::database("myDatabase");"

    Thanks

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andresantos
      wrote on last edited by
      #2

      Not sure if this is the best method, but I would create a singleton with a QQmlApplicationEngine pointer variable and in your main pass the QQmlApplicationEngine instance to the singleton. This way you can access it everywhere.

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        What's your class' superclass where you need the reference to the engine?

        If you only need the instance of the current QQmlEngine within a QQuickItem you can use a code snippet like the following:

        QQmlContext *currentContext = QQmlEngine::contextForObject(this);
        QQmlEngine *engine = currentContext->engine();
        
        1 Reply Last reply
        1

        • Login

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