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

console.log() or debug() etc. prints nothing to console from static library (QtCreator)



  • Hi,
    I need help to get QML Debugging working correctly with QtCreator.

    It seems that I only get few messages printed to the CDB Debugger Console or to the terminal.
    I'm developing a customized GUI with QML for an existing Qt Widgets based Application. My QML App comes to use embedded as shared library in the widget app directly (on Linux systems) such as a separate executable application (on Windows systems).

    This is why my project is splitted into 3 targets configured with CMake:

    • shared library (plugin)
    • executable (main.cpp, win32)
    • static library (QML application and backend)

    The shared library and the executable include the static library. So I use the executable mainly for debugging purposes for now.
    The QML files are loaded directly not via the qrc because I implement a very simple hotloading mechanism.

    Everything works fine the application itself such as the c++ debugging via CDB except for the qDebug() and the concole.log() etc. outputs.

    Here are some code snippets.

    int main(int argc, char *argv[])
    {
    #if defined(Q_OS_WIN)
    //    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    #endif
    
        std::cout << "cout before app" << std::endl;
        qDebug() << "qDebug before app" << endl;           // appears nowhere
    
        QGuiApplication app(argc, argv);
    
        Backend* backend = Backend::instance();
        QQuickView* view = backend->start_qml_app();       // the returned view is necessary for the widgets based main app
    
        std::cout << "cout after app" << std::endl;
        qDebug() << "qDebug after app" << endl;            // appears nowhere
    
        return app.exec();
    }
    
    

    in backend:

    void Backend::message_handler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
    {
        QByteArray local_msg = msg.toUtf8();
        const char* file = context.file ? context.file : "";
        const char* function = context.function ? context.function : "";
    
        switch (type) {
            case QtDebugMsg:
                fprintf(stdout, "\nDebug: %s\n    (%s:%u, %s)\n", local_msg.constData(), file, context.line, function);
                break;
            case QtInfoMsg:
                fprintf(stdout, "\nInfo: %s\n    (%s:%u, %s)\n", local_msg.constData(), file, context.line, function);
                break;
    
        ...
    
    }
    
    QQuickView* Backend::start_qml_app()
    {
        std::cout << "cout: start qml app" << std::endl;
        qInfo() << "qInfo: start qml app" << endl;                       // appears in the QML Debugger Console
    
        qDebug() << "qDebug_1" << endl;                                  // appears in the QML Debugger Console
    
        qInstallMessageHandler(message_handler);
    
        qDebug() << "qDebug_2, after install message handler" << endl;   // appears only when message handler is installed, otherwise also nowhere
    
        // some init-functions
    
        backend->load(QUrl::fromLocalFile("main.qml"));
    
        std::cout << "cout: qml app started" << std::endl;
        qDebug() << "qDebug: qml app started" << endl;                   // appears nowhere
    
        return backend->view_;
    }
    
    • normal start
      • only cout prints to the Application Output in QtCreator, except the second qDebug() call after the installing a message handler. No following qDebug call in other functions prints anything
      • no console.log or console.debug output, this is confusing because the message handler should send it to sdtout
    • debugging of startup project
      • the same but now the output is printed to the cdb console and the Application Output keeps empty
      • now the first qInfo and qDebug of start_qml_app() appears in the QML Debugger Console till message handler is set
      • the first both qDebug from main are still missing
      • console.log from QML still missing too

    When I load the main.qml directly with a QQmlEngine instance in the main function the QML console.log() and info() or debug() outputs works as they should except of the qDebug() outputs which are missed before.

    Things tried without any effect

    • setting link flags for console app in CMakeLists
    set_target_properties(${component_name} PROPERTIES
        LINK_FLAGS "/SUBSYSTEM:CONSOLE"
    )
    
    - causes the output in a separate terminal window during debug
    
    • setting startup parameter -qmljsdebugger=port:3769,block and attach debugger afterwards
    • setting some environment variables:
    QML_IMPORT_TRACE=1          # trace were shown
    QT_LOGGING_TO_CONSOLE=1
    QT_LOGGING_RULES="*.debug=true"
    QT_LOGGING_DEBUG=1
    

    I suspect it has something to do with including the static library and the executable.

    System/Environment:

    • Windows 10
    • Qt 5.12 (Siemens build, 32bit)
    • MSVC 2017 15.9
    • CDB, Windows SDK 10.0.17763.0
    • CMake 3.19.2
    • QtCreator 4.14

    Thanks for any advise.
    Best regards.



  • OK I could narrow down the problem a little bit.

    I need to use third-party libraries. It seems that there is a deeper impact on the debugging infrastructure of the Qt framework.
    When I drop all these dependencies from my own code than everything works without any further configuration out-of-the-box in QtCreator.

    Maybe there is a problem in the setContextProperty or qmlRegisterType which I used to introduce my classes and functions to the QML context.


  • Moderators

    Can you clarify what you mean by 'cdb console'?

    One thing to keep in mind that the 'debug output' that Qt Creator, Visual Studio ... use on Windows to get output for GUI programs is a system wide one, and if you have several consumers attached to it, it might be that the output it split between them. That is, if you launch apps in multiple instances of Qt Creator, or use Visual Studio, or DebugView (https://docs.microsoft.com/en-us/sysinternals/downloads/debugview) chances are that you miss some of the output.



  • Yes, I'm aware of that. With CDB I mean the Microsoft Console Debugger. And in QtCreator there are several outputs, the C++-Debugger, in my case the CDB, the QML-Debugger and the application output for standard outputs. In case the application is compiled as a console application under Windows the standard output will written to a terminal.

    But this is not the problem. I got all messages on stdout etc. directly when I use the c++ routines but no qDebug messages befor the QApplication is instantiated and after I called the setContextProperty procedure.



  • OK I did it. As I assumed the third party librarie has a deep impact of the QDebug-Infrastructure and catches all messages after the first time the library is used.

    There was no need to configure in QtCreator or something else except in the config of these other libraries. Now I get the messages back in the IDE Application Output.

    I hope that will help other poeple finding debugging problems.