1. 程式人生 > >【QT】QML型別

【QT】QML型別

1.QML語言內建的基本型別QML物件型別可以用來宣告一個物件,這個物件可以包含一些屬性、訊號、函式等,但QML基本型別則不同,不能用來宣告一個物件,它表示了屬性值的型別,可用於自定義屬性。int:整數,可以是負數、0、正數。bool:true/false。real:實數,有小數點。double:雙精度實數,有小數點。string:字串,有個length屬性,儲存了字串的長度,函式“string.arg(value)”的使用有點像C++中的佔位符,如下程式碼輸出“Thereare 20 items”。var message = "There are %1 items"var count = 20console.log(message.arg(count))
url:資源路徑,可以是絕對路徑或相對路徑,相對路徑時就會被轉換為URL物件,如果使用了Qt資源系統,就要用“qrc:///”代替“:/”,如果檔名包含了“#”特殊字元,就要用到encodeURIComponent()函數了。Image { source: "pics/logo.png" // source: "qrc:///pics/logo.png" // source: encodeURIComponent("/pics/logo#1.png") Component.onCompleted: { // This prints 'false'. Although "pics/logo.png" was the input string,
// it's been converted from a string to a URL, so these two are not the same. console.log(source == "pics/logo.png") // This prints 'true' as Qt.resovledUrl() converts the string into a // URL with the correctly resolved path console.log(source == Qt.resolvedUrl("pics/logo.png"))
// This prints the absolute path, e.g. "file:///path/to/pics/logo.png" console.log(source.toString()) }}list:QML物件列表,length屬性儲存了其物件個數,支援“[index]”下標訪問,如果是自定義列表屬性的話最好用var代替list,var用起來更靈活一些。var:泛型,支援所有的資料型別。enumeration:列舉,必須在註冊過的QML物件型別中定義了才可以使用。2.QtQuick模組提供的基本型別color:顏色,可以是SVG顏色名字如“red”、十六進位制形如“#RRGGBB”或“#AARRGGBB”、函式如Qt.rgba()等。font:字型。matrix4x4:4行4列矩陣。quaternion:四元數,有scalar、x、y、z。vector2d:x,y。vector3d:x,y,z。vector4d:x,y,z,w。date:日期。point:點,x和y。size:width和height。rect:x,y,width,height。3.QML物件型別Date:日期,定義了一些設定日期、時間格式的函式。Number:數字,定義了一些設定數字格式的函式。String:字串,有個函式“stringarg(value)”,value用於替換佔位符中的內容。Component:封裝了QML元件的定義。QtObject:只有一個“objectName”屬性,是C++訪問QML的介面。Qt:全域性物件,提供了一些有用的函式。Locale:環境場所設定,通過Qt.locale()函式建立。Binding:屬性繫結。Connections:連線訊號。Instantiator:動態建立物件。Timer:定時器。4.自定義QML型別我們也可以自定義QML型別,最簡單的辦法就是直接使用首字母大寫的QML檔名,也可以通過C++來定義。