[SOLVED] qInstallMessageHandler cannot convert parameter 1



  • Hello,

    I'm using qDebug, but when I release program for other people I want to log when program crash (Beta deploy)
    So I found I can use qInstallMessageHandler for use qDebug.

    But when I add to main qInstallMessaheHandler(LogHandler) it errors with code:
    @error: C2664: 'qInstallMessageHandler' : cannot convert parameter 1 from 'void (__cdecl *)(QtMsgType,QMessageLogContext &,const QString &)' to 'QtMessageHandler'
    None of the functions with this name in scope match the target type@

    main.cpp
    @void LogHandler(QtMsgType type, QMessageLogContext& context, const QString& message)
    {
    QByteArray lMessage = message.toLocal8Bit();

    QString text = NULL;
    
    switch(type)
    {
        case QtDebugMsg:
            text = QString("Debug (%s:%u, %s): %1").arg(context.file).arg(context.line).arg(context.function).arg(lMessage.constData());
            break;
    
        case QtWarningMsg:
            text = QString("Warning (%s:%u, %s): %1").arg(context.file).arg(context.line).arg(context.function).arg(lMessage.constData());
            break;
    
        case QtCriticalMsg:
            text = QString("Critical (%s:%u, %s): %1").arg(context.file).arg(context.line).arg(context.function).arg(lMessage.constData());
    
        case QtFatalMsg:
            text = QString("Fatal (%s:%u, %s): %1").arg(context.file).arg(context.line).arg(context.function).arg(lMessage.constData());
            abort();
    }
    
    QFile file("log.txt");
    file.open(QIODevice::WriteOnly | QIODevice::Append);
    
    QTextStream tStream(&file);
    tStream << text << endl;
    

    }

    int main(int argc, char* argv[])
    {
    QApplication app(argc, argv);

    qInstallMessageHandler(LogHandler);
    
    MainWindow mw;
    mw.showMaximized();
    
    return app.exec&#40;&#41;;
    

    }@

    I tried reintepred_cast, but it crash before app start :D


  • Moderators

    -are you sure your includes are correct and before the definition of your LogHandler()?-

    Your method definition is wrong...you are missing const on the second parameter:
    @
    void LogHandler(QtMsgType type, const QMessageLogContext& context, const QString& message)
    {
    }
    @



  • Oh thanks, that little mistake :/


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.