Theory: Qinstallmessagehandler is defined in the global space:
Http://doc.qt.io/qt-5/qtglobal.html#QtMessageHandler-typedef
Practice:
http://blog.csdn.net/liang19890820/article/details/51838379
void Mymessageoutput (qtmsgtype type, const qmessagelogcontext &context, const QString &msg){//locking static Qmutex mutex; Mutex.lock (); Qbytearray localmsg = Msg.tolocal8bit (); QString strMsg (""); Switch (type) {case Qtdebugmsg:STRMSG = QString ("Debug:");Break ; Case Qtwarningmsg:STRMSG = QString ("Warning:");Break ; Case Qtcriticalmsg:STRMSG = QString ("Critical:");Break ; Case Qtfatalmsg:STRMSG = QString ("Fatal:");Break ; }//Set Output Information format QString strdatetime = Qdatetime::currentdatetime (). toString ("Yyyy-mm-dd hh:mm:ss ddd"); QString strmessage = QString ("message:%1 file:%2 line:%3 function:%4 datetime:%5"). Arg (Localmsg.constdata () ). Arg (context.file). Arg (context.line). Arg (context.function). Arg (strdatetime); Output information to the file (read-write, append form) QFile files ("Log.txt"); File.Open (Qiodevice::readwrite | Qiodevice::append); Qtextstream Stream (&file); Stream << strmessage << "\ r \ n"; File.flush (); File.close (); Unlock Mutex.unlock ();} int main (int argc, char **argv){qapplication app (argc, argv); Install Message Handler Qinstallmessagehandler (mymessageoutput); Print information Qdebug ("This is a debug message."); Qwarning ("This is a warning message."); Qcritical ("This is a critical message."); Qfatal ("This is a fatal message."); ... return app.exec ();}
So I can customize the Mymessageoutput function, the function is arbitrarily specified, but still can work correctly in the whole QT framework, relying on the method of function pointer.
Before I knew this theory, but did not know under what circumstances should apply it, now is understood. And this should also be used in the QT framework, such as the use of Qtfutur.
A living Example: Qinstallmessagehandler accepts a function pointer of the specified type so that it can be locked by the programmer to customize