翻譯|使用教程|編輯:龔雪|2021-12-27 10:53:51.903|閱讀 386 次
概述:本文主要介紹如何在QML中的定位器和布局,歡迎下載框架產品體驗~
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
有很多種方法可以在QML中定位項目,以下是簡要的概述。
通過設置項目的 x,y 屬性,可以將項目放置在屏幕上的特定 x,y 坐標處。 根據可視化坐標系規則,這將設置它們相對于父級左上角的位置。
結合使用綁定替代這些屬性的常量值,通過將 x 和 y 坐標設置為適當的綁定,也可以輕松實現相對定位。
import QtQuick Item { width: 100; height: 100 Rectangle { // Manually positioned at 20,20 x: 20 y: 20 width: 80 height: 80 color: "red" } }
Item類型提供了錨定到其他Item類型的功能,每個項目有7條錨線:左、右、垂直居中、頂部、底部、基線和水平居中。三個垂直錨線可以錨定到另一個項目的三個垂直錨線中的任何一個,四個水平錨線可以錨定到另一個項目的水平錨線。
import QtQuick Item { width: 200; height: 200 Rectangle { // Anchored to 20px off the top right corner of the parent anchors.right: parent.right anchors.top: parent.top anchors.margins: 20 // Sets all margins at once width: 80 height: 80 color: "orange" } Rectangle { // Anchored to 20px off the top center corner of the parent. // Notice the different group property syntax for 'anchors' compared to // the previous Rectangle. Both are valid. anchors { horizontalCenter: parent.horizontalCenter; top: parent.top; topMargin: 20 } width: 80 height: 80 color: "green" } }
對于想要以常規模式定位一組類型的常見情況,Qt Quick提供了一些定位器類型。放置在定位器中的物品會以某種方式自動定位; 例如, Row將項目定位為水平相鄰(形成一行)。
import QtQuick Item { width: 300; height: 100 Row { // The "Row" type lays out its child items in a horizontal line spacing: 20 // Places 20px of space between items Rectangle { width: 80; height: 80; color: "red" } Rectangle { width: 80; height: 80; color: "green" } Rectangle { width: 80; height: 80; color: "blue" } } }
Layout types功能與定位器類似,但允許進一步細化或限制布局。 具體來說,Layout types允許您:
GroupBox { id: gridBox title: "Grid layout" Layout.fillWidth: true GridLayout { id: gridLayout rows: 3 flow: GridLayout.TopToBottom anchors.fill: parent Label { text: "Line 1" } Label { text: "Line 2" } Label { text: "Line 3" } TextField { } TextField { } TextField { } TextArea { text: "This widget spans over three rows in the GridLayout.\n" + "All items in the GridLayout are implicitly positioned from top to bottom." Layout.rowSpan: 3 Layout.fillHeight: true Layout.fillWidth: true } } }
上面的代碼片段來自基本布局示例,該代碼段顯示了在布局中添加各種字段和項目的簡單性。 GridLayout 可以調整大小,并且可以通過各種屬性自定義其格式。
Qt技術交流群:166830288 歡迎一起進群討論
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網