There is a task from different parts of the program to send data to the output in Gui (and also to have access to the settings). Singleton is the best solution for this. But when using, some kind of stupid error comes up during signal emission, it seems to have done everything correctly, classically:
class QLogger : public QObject { Q_OBJECT public: static const QLogger * instance(); static void logger(const QString &str); static void setEnableLogging(bool stat); static inline bool isLoggingEnabled(){return self->enable;} signals: void doSend(const QString &str); private: static QChatLogger *self; QLogger(QObject * parent = 0); Q_DISABLE_COPY(QLogger) bool enable; }; //================================ QChatLogger *QChatLogger::self = 0; QLogger::QLogger(QObject *parent) : QObject(parent) { self = this; self->enable = true; } const QLogger *QLogger::instance() { static QLogger logger; return &logger; } void QLogger::logger(const QString &str) { if (self->isLoggingEnabled ()) emit self->doSend(str); } void QLogger::setEnableLogging(bool stat) { self->enable = stat; }
Oh yes. Mistake:
src/qlogger.cpp: In static member function 'static void QLogger::logger(const QString&)': src/qlogger.cpp:19:50: error: passing 'const QLogger' as 'this' argument of 'void QLogger::doSend(const QString&)' discards qualifiers [-fpermissive]
UPD: Updated the code, it is fully working. Happy Coding!
enable
property. After all, this logger will be called from different threads, which means there is a very high probability of “racing” when reading and writing, which is fraught with indefinite behavior of the logger. - vladimir_ki