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. qmlregistersingletoninstance
Forum Updated to NodeBB v4.3 + New Features

qmlregistersingletoninstance

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 2.6k 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.
  • T Offline
    T Offline
    TonyN
    wrote on last edited by
    #1

    I am trying to find information regarding qmlRegisterSingletonInstance(), but found almost none. The best one I found is here
    https://code.woboq.org/qt5/qtdeclarative/src/qml/doc/src/qmlfunctions.qdoc.html

    I wondering what is the difference between qmlRegisterSingletonInstance() and instantiate an instance and register it with QQmlContext::setContextProperty(const QString &name, QObject *value) ?
    It seem that I can access to the c++ instance the same way in QML with these 2 different method. Can some one elaborate more on qmlRegisterSingletonInstance()?

    Thanks

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      QtQmlLearner
      wrote on last edited by QtQmlLearner
      #2

      @TonyN
      Basically we do singleton instance when :
      We want to use a single object of a class throughout the lifetime of an application.

      Check the link below:
      https://doc.qt.io/qt-5/qtqml-cppintegration-overview.html

      1 Reply Last reply
      1
      • GrecKoG Offline
        GrecKoG Offline
        GrecKo
        Qt Champions 2018
        wrote on last edited by
        #3

        Lookup of context properties is more expensive than singleton (it has to traverse the context hierarchy all the way to the root context to find a context property) and less typed (singleton needs an import statement and the engine know the type thanks to the registration). Qt Creator also will also have an easier time providing auto complexion on it.

        In QML 3 context properties will be removed.

        So for new code use qmlRegisterSingletonInstance() and avoid QQmlContext::setContextProperty().

        T 1 Reply Last reply
        1
        • GrecKoG GrecKo

          Lookup of context properties is more expensive than singleton (it has to traverse the context hierarchy all the way to the root context to find a context property) and less typed (singleton needs an import statement and the engine know the type thanks to the registration). Qt Creator also will also have an easier time providing auto complexion on it.

          In QML 3 context properties will be removed.

          So for new code use qmlRegisterSingletonInstance() and avoid QQmlContext::setContextProperty().

          T Offline
          T Offline
          TonyN
          wrote on last edited by
          #4

          @GrecKo Thank you. That explains it.

          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