Important: Please read the Qt Code of Conduct -

Passing logging information from C++ to QML; plugins

  • I'm trying to diagnose serialnmea/GPS issues on a client's computer and would like to be able to pass logging information from the serialnmea plugin's C++ code to my QML components so I can write this information to a file.

    For example, the plugin code includes lines like this:

    qCDebug(lcSerial) << "Opening serial port" << portName;
    qWarning("serialnmea: No known GPS device found. Specify the COM port via QT_NMEA_SERIAL_PORT.");

    And I've added some of my own qWarning() messages to the plugin and rebuilt a custom version. These are very useful when running the program via QtCreator, but now I need access to this information in a release executable.

    What are the available mechanisms for me to get these messages from the plugin to my QML components so I can write them to a log file?

  • Looks like one option is to use qInstallMessageHandler and write a handler in main.cpp that writes log messages to a file instead of stderr. That would appear to require a global file object, which isn't super clean, but it does seem like it works. I haven't tried it after building an executable, however, and I have a feeling the message handler may not get called at all in a release build.

    Am I on the right track? Any ideas on how to make the file path for this logging more dynamic/set via Qt.labs.settings Settings?

    For example, in main.cpp:

    static QPointer<QFile> logFile = nullptr;
    void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
        QTextStream stream(logFile);
        QByteArray localMsg = msg.toLocal8Bit();
        const char *file = context.file ? context.file : "";
        const char *function = context.function ? context.function : "";
        switch (type) {
        case QtDebugMsg:
            stream << "Debug: " << localMsg.constData() << " (" << file << ":" << context.line << ", " << function << ")" << endl;
        case QtWarningMsg:
    int main(int argc, char *argv[])
        QString filename = "/tmp/console_messages.txt";
        QFile file(filename);
        logFile = &file;
        if (logFile->open(QFile::Append)) {

Log in to reply