QT之QML类型篇

来源:互联网 发布:最后一个勇敢的人知乎 编辑:程序博客网 时间:2024/05/22 00:25

最近因为项目需要,决定使用QT作为linux下的界面库开发一个转码设备的界面。因为直接和yuv数据打交道,所以需要使用OpenGL利用GPU硬件加速渲染画面,最初使用的SDL,后来又改写成glut,再后来因为要添加各种按钮、播放条、嵌入网页、动画过渡效果等需求,glut显然不能胜任了,gtk也有点过时了,决定采用QT去改写,也方便日后各种需求扩展。

QML即QT Markup Language,取名和html、xml类似,旨在一个轻量级标记性语言,用来描述界面,在程序中编程界面的时代已经过去了,以html+css方式描述界面确实简单灵活。

QML语法内建类型包括:

  • bool:Binary true/false value
  • int:Whole number, e.g. 0, 10, or -20
  • real:Number with a decimal point
  • double:Number with a decimal point, stored in double precision
  • enumeration:Named enumeration value
  • string:Free form text string
  • list:List of QML objects
  • url:Resource locator
  • var:Generic property type

有我们所熟知的bool、int、real、double、enumeration、string,也有list、url、var几个c++程序员视为非内置的

QML modules扩展类型:
如QtQuick模块中

  • date:Date value
  • point:Value with x and y attributes
  • rect:Value with x, y, width and height attributes
  • size:Value with width and height attributes

使用这些类型时,需要在qml文件头加入如下导入语句:
import QtQuick x.x(x代表版本控制号

一个简单的QML文件如下所示:

import QtQuick 2.7import QtQuick.Controls 2.0import QtQuick.Layouts 1.0ApplicationWindow {    visible: true;    width: 640;    height: 480;    title: qsTr("Hello World")}

前几行是import 导入模块语句

主体格式是
Class{
property:value;
}

这点类似于json的语法格式

id

每个对象都有唯一的id属性,同一qml文档类命名唯一,其它对象可以通过id来访问它。

列表

列表格式类似于json中的列表

Item {      children:[          Image{},          Text{}      ]  } 

以[]中括号声明,不同的是qml中的列表元素只能是对象,不能是基本类型,也是可以通过下表方式访问对应元素的。

原创粉丝点击