From the docs:
typedef QtMessageHandler
This is a typedef for a pointer to a function with the following signature:
void myMessageHandler(QtMsgType, const QMessageLogContext &, const QString &);
The important words are pointer to a function, not pointer to a MEMBER function. This typically means a stand-alone function, but in your case you might be able to use a static member function of your Log class.
However, I would turn your design inside-out. Implement the message handler as a stand-alone function, but inside the handler, relay the message to the singleton's handler:
{
Q_OBJECT
public:
//class member declaration
static Log& ReturnInstance()
{
static Log Log_obj; //The only Log object that will be used
return Log_obj;
}
void myMessageHandler
(QtMsgType type,QMessageLogContext
&context,
const QString &msg
);
private:
//some member variable declartion
};
static void theMessageHandler
(QtMsgType type,QMessageLogContext
&context,
const QString &msg
) {
Log & log = Log::ReturnInstance();
log.myMessageHandler( type, context, msg );
}
#include "mainwindow.h"
#include <QApplication>
#include "logger.h"
int main(int argc, char *argv[])
{
qInstallMessageHandler( theMessageHandler ); //<------ The former error
MainWindow w;
w.show();
return a.exec();
}
class Log : public QObject
{
Q_OBJECT
public:
//class member declaration
static Log& ReturnInstance()
{
static Log Log_obj; //The only Log object that will be used
return Log_obj;
}
void myMessageHandler(QtMsgType type,QMessageLogContext &context,const QString &msg);
private:
//some member variable declartion
QFile m_file;
};
static void theMessageHandler(QtMsgType type,QMessageLogContext &context,const QString &msg)
{
Log & log = Log::ReturnInstance();
log.myMessageHandler( type, context, msg );
}
#include "mainwindow.h"
#include <QApplication>
#include "logger.h"
int main(int argc, char *argv[])
{
qInstallMessageHandler( theMessageHandler ); //<------ The former error
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks