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] Access QML Singleton object from C++ code
Forum Updated to NodeBB v4.3 + New Features

[Solved] Access QML Singleton object from C++ code

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 3.8k Views 3 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.
  • benlauB Offline
    benlauB Offline
    benlau
    Qt Champions 2016
    wrote on last edited by benlau
    #1

    Hi,

    I would like to access a Singleton QML object declared via qmldir from C++ code. Is it possible? I have tried to search the children of root object from QQmlApplicationEngine but seem that singleton object is not its children .

    Thanks for any advise.

    p3c0P 1 Reply Last reply
    0
    • benlauB benlau

      Hi,

      I would like to access a Singleton QML object declared via qmldir from C++ code. Is it possible? I have tried to search the children of root object from QQmlApplicationEngine but seem that singleton object is not its children .

      Thanks for any advise.

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by p3c0
      #2

      Hi @benlau, Probably something like this:

      //QML : main.qml
      property QtObject obj: MySingleTonObject // assuming MySingleTonObject is the singleton exposed through qmldir
      
      //C++ 
      QQmlApplicationEngine engine;
      engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
      QObject *obj = qvariant_cast<QObject *>(engine.rootObjects().first()->property("obj"));
      

      157

      1 Reply Last reply
      5
      • benlauB Offline
        benlauB Offline
        benlau
        Qt Champions 2016
        wrote on last edited by
        #3

        hi @p3c0 ,

        Thx! Seem that it is the only solution for this problem.

        p3c0P 1 Reply Last reply
        0
        • benlauB benlau

          hi @p3c0 ,

          Thx! Seem that it is the only solution for this problem.

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          @benlau You're Welcome :)
          Please mark it as solved if done.

          157

          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