Qt文字简单绘制

绘制效果: 

 绘制方法 : 在onPaint信号中绘制

//绘制信号处理
        onPaint: {
            var ctx = getContext("2d");//绘图上下文
            ctx.fillStyle = "#FF00FF";//画刷样式
            ctx.font = fontMetrics.getFont();//字体
            ctx.beginPath();//开始绘制
            ctx.text(qsTr("Qt 6 Canvas Draw text output"), 20, 40);//绘制文字
            ctx.fill();//线束绘制
        }

在画布中处理onPaint信号

//画布
    Canvas{
        id: canvas
        width: parent.width //宽
        height: parent.height/2  //高
        //绘制信号处理
        onPaint: {
            var ctx = getContext("2d");//绘图上下文
            ctx.fillStyle = "#FF00FF";//画刷样式
            ctx.font = fontMetrics.getFont();//字体
            ctx.beginPath();//开始绘制
            ctx.text(qsTr("Qt 6 Canvas Draw text output"), 20, 40);//绘制文字
            ctx.fill();//线束绘制
        }
    }

绘制文字前,构造准备字体相关参数

FontMetrics {
        id: fontMetrics
        font.family: "Arial"
        font.pixelSize: 38
        font.italic: true
        font.bold: true

        function getFont() {
            var cssFont = fontMetrics.font.italic ? "italic " : "normal ";
            cssFont += fontMetrics.font.bold ? "bold " : "normal ";
            cssFont += (fontMetrics.font.pixelSize+"px ");
            cssFont += fontMetrics.font.family;
            return cssFont;
        }
    }

完整源码:

import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15

ApplicationWindow {
    visible: true
    width: 800
    height: 600
    title: qsTr("Qt基于Qml的Canvas文字绘制")
    //画布
    Canvas{
        id: canvas
        width: parent.width //宽
        height: parent.height/2  //高
        //绘制信号处理
        onPaint: {
            var ctx = getContext("2d");//绘图上下文
            ctx.fillStyle = "#FF00FF";//画刷样式
            ctx.font = fontMetrics.getFont();//字体
            ctx.beginPath();//开始绘制
            ctx.text(qsTr("Qt 6 Canvas Draw text output"), 20, 40);//绘制文字
            ctx.fill();//线束绘制
        }
    }

    Text{
        id: text
        anchors.left: parent.left
        anchors.leftMargin: 20
        anchors.top: parent.top
        anchors.topMargin: 60
        font: fontMetrics.font
        color: "red"
        text: qsTr("Qt6 Qml Text Component Output text")
    }

    FontMetrics {
        id: fontMetrics
        font.family: "Arial"
        font.pixelSize: 38
        font.italic: true
        font.bold: true

        function getFont() {
            var cssFont = fontMetrics.font.italic ? "italic " : "normal ";
            cssFont += fontMetrics.font.bold ? "bold " : "normal ";
            cssFont += (fontMetrics.font.pixelSize+"px ");
            cssFont += fontMetrics.font.family;
            return cssFont;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

自由软件开发者

有你的鼓励,我会更加努力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值