QML console.log() 和 console.debug() 不写入控制台

QML console.log() and console.debug() don't write to console

我在 Fedora 23 上使用 Qt 5.6,我注意到 console.log()console.debug() 没有向控制台写入任何内容。我的示例代码:

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")
        }
    }
}

打印到控制台:

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

所以 warnerrorexceptioninfo 工作正常。我做错了什么?

编辑 #1: 项目是新创建的,我的所有来源:

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)

编辑#2: Qt Creator 的编译输出显示没有 QT_NO_DEBUG_OUTPUTQT_NO_INFO_OUTPUTQT_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.

Fedora 22 及更高版本默认禁用 Qt 调试输出 [1]。您可以通过修改系统范围的 /etc/xdg/QtProject/qtlogging.ini 或创建用户特定的配置文件 ~/.config/QtProject/qtlogging.ini 来启用 Qt 调试输出,例如具有以下内容:

[Rules]
*.debug=true
  1. https://bugzilla.redhat.com/show_bug.cgi?id=1227295

我通过添加两者之一找到了更方便的解决方案 - 取决于 如果您使用 Qt Quick 1 或 2 到 pro 文件(重建并再次 运行 QMake) 根据 https://doc.qt.io/qt-5/qtquick-debugging.html:

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

将其放入您的代码中,它看起来像:

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)

现在它可能看起来像这样:

而不是像 jpnurmi. You can simply set a system variable to get the debug messages to show up in the console. Qt Wiki about Logging Rules

中建议的那样在整个系统范围内启用调试日志记录

将其设置为一次执行您的应用程序或 qmlscene/qhot qml-preview。如果您从命令行启动它们:

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

或者仅为本次会话设置(重启后将取消设置)。即使您来自 QtCreator 或其他 IDE:

launch/test,也应该受到尊重
$ export QT_LOGGING_RULES="*.debug=true; qt.*.debug=false"

然后 start/test 您的可执行文件或 qmlscene/qhot 通常

为什么这样更好?

在系统范围内设置日志记录规则将意味着您 运行 的所有 Qt 应用程序都会将调试消息写入它们的日志文件。我发现了一些大小为 GB 的日志文件,因为这些应用程序在正常使用期间在其日志中积累了大量调试消息。