Qt_QML定位元素学习

  • Column(列)元素将它的子对象通过顶部对齐的列方式进行排列。spacing属性用来设置每个元素之间的间隔大小。
  • Row(行)元素将它的子对象从左到右,或者从右到左依次排列,排列方式取决于layoutDirection属性。spacing属性用来设置每个元素之间的间隔大小。
  • Grid(栅格)元素通过设置rows(行数)和columns(列数)将子对象排列在一个栅格中。可以只限制行数或者列数。如果没有设置它们中的任意一个,栅格元素会自动计算子项目总数来获得配置,例如,设置rows(行数)为3,添加了6个子项目到元素中,那么会自动计算columns(列数)为2。属性flow(流)与layoutDirection(布局方向)用来控制子元素的加入顺序。spacing属性用来控制所有元素之间的间隔。
  • Flow(流)。通过flow(流)属性和layoutDirection(布局方向)属性来控制流的方向。它能够从头到底的横向布局,也可以从左到右或者从右到左进行布局。作为加入流中的子对象,它们在需要时可以被包装成新的行或者列。为了让一个流可以工作,必须指定一个宽度或者高度,可以通过属性直接设定,或者通过anchor(锚定)布局设置。

通常Repeater(重复元素)与定位器一起使用。它的工作方式就像for循环与迭代器的模式一样。

// 基础  RedSquare.qml
import QtQuick 2.0

Rectangle {
    width: 48
    height: 48
    color: "red"
    border.color: Qt.lighter(color)
}
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.VirtualKeyboard 2.2

Window {
    id: root
    visible: true
    width: 700
    height: 525
    title: qsTr("Hello World")

	// Column(列)元素
    Column {
        id: column
        x: 10
        y: 10
        spacing: 8
        RedSquare { }
        GreenSquare { width: 96 }
        BlueSquare { }
    }

	// Row(行)元素
    Row {
        id: row
        x: 10
        y: 200
        spacing: 20
        BlueSquare { }
        GreenSquare { }
        RedSquare { }
    }

	// Grid(栅格)
    Grid {
        id: grid
        rows: 2
        columns: 2
        x: 300
        y: 10
        spacing: 8
        RedSquare { }
        RedSquare { }
        RedSquare { }
        RedSquare { }
    }

    // 不成功
    // Flow(流)
//    Flow {
////        anchors.fill: parent
//        x: 600
//        y: 10
//        anchors.margins: 20
//        spacing: 20
//        RedSquare { }
//        BlueSquare { }
//        GreenSquare { }
//    }

	// Repeater(重复元素)
    property variant colorArray: ["#00bde3", "#67c111", "#ea7025"]
    Grid{
//        anchors.fill: parent
        x: 10
        y: 280
        anchors.margins: 8
        spacing: 4

        Repeater {
            model: 16
            Rectangle {
                width: 56; height: 56
                property int colorIndex: Math.floor(Math.random()*3)
                color: root.colorArray[colorIndex]
                border.color: Qt.lighter(color)
                    Text {
                        anchors.centerIn: parent
                        color: "#f0f0f0"
                        text: "Cell " + index
                    }
            }
        }
    }
}

效果图:
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章