22

I'm using Qt 5.6 on Fedora 23 and I noticed that console.log() and console.debug() don't write anything to console. My example code:

import QtQuick 2.6
import QtQuick.Window 2.2

Window {
    visible: true

    Text {
        text: qsTr("Hello World")
        anchors.centerIn: parent

        Component.onCompleted: {
            console.warn("warn completed")
            console.log("log completed")
            console.error("error completed")
            console.debug("debug completed")
            console.exception("exception completed")
            console.info("info completed")
        }
    }
}

prints to console:

QML debugging is enabled. Only use this in a safe environment.
qml: warn completed
qml: error completed
qml: exception completed
onCompleted (qrc:/main.qml:16)
qml: info completed

so warn, error, exception, and info work fine. What am I doing wrong?

Edit #1: Project is freshly created, all my sources:

main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>

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

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

project.pro

TEMPLATE = app

QT += qml quick
CONFIG += c++11

SOURCES += main.cpp

RESOURCES += qml.qrc

# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =

# Default rules for deployment.
include(deployment.pri)

Edit #2: Compile Output from Qt Creator shows that there are no QT_NO_DEBUG_OUTPUT, QT_NO_INFO_OUTPUT, or QT_NO_WARNING_OUTPUT:

14:43:36: Running steps for project project...
14:43:36: Configuration unchanged, skipping qmake step.
14:43:36: Starting: "/usr/bin/make" 
g++ -c -pipe -g -std=gnu++0x -Wall -W -D_REENTRANT -fPIC -DQT_QML_DEBUG -DQT_QUICK_LIB -DQT_GUI_LIB -DQT_QML_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -I../project -I. -I../../Qt5.6.0/5.6/gcc_64/include -I../../Qt5.6.0/5.6/gcc_64/include/QtQuick -I../../Qt5.6.0/5.6/gcc_64/include/QtGui -I../../Qt5.6.0/5.6/gcc_64/include/QtQml -I../../Qt5.6.0/5.6/gcc_64/include/QtNetwork -I../../Qt5.6.0/5.6/gcc_64/include/QtCore -I. -I../../Qt5.6.0/5.6/gcc_64/mkspecs/linux-g++ -o main.o ../project/main.cpp
/home/krzys/Qt5.6.0/5.6/gcc_64/bin/rcc -name qml ../project/qml.qrc -o qrc_qml.cpp
g++ -c -pipe -g -std=gnu++0x -Wall -W -D_REENTRANT -fPIC -DQT_QML_DEBUG -DQT_QUICK_LIB -DQT_GUI_LIB -DQT_QML_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -I../project -I. -I../../Qt5.6.0/5.6/gcc_64/include -I../../Qt5.6.0/5.6/gcc_64/include/QtQuick -I../../Qt5.6.0/5.6/gcc_64/include/QtGui -I../../Qt5.6.0/5.6/gcc_64/include/QtQml -I../../Qt5.6.0/5.6/gcc_64/include/QtNetwork -I../../Qt5.6.0/5.6/gcc_64/include/QtCore -I. -I../../Qt5.6.0/5.6/gcc_64/mkspecs/linux-g++ -o qrc_qml.o qrc_qml.cpp
g++ -Wl,-z,origin -Wl,-rpath,\$ORIGIN -Wl,-rpath,/home/krzys/Qt5.6.0/5.6/gcc_64/lib -o project main.o qrc_qml.o   -L/home/krzys/Qt5.6.0/5.6/gcc_64/lib -lQt5Quick -lQt5Gui -lQt5Qml -lQt5Network -lQt5Core -lGL -lpthread 
14:43:37: The process "/usr/bin/make" exited normally.
14:43:37: Elapsed time: 00:01.
jpnurmi
  • 5,716
  • 2
  • 21
  • 37
  • Did you build in release or debug mode? info/debug are translated to qDebug, I think, which can be disabled when built in release mode. – Frank Osterfeld Mar 31 '16 at 20:41
  • According to the documentation, `qDebug()`, `qInfo()`, and `qWarning()` are debugging tools. They can be compiled away by defining `QT_NO_DEBUG_OUTPUT`, `QT_NO_INFO_OUTPUT`, or `QT_NO_WARNING_OUTPUT` during compilation. – Tarod Apr 01 '16 at 06:46
  • @FrankOsterfeld: my Qt Creator (3.6.1) offers three builds: Debug, Profile, Release, and I tried all three with the same outcome. @Tarod: I used freshly created project and I didn't add any custom defines to sources or `project.pro` file. Question is edited to include all the sources. – Krzysztof Piekutowski Apr 01 '16 at 11:20
  • It looks like you're using a prebuilt Qt, which are always release builds. The Debug and Release build modes in Creator only change what *your project* is built with, not Qt itself. I don't think any of that matters though, as I do get the log/debug output with a prebuilt Qt. I would suggest building Qt yourself and stepping into the sources to see what's happening. – Mitch Apr 01 '16 at 16:52

