分享

QML之可视化元素

 t涂鸦 2012-02-21

1、Item

      所有的QML可视化元素都继承自Item。Item没有可视化界面,但是它定义了可视化元素的所有属性。

   

  1. import Qt 4.7  
  2. Item{  
  3.     width500;height400  
  4.     Image{  
  5.         source: "images/qt.png"  
  6.     }  
  7.     Image {  
  8.         x: 80  
  9.         y: 100  
  10.         source: "images/qt.png"  
  11.     }  
  12.     Image {  
  13.         x: 190  
  14.         width100;height100  
  15.         fillMode: Image.Tile  
  16.         source: "images/qt.png"  
  17.     }  
  18. }  
  19. //记住要为Item指定width和height不然运行看不到窗体  

 

    按键事件:

  1. Item {  
  2.     width200height100  
  3.     focus: true  
  4.     Keys.onPressed: {  
  5.         console.log(event.key);  
  6.         console.log(Qt.Key_Left);  
  7.         if(event.key == Qt.Key_Left){  
  8.             console.log("move left");//控制台打印信息  
  9.             event.accepted = true;  
  10.         }  
  11.     }  
  12.      Keys.onSelectPressed: console.log("Selected");  
  13.      Keys.onDigit0Pressed: console.log("0 Pressed");  
  14. }  
  15. //Keys事件还有很多具体可以查API,onSelectPressed,我现在也不知道怎么触发,希望有朋友知道告诉我。  

Item以及继承自它的元素的大多数属性,当属性值变化会有一个信号发射出去。

 

2、Rectangle

   

  1. Rectangle {  
  2.     width100  
  3.     height100  
  4.     color"white"  
  5.     border.color"#FF12FF"  
  6.     border.width10  
  7.     radius: 10  
  8.     Rectangle {  
  9.         anchors.fill :parent  
  10.         anchors.margins: 10  
  11.         color"red"  
  12.         clip: true  
  13.         Rectangle {  
  14.             anchors.fill: parent  
  15.             border.width1  
  16.         }  
  17.     }  
  18. }  
  19. //注:color的值不能用16进制0x******表示  

 

  1. Rectangle{  
  2.     width400height300  
  3.     Rectangle {  
  4.         width80height80  
  5.         color:  "lightsteelblue"  
  6.         radius: 8  
  7.     }  
  8.     Rectangle {  
  9.         y: 100width80height80  
  10.         gradient: Gradient {  
  11.             GradientStop {position0.0color"lightsteelblue"}  
  12.             GradientStop {position1.0color"blue"}  
  13.         }  
  14.         radius: 8  
  15.     }  
  16.     Rectangle {  
  17.          y: 200width80height80  
  18.          rotation: 90  
  19.          gradient: Gradient {  
  20.              GradientStop { position0.0color"lightsteelblue" }  
  21.              GradientStop { position1.0color"blue" }  
  22.          }  
  23.          radius: 8  
  24.      }  
  25. }  

Gradient顾名思义,即梯度,也就是我们常说的逐变色。一个gradient 被两个或更多的颜色定义混合形成。颜色可以从位置0.0到1.0逐渐变化,如:

  1. Rectangle {  
  2.      width100height100  
  3.      gradient: Gradient {  
  4.          GradientStop { position0.0color"red" }  
  5.          GradientStop { position0.33color"yellow" }  
  6.          GradientStop { position1.0color"green" }  
  7.      }  
  8.  }  

Gradient包含一个属性:stops:list<GradientStop>

GradientStop元素仅包含两个属性color:color和position:real

 

3、AnimatedImage

      此元素是用于播放动画像gif这样存储着帧的文件

      

  1. import Qt 4.7  
  2. Rectangle {  
  3.     width: animation.width; height: animation.height + 8  
  4.     AnimatedImage { id: animation; source: "animation.gif" }  
  5.     Rectangle {  
  6.         property int frames: animation.frameCount  
  7.         width4height8  
  8.         x: (animation.width - width) * animation.currentFrame / frames  
  9.         y: animation.height  
  10.         color"red"  
  11.     }  
  12. }  

 

 

4、TextInput

       做过界面开发的应该都知道这就是个单行文本框.它有很多丰富的属性

     

  1. TextInput {  
  2.     id: textInput  
  3.     text: "Hello world!"  
  4.     width200;  
  5.     height20;  
  6.     activeFocusOnPress: false  
  7.     selectionColor: "#00FF00"  
  8.     selectByMouse: true  
  9.     echoMode: TextInput.Password  
  10.     MouseArea {  
  11.         anchors.fill: parent  
  12.         onClicked: {  
  13.             if (!textInput.activeFocus) {  
  14.                 textInput.forceActiveFocus()  
  15.                 textInput.openSoftwareInputPanel();  
  16.             } else {  
  17.                 textInput.focus = false;  
  18.             }  
  19.         }  
  20.         onPressAndHold: textInput.closeSoftwareInputPanel();  
  21.     }  
  22. }   

 

5、TextEdit

       多行文本框.

6、Image

      上面已用到过了,就是普通显示图片的元素

7、Text

       通常就是用作一个标签,在界面上显示信息.

8、BorderImage

      用来做窗口的边界。

     http://www./?p=592 介绍的很详细。哈哈。谢谢这个网站,我在里面学了很多qt相关的知识

9、除了上面介绍这些之外QML还提供了三个验证器,分别是IntValidator,DoubleValidator,RegExpValidator

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多