qml中text显示不同颜色或大小的文字

运行效果:
在这里插入图片描述

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

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值