Skip to content

Commit ece3e48

Browse files
committed
Add QmlCanvasText
1 parent 0f670c1 commit ece3e48

File tree

7 files changed

+115
-1
lines changed

7 files changed

+115
-1
lines changed

QmlCanvasText/QmlCanvasText.pro

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#-------------------------------------------------
2+
#
3+
# Copyright (C) 2003-2103 CamelSoft Corporation
4+
#
5+
#-------------------------------------------------
6+
7+
QT += qml quick
8+
9+
CONFIG += c++11
10+
11+
SOURCES += main.cpp
12+
13+
RESOURCES += qml.qrc

QmlCanvasText/main.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*!
2+
*@file main.cpp
3+
*@brief 程序主文件
4+
*@version 1.0
5+
*@section LICENSE Copyright (C) 2003-2103 CamelSoft Corporation
6+
*@author zhengtianzuo
7+
*/
8+
#include <QGuiApplication>
9+
#include <QQmlApplicationEngine>
10+
11+
int main(int argc, char *argv[])
12+
{
13+
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
14+
QGuiApplication app(argc, argv);
15+
16+
QQmlApplicationEngine engine;
17+
engine.load(QUrl(QLatin1String("qrc:/main.qml")));
18+
19+
return app.exec();
20+
}

QmlCanvasText/main.qml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*!
2+
*@file main.qml
3+
*@brief 主文件
4+
*@version 1.0
5+
*@section LICENSE Copyright (C) 2003-2103 CamelSoft Corporation
6+
*@author zhengtianzuo
7+
*/
8+
import QtQuick 2.7
9+
import QtQuick.Controls 2.0
10+
import QtQuick.Layouts 1.0
11+
12+
ApplicationWindow {
13+
visible: true
14+
width: 400
15+
height: 300
16+
title: qsTr("QmlCanvas文字")
17+
18+
Canvas{
19+
id: canvas
20+
width: 300
21+
height: 100
22+
onPaint: {
23+
var ctx = getContext("2d");
24+
ctx.fillStyle = "red";
25+
ctx.font = fontMetrics.getFontToContext2D();
26+
27+
ctx.beginPath();
28+
ctx.text(qsTr("Canvas 这是一段文字"), 20, 40);
29+
ctx.fill();
30+
}
31+
}
32+
33+
Text{
34+
id: text
35+
anchors.left: parent.left
36+
anchors.leftMargin: 20
37+
anchors.top: parent.top
38+
anchors.topMargin: 60
39+
font: fontMetrics.font
40+
color: "red"
41+
text: qsTr("Text 这是一段文字")
42+
}
43+
44+
FontMetrics {
45+
id: fontMetrics
46+
font.family: "Arial"
47+
font.pixelSize: 16
48+
font.italic: false
49+
font.bold: false
50+
51+
function getFontToContext2D() {
52+
var cssFontString = "";
53+
if(fontMetrics.font.italic) {
54+
cssFontString += "italic ";
55+
} else {
56+
cssFontString += "normal ";
57+
}
58+
59+
if(fontMetrics.font.bold) {
60+
cssFontString += "bold ";
61+
} else {
62+
cssFontString += "normal ";
63+
}
64+
65+
cssFontString += (fontMetrics.font.pixelSize+"px ");
66+
cssFontString += fontMetrics.font.family;
67+
return cssFontString;
68+
}
69+
}
70+
}

QmlCanvasText/qml.qrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<RCC>
2+
<qresource prefix="/">
3+
<file>main.qml</file>
4+
</qresource>
5+
</RCC>

QmlCanvasText/show.jpg

17 KB
Loading

QtQuickExamples.pro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@ SUBDIRS += QmlFlipImage
3232
SUBDIRS += QmlGrayImage
3333
SUBDIRS += QmlProgress
3434
SUBDIRS += QmlWinExtras
35-
SUBDIRS += QmlCalendar
35+
SUBDIRS += QmlCalendar
36+
SUBDIRS += QmlCanvasText

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,8 @@ QmlCalendar:Qml日历
147147

148148
![](https://github.com/zhengtianzuo/QtQuickExamples/blob/master/QmlCalendar/show.gif?raw=true)
149149

150+
151+
QmlCanvasText: QmlCanvas文字
152+
153+
![](https://github.com/zhengtianzuo/QtQuickExamples/blob/master/QmlCanvasText/show.jpg?raw=true)
154+

0 commit comments

Comments
 (0)