QML: How to display certain parts of Text in different colors

222 Views Asked by At

I have the following QML Text embedded inside a Rectangle

Rectangle {
    id: textContainer
    border.color: "black"

    Text {
        id: myTextView
        anchors.fill: textContainer
        anchors.margins: 10
        font.pointSize: 4
        wrapMode: Text.WordWrap

        Connections {
            target: cpp_controller
            onUpdateCalledFromCppCode: {
                myTextView.text = message + myTextView.text
            }
        }
    }
}

onUpdateCalledFromCppCode gets called like twice or thrice in a second which updates myTextView::text field. This works well and good.

Question:
Now, I want to display certain words/lines in the text in certain colors, given whatever is possible within QML. Like lets say, in one word in the variable message coming in onUpdateCalledFromCppCode says "Error", then I want to display that word or that whole line in Red. Is that possible in QML? How can I do that?

Environment:
I am am using commercial version of Qt 5.12.5

0

There are 0 best solutions below