当前位置:网站首页>QML type system
QML type system
2022-07-27 00:28:00 【Mango black】
We all know ,Qt Quick It provides another way of interface development , differ Widget and Graphics Use C++ Development ,Qt Quick It's using QML.QML Is a descriptive scripting language , The file format is .qml ending , The syntax format is very similar to CSS, Support javascript Form of programming control .
What we know C++ Basic data types for ,int、bool、double etc. , Composite type structure 、 Class etc. , Relative QML Since it's a programming language , Naturally, it also has its own set of data types .
QML There are mainly the following types of sources :
- QML Its own basic data type
- Registered C++ type
- qml The custom type provided by the file
- JS data type
Basic data type
QML Basic data types supported by itself
Binary true/false value | |
Number with a decimal point, stored in double precision | |
Named enumeration value | |
Whole number, e.g. 0, 10, or -20 | |
List of QML objects | |
Number with a decimal point | |
Free form text string | |
Resource locator | |
Generic property type |
among bool、double、int、real、string and C++ The basic types in are basically the same , There's no need to introduce it too much
enumeration( enumeration )
Enumeration and C++ The enumeration type in is the same , Is a set containing several constants , Often used to indicate status , Improve code readability , Support conversion int type
The following is a rectangle of the package , Use enum Express 2 The medium state corresponds to different colors
import QtQuick 2.0
Rectangle {
width: 100
height: 100
enum RectType {
Type1,
Type2
}
property int testType: MyRect.RectType.Type1
color: testType === MyRect.RectType.Type1 ? "red" : "blue"
}
MyRect {
testType: MyRect.RectType.Type1
}url
url Type refers to a resource locator ( for example , Like a file name ). It can be absolute , for example “http://qt-project.org”, It can also be relative , for example “pics/logo.png”. relative URL Is relative to containing components URL To analyze
Image { source: "pics/logo.png" }At first glance , I thought source yes string Type? , In fact, there are still differences , You can see the following examples and explanations
Image {
source: "pics/logo.png"
Component.onCompleted: {
// source Type cannot match string Compare
console.log(source == "pics/logo.png")
// practical Qt.resovledUrl() hold string Turn into source type
console.log(source == Qt.resolvedUrl("pics/logo.png"))
// Print absolute path "file:///path/to/pics/logo.png"
console.log(source.toString())
}
}
var( Generic type )
Same as js equally , Support var Common types of , The definition of type in scripting language is relatively loose , Generally, dynamic type checking is automatically performed at runtime . and C++ Such compiled languages are strongly typed or statically defined , The type of variable needs to be specified in the program .
property var aNumber: 100
property var aBool: false
property var aString: "Hello world!"
property var anotherString: String("#FF008800")
property var aColor: Qt.rgba(0.2, 0.3, 0.4, 0.5)
property var aRect: Qt.rect(10, 10, 10, 10)
property var aPoint: Qt.point(10, 10)
property var aSize: Qt.size(10, 10)
property var aVector3d: Qt.vector3d(100, 100, 100)
property var anArray: [1, 2, 3, "four", "five", (function() { return "six"; })]
property var anObject: { "foo": 10, "bar": 20 }
property var aFunction: (function() { return "one"; })You can see ,var Many data types can be referenced
But in use Qt Creator When , Many types will report warnings , Warning it is recommended to specify the type instead of var, Specifically, I don't know why , Of course, it's OK to use , It is annoying to prompt various warnings

