qml学习笔记(二):可视化元素基类Item详解(上半场anchors等等)
原博主博客地址:http://blog.csdn.net/qq21497936
本文章博客地址:http://blog.csdn.net/qq21497936/article/details/78516201
qml學習筆記(二):可視化元素基類Item詳解(上半場anchors等等)
? ? 本學章節(jié)筆記主要詳解Item元素(上半場主要涉及anchors錨),因為所有可視化的界面元素都繼承于Item,熟悉Item后,不同的繼承子類,有其定制的屬性(從幾個到幾十個不等)。
? ??《Qt實用技巧:在Qt Gui程序中嵌入qml界面(可動態(tài)覆蓋整個窗口)》:
http://blog.csdn.net/qq21497936/article/details/78486552
? ? 《qml學習筆記(一):界面元素初探》:
http://blog.csdn.net/qq21497936/article/details/78498575
? ?《qml學習筆記(三):可視化元素基類Item詳解(下半場)》
http://blog.csdn.net/qq21497936/article/details/78522816
基類Item介紹
?
??? 基類Item是所有可視化子類的父類,它不是可見的,但是它定義了所有通用元素通用的屬性,比如x、y、width、height、anchoring和handingsuppourt。
? ? 幾個Item的使用示例? ??
Image示例
?
[css]?view plain?copy- 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"??
- ????????}??
- ????}??
- }??
? ? 效果如下圖:
?
?
捕捉鍵盤
?
?
[css]?view plain?copy- Item{??
- ????focus:true??
- ????Keys.onPressed:{??
- ????????if(event.key==Qt.Key_Left){??
- ????????????console.log("moveleft");??
- ????????????event.accepted=true;??
- ????????}??
- ????}??
- ????Keys.onReturnPressed:??
- ????????console.log("Pressedreturn");??
- }??
?
輸入處理
[css]?view plain?copy- Rectangle{??
- width:100;??
- height:100??
- ????FocusScope{??
- ????id:focusScope??
- ????focus:true??
- ????TextInput{??
- ????????id:input??
- ????????focus:true??
- ????????}??
- ????}??
- }??
效果如圖
?
屬性詳解
activeFocus?: bool [可讀寫][指示焦點:窗口是否獲取焦點]
?
? ? ? ? 此屬性指示元素是否具有活動焦點。如果指示是真的,這個對象是目前接收鍵盤輸入,或是一個FocusScope為父對象的對象,目前接收鍵盤輸入。
通常,此屬性是通過設置焦點在其子元素(繼承于Item)和其外圍FocusScope對象獲得。在下面的例子中,TextInput和FocusScope對象會有活躍的熱點,而根矩形對象將不會。
activeFocusOnTab?: bool [可讀寫][設置item是否可被tab選中,默認為false]
anchors:一組屬性,提供了以元素相互關系為基準的定位方式,主要包括以下的:
anchors.top?: AnchorLine [可讀寫][頂部邊界]
[css]?view plain?copy- 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:單位像素,依賴于設備??
- ????}??
- }??
anchors.bottom?: AnchorLine [可讀寫][底部邊界]
[css]?view plain?copy- 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:單位像素,依賴于設備??
- ????}??
- }??
anchors.left?: AnchorLine [可讀寫][左邊界]
[css]?view plain?copy- 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:單位像素,依賴于設備??
- ????}??
- }??
anchors.right?: AnchorLine [可讀寫][右邊界]
[css]?view plain?copy- Item?{??
- ????Image?{??
- ????????id:?pic;??
- ????????x:100;??
- ????????y:10;??
- ????????source:?"./1.png";??
- ????}??
- ????Text?{??
- ????????id:?label;??
- ????????anchors.right:?pic.right;?//?對象的頂部是與pic的底部高度相同??
- ????????text:?"hello?world";??
- ????????color:?"black";??
- ????????font.pointSize:?14;?//大于0的值,與設備無關font.pixelSize:單位為像素,依賴于設備??
- ????}??
- }??
anchors.horizontalCenter?: AnchorLine [可讀寫][水平中心]
[css]?view plain?copy- Item?{??
- ????Image?{??
- ????????id:?pic;??
- ????????source:?"./1.png";??
- ????}??
- ????Text?{??
- ????????id:?label??
- ????????//?對象的水平中心?以?pic的水平中心?為中心??
- ????????anchors.horizontalCenter:?pic.horizontalCenter;??????????
- ????????text:?"hello?world";??
- ????????color:?"white";??
- ????????font.pointSize:?14;?//?大于0的值,與設備無關font.pixelSize:單位像素,依賴于設備??
- ????}??
- }??
anchors.verticalCenter?: AnchorLine [可讀寫][垂直中心]
[css]?view plain?copy- Item?{??
- ????Image?{??
- ????????id:?pic;??
- ????????x:100;??
- ????????y:10;??
- ????????source:?"./1.png";??
- ????}??
- ????Text?{??
- ????????id:?label;??
- ????????anchors.verticalCenter:?pic.bottom;?//?對象的頂部是與pic的底部高度相同??
- ????????text:?"hello?world";??
- ????????color:?"black";??
- ????????font.pointSize:?14;?//大于0的值,與設備無關font.pixelSize:單位像素,依賴于設備??
- ????}??
- }??
anchors.baseline?: AnchorLine AnchorLine [可讀寫][baseline是指的文本所在的線,如果item沒有文字的話baseline就和top的位置是相同的]
[css]?view plain?copy- Item?{??
- ????Image?{??
- ????????id:?pic;??
- ????????x:40;??
- ????????y:40;??
- ????????source:?"./1.png";??
- ????}??
- ????Text?{??
- ????????id:?label;??
- ????????anchors.baseline:?pic.top;??
- ????????text:?"hello?world";??
- ????????color:?"black";??
- ????????font.pointSize:?14;?//大于0的值,與設備無關font.pixelSize:單位像素,依賴于設備??
- ????}??
- }??
anchors.fill?: Item [可讀寫][用本對象填充指向的對象元素]
?
[css]?view plain?copy- Item{??
- ????Image{??
- ????????id:pic;??
- ????????x:40;??
- ????????y:40;??
- ????????source:"./1.png";??
- ????}??
- ????Rectangle{??
- ????????id:label;??
- ????????anchors.fill:pic;?//?此時設置width和height,測試無效,直接填滿pic??
- ????????color:"black";??
- ????}??
- }??
?
?
anchors.centerIn?: Item [可讀寫][用本對象的中心對準指向對象的中心,開始輻射出去,區(qū)域可大于設置指向的對象]
[css]?view plain?copy- Item?{??
- ????Image?{??
- ????????id:?pic;??
- ????????x:40;??
- ????????y:40;??
- ????????source:?"./1.png";??
- ????}??
- ????Rectangle?{??
- ????????id:?label;??
- ????????width:?60;??
- ????????height:?60;??
- ????????anchors.centerIn:?pic;?//?以pic的中心為該對象中心進行輻射(區(qū)域可大于pic)??
- ????????color:?"black";??
- ????}??
- }??
?
?
anchors.margins?: real [可讀寫][設置所有(top,bottom,left,right)邊框的寬度]
[css]?view plain?copy- Item?{??
- ????Image?{??
- ????????id:?pic;??
- ????????x:40;??
- ????????y:40;??
- ????????source:?"./1.png";??
- ????}??
- ????Rectangle?{??
- ????????id:?label;??
- ????????width:?60;??
- ????????height:?60;??
- ????????color:?"black";??
- ????????anchors.margins:?10;??
- ????????anchors.left:?pic.right;??
- ????}??
- ????Rectangle?{??
- ????????id:?label2;??
- ????????width:?60;??
- ????????height:?60;??
- ????????color:?"black";??
- ????????anchors.margins:?10;??
- ????????anchors.top:?pic.bottom;??
- ????}??
- }??
?
?
[css]?view plain?copy- Item?{??
- ????Rectangle?{??
- ????????id:?label;??
- ????????width:?60;??
- ????????height:?60;??
- ????????color:?"red";??
- ????????anchors.margins:?50;??
- ????}??
- ????Rectangle?{??
- ????????id:?label2;??
- ????????width:?60;??
- ????????height:?60;??
- ????????color:?"black";??
- ????????anchors.margins:?50;?//?只對本對象設置anchors邊框有效??
- ????????anchors.top:?label.bottom;??
- ????}??
- ????Rectangle?{??
- ????????id:?labe3;??
- ????????width:?60;??
- ????????height:?60;??
- ????????color:?"red";??
- ????????anchors.margins:?50;?//?只對本對象設置anchors邊框有效??
- ????????anchors.top:?labe2.bottom;??
- ????}??
- }??
?
?
anchors.topMargin?: real [可讀寫][設置top邊框的寬度,參照margins]
anchors.bottomMargin?: real [可讀寫][設置bottom邊框的寬度,參照margins]
anchors.leftMargin?: real [可讀寫][設置left邊框的寬度,參照margins]
anchors.rightMargin?: real [可讀寫][設置right邊框的寬度,參照margins]
anchors.horizontalCenterOffset?: real [可讀寫][設置水平中心偏移量]
[css]?view plain?copy- Item?{??
- ????Image?{??
- ????????id:?pic;??
- ????????source:?"./1.png";??
- ????}??
- ????Rectangle?{??
- ????????width:?30;??
- ????????height:?30;??
- ????????id:?rect;??
- ????????color:?"black";??
- ????????//?對象的水平中心?以?pic的水平中心?為中心??
- ????????anchors.horizontalCenter:?pic.horizontalCenter;??
- ????????//?注意:horizomtalCenterOffset針對于horizontalCenter??
- ????????anchors.horizontalCenterOffset:?50;??
- ????}??
- }??
?
?
anchors.verticalCenterOffset?: real [可讀寫][參照設horizontalCenter,與其類似]
anchors.baselineOffset?: real[可讀寫][參照設horizontalCenter,與其類似]
anchors.alignWhenCentered?: bool [可讀寫][指定不使用半個像素繪制圖形,當需要居中一個elements,寬度或者高度是基數(shù),不使用半個像素繪制,對此處理解有疑問]
下章節(jié)
《qml學習筆記(三):可視化元素基類Item詳解(下半場)》:http://blog.csdn.net/qq21497936/article/details/78522816原博主博客地址:http://blog.csdn.net/qq21497936
本文章博客地址:http://blog.csdn.net/qq21497936/article/details/78516201
?
?
轉載于:https://www.cnblogs.com/senior-engineer/p/7986866.html
總結
以上是生活随笔為你收集整理的qml学习笔记(二):可视化元素基类Item详解(上半场anchors等等)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 10月份去敦煌有什么好玩的,10月去敦煌
- 下一篇: 为什么我的奥比岛页面白屏了