`css
qml学习笔记(二):可视化元素基类Item详解(上半场anchors等等)
本学章节笔记主要详解Item元素(上半场主要涉及anchors锚),由于全部可视化的界面元素都继承于Item,熟悉Item后,不一样的继承子类,有其定制的属性(从几个到几十个不等)。ide
《Qt实用技巧:在Qt Gui程序中嵌入qml界面(可动态覆盖整个窗口)》·
《qml学习笔记(一):界面元素初探》·学习
《qml学习笔记(三):可视化元素基类Item详解(下半场)》`测试
基类Item介绍
基类Item是全部可视化子类的父类,它不是可见的,可是它定义了全部通用元素通用的属性,好比x、y、width、height、anchoring和handingsuppourt。ui
几个Item的使用示例 spa
Image示例
- Item{
- Rectangle{
- width:1000;
- height:1000;
- color:"black";
- Image { // Image默认的背景是透明
- source:"1.png"// 相对于.qml的路径
- }
- Image{
- x:80
- y:200
- width:100
- height:100
- source:"1.png"
- }
- Image{
- x:190
- y:400
- width:100
- height:100
- fillMode:Image.Tile
- source:"1.png"
- }
- }
- }
效果以下图:3d

捕捉键盘
- Item{
- focus:true
- Keys.onPressed:{
- if(event.key==Qt.Key_Left){
- console.log("moveleft");
- event.accepted=true;
- }
- }
- Keys.onReturnPressed:
- console.log("Pressedreturn");
- }
输入处理
- Rectangle{
- width:100;
- height:100
- FocusScope{
- id:focusScope
- focus:true
- TextInput{
- id:input
- focus:true
- }
- }
- }
效果如图对象

属性详解
此属性指示元素是否具备活动焦点。若是指示是真的,这个对象是目前接收键盘输入,或是一个FocusScope为父对象的对象,目前接收键盘输入。blog
一般,此属性是经过设置焦点在其子元素(继承于Item)和其外围FocusScope对象得到。在下面的例子中,TextInput和FocusScope对象会有活跃的热点,而根矩形对象将不会。继承
anchors:一组属性,提供了以元素相互关系为基准的定位方式,主要包括如下的:
- Item {
- Image {
- id: pic;
- x:100;
- y:200;
- source: "./1.png";
- }
- Text {
- id: label;
- anchors.top: pic.bottom; // 对象的顶部是与pic的底部高度相同
- text: "hello world";
- color: "black";
- font.pointSize: 14; // 大于0的值,与设备无关font.pixelSize:单位像素,依赖于设备
- }
- }
- Item {
- Image {
- id: pic;
- x:100;
- y:200;
- source: "./1.png";
- }
- Text {
- id: label;
- anchors.bottom: pic.bottom; // 对象的顶部是与pic的底部高度相同
- text: "hello world";
- color: "black";
- font.pointSize: 14; // 大于0的值,与设备无关font.pixelSize:单位像素,依赖于设备
- }
- }
- Item {
- Image {
- id: pic;
- x:100;
- y:10;
- source: "./1.png";
- }
- Text {
- id: label;
- anchors.left: pic.right; // 对象的顶部是与pic的底部高度相同
- text: "hello world";
- color: "black";
- font.pointSize: 14; // 大于0的值,与设备无关font.pixelSize:单位像素,依赖于设备
- }
- }
下章节
《qml学习笔记(三):可视化元素基类Item详解(下半场)》`