File tree Expand file tree Collapse file tree 7 files changed +115
-1
lines changed Expand file tree Collapse file tree 7 files changed +115
-1
lines changed Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ <RCC>
2
+ <qresource prefix="/">
3
+ <file>main.qml</file>
4
+ </qresource>
5
+ </RCC>
Original file line number Diff line number Diff line change @@ -32,4 +32,5 @@ SUBDIRS += QmlFlipImage
32
32
SUBDIRS += QmlGrayImage
33
33
SUBDIRS += QmlProgress
34
34
SUBDIRS += QmlWinExtras
35
- SUBDIRS += QmlCalendar
35
+ SUBDIRS += QmlCalendar
36
+ SUBDIRS += QmlCanvasText
Original file line number Diff line number Diff line change @@ -147,3 +147,8 @@ QmlCalendar:Qml日历
147
147
148
148
![ ] ( https://github.com/zhengtianzuo/QtQuickExamples/blob/master/QmlCalendar/show.gif?raw=true )
149
149
150
+
151
+ QmlCanvasText: QmlCanvas文字
152
+
153
+ ![ ] ( https://github.com/zhengtianzuo/QtQuickExamples/blob/master/QmlCanvasText/show.jpg?raw=true )
154
+
You can’t perform that action at this time.
0 commit comments