4 Answers4

16

Fedora 22 and later disables Qt debug output by default [1]. You can enable Qt debug output by modifying the system-wide /etc/xdg/QtProject/qtlogging.ini or by creating a user-specific configuration file ~/.config/QtProject/qtlogging.ini for example with the following contents:

[Rules]
*.debug=true
  1. https://bugzilla.redhat.com/show_bug.cgi?id=1227295
jpnurmi
  • 5,716
  • 2
  • 21
  • 37
  • 5
    Thanks for pointing me to the bug report, description and comments under it explain whole problem. Rule `*.debug=true` alone enabled tons of QT library logs, but adding `qt.*.debug=false` fixed it. tldr; configuration below saved in `~/.config/QtProject/qtlogging.ini` fixed the debug problem. `[Rules] *.debug=true; qt.*.debug=false` – Krzysztof Piekutowski Jun 27 '16 at 17:44
2

I found a more convenient solution by adding one of the two - depending on if you use Qt Quick 1 or 2 to the pro file (rebuild and run QMake again) according to https://doc.qt.io/qt-5/qtquick-debugging.html:

Qt Quick 1: CONFIG+=declarative_debug
Qt Quick 2: CONFIG+=qml_debug

To put it in your code it would look like:

project.pro


TEMPLATE = app

QT += qml quick
CONFIG += c++11 qml_debug #or CONFIG+=declarative_debug for QtQuick 1

SOURCES += main.cpp

RESOURCES += qml.qrc

# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =

# Default rules for deployment.
include(deployment.pri)

Now it might look like this: enter image description here

Ingo Mi
  • 999
  • 12
  • 26
1

Instead of enabling Debug logging system wide like jpnurmi suggests in the accepted answer. You can simply set a system variable to get the debug messages to show up in the console. Qt Wiki about Logging Rules

To set it for one single execution of your app or qmlscene/qhot qml-preview. If you are starting them from the commandline:

$ QT_LOGGING_RULES="*.debug=true; qt.*.debug=false" your_executable
$ QT_LOGGING_RULES="*.debug=true; qt.*.debug=false" qmlscene/qhot main.qml

Or to set it for this session only (will be unset after reboot). And should be respected even if you launch/test from QtCreator or other IDE:

$ export QT_LOGGING_RULES="*.debug=true; qt.*.debug=false"

And then just start/test your executable or qmlscene/qhot normally

Why is this better?

Setting the logging rules systemwide will mean all Qt apps you run will write debug messages to their logfiles. I discovered a few log files that were gigabytes in size because the apps were accumulating a huge amount debug messages in their logs during normal usage.

Nidus
  • 56
  • 4
0

If you are working in a project that uses a tool like log4cplus the configure loggers, the console.x functions will be filtered through that tool as well as part of the Qt logger. So if logging is configured to only log error level messages you will have to use console.error() for the message to make it to the console.

Adam
  • 98
  • 1
  • 7