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. Instance one class
Qt 6.11 is out! See what's new in the release blog

Instance one class

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 2.0k Views 2 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
    Jeronimo
    wrote on last edited by
    #1

    Hi i'm trying to do one example to instance one class.
    At first i add headers:

    #ifndef EXAMPLE_H
    #define EXAMPLE_H
    
    #include <QObject>
    
    class Example : public QObject
    {
        Q_OBJECT
    public:
        static Example &instance();
        explicit Example(QObject *parent = 0);
    
    signals:
        //here i put my signals
    public slots:
    };
    
    #endif // EXAMPLE_H
    
    

    Cpp, here i need to include some function to emit the instance will be great if some one take ideas:

    #include "example.h"
    
    Example &Example::instance()
    {
        static MouseLogger _instance;
        return _instance;
    }
    
    
    Example::Example(QObject *parent) : QObject(parent)
    {
        HINSTANCE hInstance = GetModuleHandle(NULL);
    
    }
    
    
    

    Main function:

    #include <QCoreApplication>
    #include <QDebug>
    #include "example.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        
        QObject::connect(&Example::instance(),
                         [](){
    
    
        });
    
        return a.exec();
    }
    

    Any example or suggestion is good for know more about instance class thx

    K 1 Reply Last reply
    0
    • J Jeronimo

      Hi i'm trying to do one example to instance one class.
      At first i add headers:

      #ifndef EXAMPLE_H
      #define EXAMPLE_H
      
      #include <QObject>
      
      class Example : public QObject
      {
          Q_OBJECT
      public:
          static Example &instance();
          explicit Example(QObject *parent = 0);
      
      signals:
          //here i put my signals
      public slots:
      };
      
      #endif // EXAMPLE_H
      
      

      Cpp, here i need to include some function to emit the instance will be great if some one take ideas:

      #include "example.h"
      
      Example &Example::instance()
      {
          static MouseLogger _instance;
          return _instance;
      }
      
      
      Example::Example(QObject *parent) : QObject(parent)
      {
          HINSTANCE hInstance = GetModuleHandle(NULL);
      
      }
      
      
      

      Main function:

      #include <QCoreApplication>
      #include <QDebug>
      #include "example.h"
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
      
          
          QObject::connect(&Example::instance(),
                           [](){
      
      
          });
      
          return a.exec();
      }
      

      Any example or suggestion is good for know more about instance class thx

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @Jeronimo

      Did you have a look to the examples page?

      There are plenty of different examples.

      Vote the answer(s) that helped you to solve your issue(s)

      J 1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        It is a Singleton.
        Not much to say about it except its just glorified global variable and
        all the issues that comes from that. Some even call it an
        anti-pattern.
        anyway, this topic is already beaten like an old horse ;)
        so you can google if it matters.

        here i need to include some function to emit the instance
        Ehh ? Emit when?
        Normally you would use Example::instance() to get the global variable.

        J 1 Reply Last reply
        0
        • K koahnig

          @Jeronimo

          Did you have a look to the examples page?

          There are plenty of different examples.

          J Offline
          J Offline
          Jeronimo
          wrote on last edited by
          #4

          @koahnig But related with my question?

          mrjjM 1 Reply Last reply
          0
          • J Jeronimo

            @koahnig But related with my question?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Jeronimo
            Hi
            Do you ask about using a SingleTon ?
            (Single instance class)

            Just google for it. its all over the net :)

            1 Reply Last reply
            0
            • mrjjM mrjj

              Hi
              It is a Singleton.
              Not much to say about it except its just glorified global variable and
              all the issues that comes from that. Some even call it an
              anti-pattern.
              anyway, this topic is already beaten like an old horse ;)
              so you can google if it matters.

              here i need to include some function to emit the instance
              Ehh ? Emit when?
              Normally you would use Example::instance() to get the global variable.

              J Offline
              J Offline
              Jeronimo
              wrote on last edited by
              #6

              @mrjj yeah in the net a lot but outdate i think . Ok i will search more about this and the example koahnig help me a lot thx.

              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