元素排列

Column

QML
    Column{
        spacing: 20 // 控件间间隔
        xxxPadding: 20 // 外围填充
        Button{
            width:100
            height:20
        }
        Button{
            width:100
            height:20
        }
        Button{
            width:100
            height:20
        }
    }

Row

QML
    Row{
        layoutDirection: Qt.RightToLeft // 排列方向
        Button{
            width:100
            height:20
        }
        Button{
            width:100
            height:20
        }
        Button{
            width:100
            height:20
        }
    }

Grid

QML
    Grid{
        columns: 3 // 列数,会自动换行
    }

元素布局

RowLayout

QML
    RowLayout{
        // NOTE - 只有布局的父布局才需要指定,子布局和子 Item 不用设置
        anchors.fill:parent 

        Rectangle{
            Layout.fillWidth:true  // 当前 Item 宽度按照最大允许值填充 Layout
            Layout.fillHeight:true // 当前 Item 高度按照最大允许值填充 Layout
        }
        Rectangle{
            // 
            Layout.preferredWidth:20   // 当前 Item 宽度度参考给定值填充 Layout
            Layout.preferredHeight:10  // 当前 Item 高度参考给定值填充 Layout
        }
    }

Row

ColumnLayout

QML
    ColumnLayout{
        Rectangle{
            Layout.fillWidth:true  // 当前 Item 宽度按照最大允许值填充 Layout
            Layout.fillHeight:true // 当前 Item 高度按照最大允许值填充 Layout
        }
        Rectangle{
            Layout.preferredWidth:20   // 当前 Item 宽度度参考给定值填充 Layout
            Layout.preferredHeight:10  // 当前 Item 高度参考给定值填充 Layout
        }
    }

GridLayout

QML
    GridLayout{

    }

宽高控制

布局中的子 Item 会存在三类宽高属性,被布局采纳顺序如下:

优先级 属性 说明
高 Layout.preferredWidth
Layout.preferredHeight
布局的宽高
中 implicitWidth
implicitHeight
Item 中展示的内容大小,0 表示无效
低 width
height
Item 在qml框架中大小,上述属性均无效时会被采纳。
QML
Text{
    id: lbl
    width: 50
    height:20
    text:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    Component.onCompleted:{
        // Text 控件在框架中被识别出的宽高
        console.log(lbl.width, lbl.height);

        // Text 控件中文字的宽高
        console.log(lbl.implicitWidth, lbl.implicitHeight);
    }
}
Terminal
$./a.out
qml: 50 20
qml: 210 12

Flow

QML
    Rectangle{
        width: 100
        height : 50

        // 会根据控件尺寸调整内部控件布局
        Flow{
            flow: Flow.TopToBottom // 方向
            anchors.fill: parent
            Text{
                text: "aaaaaaa"
            }
            Text{
                text: "bbbbbbbb"
            }
        }
    }

Flickable

drag

Flickable 可以实现子控件的拖拽与滚动。

QML
Flickable{
    id: flick

    // Flickable 的宽高,即显示窗口的大小
    width: 100
    height:100

    // contentItem 展示内容的宽高,即哆啦A梦图片的实际大小
    contentHeight: img.height
    contentWidth: img.width

    clip:true // 允许 contentItem 能截取展示
    ScrollBar.vertical: ScrollBar{} // 为 contentItem 添加滚动条

    boundsMovement: Flickable.StopAtBounds // 控制拖动、轻弹功能
    boundsMovement:Flickable.StopAtBounds // 控制移动

    Rectangle{
        // 这里的 parent 并不是 Flickable,的使用 id: flick 才能访问 Flickable
        anchors.fill:parent 
    }
}

通过属性 contentX 与 contentY 可以访问控件被拖拽的坐标。以控件初始位置的左上角为坐标原点,x 轴向左为整,y轴向上为正。

flick position

QML
Flickable { 

    clip: true

    // 展示内容的宽高
    contentHeight: gridLayout.height
    contentWidth: gridLayout.width

    // 滚动条
    ScrollBar.vertical: ScrollBar{
        id:scrollbar
    }

    interactive: false

    MouseArea{
        anchors.fill:parent
        preventStealing: true
        propagateComposedEvents: true
        onPressed: {
            mouse.accepted = false;
        }

        onWheel: {
            if(wheel.angleDelta.y > 0){
                scrollbar.decrease();
            }else{
                scrollbar.increase();
            }
        }
    }

    // 实际内容
    GridLayout{
        id: gridLayout
    }
}

OpacityMask

QML
    OpacityMask{
        source: src_id // 要展示的图层
        maskSource: mask_id // 遮罩图层
    }

License

Author: 海拉鲁的三角

Link: http://localhost:1313/cpp_notes/posts/qml/layout/

License: MIT

只要学不死,就往死里学