Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. getSingletonInstance() for singletons in QML
Forum Updated to NodeBB v4.3 + New Features

getSingletonInstance() for singletons in QML

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 147 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.
  • Y Offline
    Y Offline
    Yihua Liu
    wrote on last edited by
    #1

    I'm reading https://doc.qt.io/qt-6/qml-singleton.html#exposing-an-existing-object-as-a-singleton where there is a code snippet:

    SingletonForeign::s_singletonInstance = getSingletonInstance();
    QQmlApplicationEngine engine;
    engine.loadFromModule("MyModule", "Main");
    

    However, I did not find the definition or descriptions of the function getSingletonInstance() anywhere. What is this function and how can I define and use it? Thanks

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Shrishashank
      wrote on last edited by Shrishashank
      #2

      The function getSingletonInstance() is not a built-in Qt function. It is likely a custom function that the code author created to provide a singleton instance of the SingletonForeign class.

      A singleton is a design pattern that ensures that a class has only one instance and provides a global point of access to that instance. The getSingletonInstance() function in this context is probably used to retrieve or create the single instance of the SingletonForeign class.

      Here’s an example of how you can define the getSingletonInstance() function for a singleton class:

      Step 1: Define the SingletonForeign class

      class SingletonForeign {
      public:
      static SingletonForeign* getSingletonInstance() {
      if (s_singletonInstance == nullptr) {
      s_singletonInstance = new SingletonForeign();
      }
      return s_singletonInstance;
      }

      static void cleanup() {
          delete s_singletonInstance;
          s_singletonInstance = nullptr;
      }
      

      private:
      SingletonForeign() {}

      // Static member to hold the single instance
      static SingletonForeign* s_singletonInstance;
      

      };

      SingletonForeign* SingletonForeign::s_singletonInstance = nullptr;

      Step 2: Use the getSingletonInstance() method in your main code
      In your case, you would use getSingletonInstance() to get the singleton instance of SingletonForeign:
      SingletonForeign::s_singletonInstance = SingletonForeign::getSingletonInstance();
      QQmlApplicationEngine engine;
      engine.loadFromModule("MyModule", "Main");

      SHRISHASHANK S K

      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