当前位置:网站首页>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 :
边栏推荐
- Anaconda => PyCharm => CUDA => cudnn => PyTorch 环境配置
- Knowledge distillation -- pytorch implementation
- Uni app learning (II)
- 用New,delete和用malloc,free申请,释放堆区空间
- Arcgis和Cass实现断面展高程点
- C and pointers Chapter 18 runtime environment 18.4 summary
- [LeetCode] 无重复最长字符串
- C and pointers Chapter 18 runtime environment 18.8 programming exercises
- [Qt]元对象系统
- 12_决策树(Decision tree)
猜你喜欢

RESNET paper interpretation and code implementation (pytorch)

13_ Ensemble learning and random forests

Complete review of parsing web pages

CDs simulation of minimum dominating set based on MATLAB

滑动窗口问题总结

信号与系统学习零输入响应

20220720折腾deeplabcut2

放图仓库-Tsai

Request attribute in crawler

Reduced dimension mean dot product matrix multiplicative norm probability normal distribution square loss
随机推荐
C and pointer Chapter 18 runtime environment 18.7 problems
AutoCAD的卸载后重新安装,删除注册表的详细过程
Complete review of parsing web pages
深度学习调参技巧
Anaconda = > pycharm=> CUDA=> cudnn=> pytorch environment configuration
Codeforces C1. Simple Polygon Embedding
"Could not load host key" error when xshell connects to the server
AlexNet(Pytorch实现)
用New,delete和用malloc,free申请,释放堆区空间
Shufflenet series (2): explanation of shufflenet V2 theory
蒙着头配置deeplabcut2
Deeplabcut uses 1
deeplabcut使用1
4. Talk about the famous Zhang Zhengyou calibration method
Error generating yolov5.wts file
C语言 关机小程序
Fourier analysis (basic introduction)
[Qt]解决中文乱码问题
[Qt]容器类、迭代器、foreach关键字
生成yolov5.wts文件出错