Important: Please read the Qt Code of Conduct -

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
        static Example &instance();
        explicit Example(QObject *parent = 0);
        //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);
        return a.exec();

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

  • @Jeronimo

    Did you have a look to the examples page?

    There are plenty of different examples.

  • Lifetime Qt Champion

    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
    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?

  • Lifetime Qt Champion

    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