QML如何动态chage TextArea的颜色?

时间:2016-09-11 19:53:47

标签: qml

以下是我的测试APP的代码:

file:///opt/Qt5.7.0/5.7/gcc_64/qml/QtQuick/Controls/Private/Style.qml:52: ReferenceError: __control is not defined
file:///opt/Qt5.7.0/5.7/gcc_64/qml/QtQuick/Controls/Styles/Base/ScrollViewStyle.qml:56: ReferenceError: __control is not defined
file:///opt/Qt5.7.0/5.7/gcc_64/qml/QtQuick/Controls/Styles/Base/TextAreaStyle.qml:80: TypeError: Cannot read property 'enabled' of null
file:///opt/Qt5.7.0/5.7/gcc_64/qml/QtQuick/Controls/Styles/Base/TextAreaStyle.qml:77: TypeError: Cannot read property 'enabled' of null
file:///opt/Qt5.7.0/5.7/gcc_64/qml/QtQuick/Controls/Styles/Base/TextAreaStyle.qml:68: ReferenceError: __control is not defined
file:///opt/Qt5.7.0/5.7/gcc_64/qml/QtQuick/Controls/ScrollView.qml:354: TypeError: Cannot read property 'padding' of null
file:///opt/Qt5.7.0/5.7/gcc_64/qml/QtQuick/Controls/ScrollView.qml:353: TypeError: Cannot read property 'padding' of null
file:///opt/Qt5.7.0/5.7/gcc_64/qml/QtQuick/Controls/ScrollView.qml:352: TypeError: Cannot read property 'padding' of null
file:///opt/Qt5.7.0/5.7/gcc_64/qml/QtQuick/Controls/ScrollView.qml:351: TypeError: Cannot read property 'padding' of null
file:///opt/Qt5.7.0/5.7/gcc_64/qml/QtQuick/Controls/TextArea.qml:906: TypeError: Cannot read property '__selectionHandle' of null
file:///opt/Qt5.7.0/5.7/gcc_64/qml/QtQuick/Controls/TextArea.qml:942: TypeError: Cannot read property '__cursorHandle' of null

当我尝试运行此代码时,我收到所有这些消息:

SELECT 
    SUM(fldValue) AS 'kWh',
    CASE WHEN CONVERT(VARCHAR(8), fldDateTime, 108)='00:00:00' THEN DAY(fldDateTime)-1 ELSE DAY(fldDateTime) END AS 'Day',
    MONTH(fldDateTime) AS 'Month',
    YEAR(fldDateTime) AS 'Year'            
FROM 
    Data.[tblData]
GROUP BY    
    YEAR(fldDateTime), MONTH(fldDateTime),CASE WHEN CONVERT(VARCHAR(8), fldDateTime, 108)='00:00:00' THEN DAY(fldDateTime)-1 ELSE DAY(fldDateTime) END

ORDER BY 
    YEAR(fldDateTime), MONTH(fldDateTime), CASE WHEN CONVERT(VARCHAR(8), fldDateTime, 108)='00:00:00' THEN DAY(fldDateTime)-1 ELSE DAY(fldDateTime) END

并且没有样式应用于我的文本区域。

那么动态更改用于将消息写入TextArea的颜色的正确方法是什么?

1 个答案:

答案 0 :(得分:3)

我已经设法用这段代码做我想做的事情:

TextArea {
    id: taLog
    readOnly: true
    width: parent.width
    height: parent.height - labelTitle.height - btnTest.height - 2*v_AIR
    textFormat: TextEdit.RichText
    x: 0
    y: labelTitle.height + v_AIR
    style: TextAreaStyle{
        backgroundColor: "#000000";
    }
    font.family: "Helvetica";
    font.pointSize: 16;
    font.bold: true
    function logError(msg){
        logNew(msg,"#FF0000");
    }
    function logMessage(msg){
        logNew(msg,"#FFFFFF");
    }
    function logSuccess(msg){
        logNew(msg,"#00FF00");
    }
    function logNew(msg, color){
        msg = "<p style='color: " + color +  ";' >" + msg + "</p>"
        taLog.text = taLog.text + msg
    }
}

我基本上告诉它,文本将是HTML,我不同的消息有不同的颜色。