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 Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 3.7k Views
  • 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.
  • B Offline
    B Offline
    benlau
    Qt Champions 2016
    wrote on 18 May 2015, 13:29 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.

    P 1 Reply Last reply 19 May 2015, 10:26
    0
    • B benlau
      18 May 2015, 13:29

      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.

      P Offline
      P Offline
      p3c0
      Moderators
      wrote on 19 May 2015, 10:26 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
      • B Offline
        B Offline
        benlau
        Qt Champions 2016
        wrote on 19 May 2015, 17:07 last edited by
        #3

        hi @p3c0 ,

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

        P 1 Reply Last reply 20 May 2015, 04:54
        0
        • B benlau
          19 May 2015, 17:07

          hi @p3c0 ,

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

          P Offline
          P Offline
          p3c0
          Moderators
          wrote on 20 May 2015, 04:54 last edited by
          #4

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

          157

          1 Reply Last reply
          0

          1/4

          18 May 2015, 13:29

          • Login

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