QML The composite type provided by the module
QML The module provides a pair of composite types , It's basically benchmarking Qt Supported built-in types , Such as size、rect、color、font、point、rect etc.
Date value | |
Value with x and y attributes | |
Value with x, y, width and height attributes | |
Value with width and height attributes |
Qt The composite type provided can be through Qt Construct global objects
property var aColor: Qt.rgba(0.2, 0.3, 0.4, 0.5)
property var aRect: Qt.rect(10, 10, 10, 10)
property var aPoint: Qt.point(10, 10)
property var aSize: Qt.size(10, 10)QML object type
QML An object type is one that can be instantiated QML Type of object , As we know Rectangle.QML Object types can be registered C++ type , You can also create a new .qml File generation
adopt qml File defines QML object type
This is a very common usage , This is the way that controls are encapsulated , The file name is the type name , The file name defined in the above enumerated type example is MyRect.qml, When used, the type is MyRect
The naming of files should follow certain rules :
- Must consist of alphanumeric characters or underscores
- Must start with a capital letter
The engine will automatically recognize this document as QML The definition of type , When the engine is parsing QML When searching for a type name in the sibling directory , Types defined in this way can be used by others in the same directory QML file .
from C++ Define object types
This method is the registration introduced before C++ type , Inherited from QObject Class registered to QML In the system , It can be like ordinary QML The same type is used , To be specific, see here
JS type
QML Engine support JavaScript Objects and arrays . Any standard JavaScript All types can use general var Type to create and store .
import QtQuick 2.0
Item {
property var theArray: []
property var theDate: new Date()
Component.onCompleted: {
for (var i = 0; i < 10; i++)
theArray.push("Item " + i)
console.log("There are", theArray.length, "items in the array")
console.log("The time is", theDate.toUTCString())
}
}Property change behavior of basic types
When the data changes , Use on+ name +Changed The slot function of can listen for data changes , This feature is very practical ,QML no C++ That's convenient for debugging , With this method, it is more convenient to track the changes of data , Convenient debugging .
property int aNumber: 100
onANumberChanged: {
console.log("aNumber changed")
}We need to pay attention to , Whenever any attribute of the basic type changes and the attribute itself changes , Will send a basic type of property change signal . Take the following code for example :
Text {
onFontChanged: console.log("font changed")
Text { id: otherText }
focus: true
// as long as font Any change in the value of an attribute of will trigger onFontChanged
Keys.onDigit1Pressed: font.pixelSize += 1
Keys.onDigit2Pressed: font.b = !font.b
Keys.onDigit3Pressed: font = otherText.font
}Reference resources :
边栏推荐
- Deep learning of parameter adjustment skills
- 三层架构 模拟
- 生成yolov5.wts文件出错
- Double. isNaN(double var)
- 输入一串字母 将里面的元音输出希望各位大佬能给指导
- Class and object notes I
- Pyautogui usage example
- [Qt]解决中文乱码问题
- C and pointers Chapter 18 runtime environment 18.4 summary
- Based on the theoretical principle and simulation results of MATLAB spherical decoding, compare 2norm spherical decoding, infinite norm spherical decoding, ML detection
猜你喜欢

Knowledge distillation -- pytorch implementation

三层架构 模拟

7_主成分分析法(Principal Component Analysis)

CSDN文章语法规则

查看 Anaconda 创建环境的位置

Shang school software testing (1) software testing curriculum system, advantages, learning suggestions, understanding software, software testing and defects, software testing process, debugging and te

C语言 求素数、闰年以及最小公倍数最大公约数

3_Jupyter Notebook, numpy和matplotlib

deeplabcut使用1

爬虫解析网页的find方法
随机推荐
Deeplabcut uses 1
Codeforces d.constructing the array (priority queue)
The crawler parses the object of the web page. Element name method
Recbole use 1
PTA 7-3 lists leaf nodes
C and pointer Chapter 18 runtime environment 18.2 interface between C and assembly language
Comparative simulation of LEACH protocol performance, including the number of dead nodes, data transmission, network energy consumption, the number of cluster heads and load balance
解析网页的完整回顾
[Qt]元对象系统
Drawing warehouse-2 (function image)
继承,继承,继承
TypeScript(tsconfig.json)
Today's 20220719 toss deeplobcut
13_ Ensemble learning and random forests
Complete review of parsing web pages
放图仓库-Tsai
机器学习模型——lightGBM
Resolve Microsoft 365 and Visio conflicts
Huffman encoding and decoding
CSDN article syntax rules