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

时间:2016-03-31 15:45:14

标签: qt qml fedora

我在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.

3 个答案:

答案 0 :(得分:13)

Fedora 22及更高版本默认禁用Qt调试输出[1]。您可以通过修改系统范围的<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery Show Hide Using Select Box</title> <style type="text/css"> .box{ padding: 20px; display: none; margin-top: 20px; border: 1px solid #000; } .red{ background: #ff0000; } .green{ background: #00ff00; } .blue{ background: #0000ff; } </style> <script type="text/javascript" src="http://code.jquery.com/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#first").change(function(){ $(this).find("option:selected").each(function(){ if($(this).attr("value")=="red"){ $(".box").not(".red").hide(); $(".red").show(); } else if($(this).attr("value")=="green"){ $(".box").not(".green").hide(); $(".green").show(); } else if($(this).attr("value")=="blue"){ $(".box").not(".blue").hide(); $(".blue").show(); } else{ $(".box").hide(); } }); }).change(); }); </script> </head> <body> <div> <select id="first"> <option>Choose Color</option> <option value="red">Red</option> <option value="green">Green</option> <option value="blue">Blue</option> </select> </div> <div class="red box"> <p> <select id="second"> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="mercedes">Mercedes</option> <option value="audi">Audi</option> </select></p> First name: <input type="text" name="fname"><br> Last name: <input type="text" name="lname"><br> </div> <div class="green box">You have selected <strong>green option</strong> so i am here</div> <div class="blue box">You have selected <strong>blue option</strong> so i am here</div> </body> </html>或创建用户特定的配置文件/etc/xdg/QtProject/qtlogging.ini来启用Qt调试输出,例如,包含以下内容:

~/.config/QtProject/qtlogging.ini
  1. https://bugzilla.redhat.com/show_bug.cgi?id=1227295

答案 1 :(得分:1)

我发现了一种更方便的解决方案,方法是将两者之一相加 如果您对专业文件使用Qt Quick 1或2,请打开(重新生成并再次运行QMake) 根据{{​​3}}:

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)

现在看起来可能像这样: https://doc.qt.io/qt-5/qtquick-debugging.html

答案 2 :(得分:0)

而不是像 jpnurmiaccepted answer 中建议的那样在系统范围内启用调试日志记录。您可以简单地设置一个系统变量来让调试消息显示在控制台中。 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 启动/测试也应该受到尊重:

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

然后正常启动/测试您的可执行文件或 qmlscene/qhot

为什么这更好?

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