Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

qInstallMessageHandler() add object method. How?



  • Hello!
    Is there any way to add to qInstallMessageHandler() NOT static function but object method?
    Something like this:

    ObjectType* oObject = new ObjectType();
    qInstallMessageHandler(oObject->objectMethod());
    

    I know how to use static method:

    class Class {
      static void  messageHandler(QtMsgType, const QMessageLogContext &, const QString &);
    };
    
    ...
    qInstallMessageHandler(Class::messageHandler);
    ...
    

    The question is about calling from object.



  • @bogong
    To answer your question directly, you should be able to go one of:

    qInstallMessageHandler(oObject->objectMethod);
    qInstallMessageHandler(&oObject->objectMethod);
    

    But it's not a very good idea! Because if oObject goes away your global message handler is left pointing to a method of a non-existent object....



  • Solution found. Issue closed. Example is here.



  • @bogong
    Having glanced through that, I see it goes

    qInstallMessageHandler(ALogger::mWriteToLog);
    

    Which is a static function, exactly what you said you did not want!

    Note that it does not go

    qInstallMessageHandler(oLogger->mWriteToLog);
    

Log in to reply