运行效果:
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Layouts 1.12
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
ColumnLayout {
Text {
text: "测试字体颜色" + '<font color="#D3D3D3"">测试</font>'
}
Text {
text: "测试字体大小" + '<font size="5">测试</font>'
}
Text {
text: "测试字体颜色" + '<font color="green">测试</font>'
}
}
}
在listView中要显示不同颜色的文字,这里写了个简单的demo
代码:
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.0
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
ColumnLayout {
Frame {
ListView {
id: listViewId
implicitWidth: 250
implicitHeight: 250
clip: true
model: ListModel {
Component.onCompleted: {
append({"description": "Wash the car" + '<font color="#D3D3D3"">测试</font>'});
append({"description": "123" + '<font color="#98FB98"">测试</font>'});
append({"description": "345" + '<font color="#FF00FF"">测试</font>'});
append({"description": "567" + '<font color="#FFB6C1"">测试</font>'});
}
}
delegate: RowLayout {
width: parent.width
Text {
text: model.description
}
}
}
} //end Frame
}
}
运行效果:
如果想用
参考:
https://zhidao.baidu.com/question/394935330372450245.html
QML:ListElement: cannot use script for property value解决方法:
https://blog.csdn.net/qq_33425353/article/details/82777303