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. Static singleton and Q_GLOBAL_STATIC
Forum Updated to NodeBB v4.3 + New Features

Static singleton and Q_GLOBAL_STATIC

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 9.9k Views 1 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.
  • J Offline
    J Offline
    JulienMaille
    wrote on last edited by
    #1

    I'm trying to use this "macro ":http://lists.trolltech.com/qt-interest/2008-03/msg00022.html to define a static singleton of one class.
    Here is what I wrote in MyClass.h
    @class MyClass : public QObject
    {
    Q_OBJECT

    public:
    MyClass();
    void doSomething();
    };

    Q_GLOBAL_STATIC(MyClass, myClassInstance)@

    But then it creates two different instances when I use myClassInstance()->doSomething() in my mainwindow, and once more in a QDialog created from the mainwindow (same thread)
    What's wrong?

    1 Reply Last reply
    1
    • J Offline
      J Offline
      JulienMaille
      wrote on last edited by
      #2

      Hum, I changed it to:
      MyClass.h
      @class MyClass : public QObject
      {
      Q_OBJECT

      public:
      MyClass();
      static MyClass* instance();
      void doSomething();
      };@
      and MyClass.cpp
      @Q_GLOBAL_STATIC(MyClass, myClassInstance)

      MyClass *MyClass::instance()
      {
      return myClassInstance();
      }@
      Now, am I doing it right?

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        Q_GLOBAL_STATIC should be in the .cpp file. Otherwise it would be compiled as often as the header file is included, resulting in the same amount of instances.

        The instance() method isn't needed, I would say.

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • J Offline
          J Offline
          JulienMaille
          wrote on last edited by
          #4

          Dear Volker, considering that myClassInstance() is defined in the .cpp file, what method will you call if you remove the instance() one?

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            Ah, sorry. You'll need the instance() method, of course. The method defined by the macro is only visible in the .cpp file it is in. Sorry for the confusion.

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • J Offline
              J Offline
              JulienMaille
              wrote on last edited by
              #6

              Ok thanks for the clarification

              1 Reply Last reply
              0
              • M Offline
                M Offline
                miroslav
                wrote on last edited by
                #7

                I think the Q_GLOBAL_STATIC macro defines the myClassInstance method.

                Mirko Boehm | mirko@kde.org | KDE e.V.
                FSFE Fellow
                Qt Certified Specialist

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  JulienMaille
                  wrote on last edited by
                  #8

                  Yes it does.

                  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