Instance one class



  • 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


  • Moderators

    @Jeronimo

    Did you have a look to the examples page?

    There are plenty of different examples.


  • Qt Champions 2016

    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.



  • @koahnig But related with my question?


  • Qt Champions 2016

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

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



  • @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.