【发布时间】:2016-07-20 04:16:48
【问题描述】:
我在 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
所以warn、error、exception 和 info 工作正常。我做错了什么?
编辑#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();
}
项目.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_OUTPUT、QT_NO_INFO_OUTPUT 或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.
【问题讨论】:
-
你是在发布模式还是调试模式下构建的? info/debug 被翻译成 qDebug,我认为,在发布模式下可以禁用。
-
根据文档,
qDebug()、qInfo()和qWarning()是调试工具。它们可以通过在编译期间定义QT_NO_DEBUG_OUTPUT、QT_NO_INFO_OUTPUT或QT_NO_WARNING_OUTPUT来编译掉。 -
@FrankOsterfeld:我的 Qt Creator (3.6.1) 提供了三个版本:Debug、Profile、Release,我尝试了所有三个版本,结果相同。 @Tarod:我使用了新创建的项目,我没有向源或
project.pro文件添加任何自定义定义。对问题进行了编辑以包含所有来源。 -
看起来您使用的是预构建的 Qt,它始终是发布版本。 Creator 中的 Debug 和 Release 构建模式只会改变 您的项目 的构建方式,而不是 Qt 本身。不过,我认为这些都不重要,因为我确实使用预构建的 Qt 获得了日志/调试输出。我建议自己构建 Qt 并进入源代码以查看发生了什么。