2016-09-11 10 views
2

Hier ist der Code für meinen Test APP:QML Wie ändert man die Farbe einer TextArea dynamisch?

TextAreaStyle{ 
    id: loggerStyle 
    textColor: "#00ff88"; 
    backgroundColor: "#000000"; 
} 

TextArea { 
    id: taLog 
    readOnly: true 
    width: parent.width 
    height: parent.height - labelTitle.height - btnTest.height - 2*v_AIR 
    x: 0 
    y: labelTitle.height + v_AIR 
    style: loggerStyle 
    font.family: "Helvetica"; 
    font.pointSize: 16; 
    font.bold: true 
    function logError(msg){ 
     loggerStyle.textColor = "#FF0000" 
     taLog.text = taLog.text + "\n" + msg 
    } 
    function logMessage(msg){ 
     loggerStyle.textColor = "#FFFFFF" 
     taLog.text = taLog.text + "\n" + msg 
    } 
    function logSuccess(msg){ 
     loggerStyle.textColor = "#00FF00" 
     taLog.text = taLog.text + "\n" + msg 
    } 
} 

Wenn ich versuche, diesen Code ich alle diese Meldungen erhalten laufen:

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 

Und kein Stil meiner Textbereich angewendet wird.

Also, was ist der richtige Weg, um die Farbe dynamisch zu ändern, die ich zum Schreiben von Nachrichten in eine TextArea verwende?

+0

Warum funktioniert die direkte Änderung ['textColor'] (http://doc.qt.io/qt-5/qml-qtquick-controls-textarea.html#textColor-prop) nicht für Sie? – skypjack

+0

Es tut. Wie auch immer ich gelesen habe, wenn ich eine TextArea gestylt habe, sollte ich TextAreaStyle verwenden, also habe ich das benutzt. Sowieso. Ändern von textColor ändert die Farbe für den gesamten Text. Es stellte sich heraus, HMTL Rich Text war die Antwort. – aarelovich

Antwort

2

habe ich es geschafft, zu tun, was ich mit diesem Code möchte:

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

Ich sagte es basially, dass der Text HTML und verschiedenen Farben die verschiedenen Meldungen haben, dass ich sein würde.

Verwandte Themen