A living Example: Qinstallmessagehandler accepts a function pointer of the specified type so that it can be locked by the programmer to customize

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.