Qml:布局

简介: Qml:布局
import QtQuick
import QtQuick.Window
import QtQuick.Layouts
Window
{
    width: 1280
    height: 800
    visible: true
    title: qsTr("Test Layout")
    Rectangle
    {
        id:colroot
        implicitWidth: 300
        implicitHeight: 400
        color: "#EEEEEE"
        ColumnLayout
        {
            anchors.fill: parent
            spacing: 2
            Rectangle
            {
                color:"pink"
                Layout.minimumHeight: 40
                Layout.maximumHeight: 200
                Layout.maximumWidth: 300
                Layout.fillWidth: true
                Layout.fillHeight: true
                Text{anchors.centerIn: parent; text:parent.width +"x"+parent.height}
                MouseArea
                {
                    anchors.fill: parent
                    onClicked:
                    {
                        colroot.width+=10
                        colroot.height+=10
                    }
                }
            }
            Rectangle
            {
                color:"plum"
                Layout.fillWidth: true
                Layout.preferredHeight: 40
                Text{anchors.centerIn: parent; text:parent.width +"x"+parent.height}
                MouseArea
                {
                    anchors.fill: parent
                    onClicked:
                    {
                        colroot.width-=10
                        colroot.height-=10
                    }
                }
            }
            Rectangle
            {
                color:"lightgreen"
                Layout.preferredWidth: 100
                Layout.preferredHeight: 40
                Text{anchors.centerIn: parent; text:parent.width +"x"+parent.height}
            }
        }//ColumnLayout
        Text{text:"ColumnLayout"}
    }//colroot
    Rectangle
    {
        x:310
        id:rowroot
        implicitWidth: 600
        implicitHeight: 400
        color: "#EEEEEE"
        RowLayout
        {
            id:row
            anchors.fill: parent
            spacing: 0  //去掉间隔
            Rectangle
            {
                color:"pink"
                Layout.preferredWidth: 100
                Layout.fillHeight:true
                //放大背景
                MouseArea
                {
                    anchors.fill: parent
                    onClicked:
                    {
                        rowroot.width+=10
                        rowroot.height+=10
                    }
                }
                Text{anchors.centerIn: parent; text:parent.width +"x"+parent.height}
            }
            Rectangle
            {
                color:"lightgreen"
                Layout.fillWidth:true
                Layout.fillHeight:true
                Layout.minimumHeight: 100
                Layout.minimumWidth: 200
                MouseArea
                {
                    anchors.fill: parent
                    onClicked:
                    {
                        row.layoutDirection = (row.layoutDirection +1)%2
                    }
                }
                Text{anchors.centerIn: parent; text:parent.width +"x"+parent.height}
            }
            Rectangle
            {
                color:"plum"
                Layout.alignment: Qt.AlignRight //右对齐
                Layout.preferredWidth: 100
                Layout.fillHeight:true
                //缩小背景
                MouseArea
                {
                    anchors.fill: parent
                    onClicked:
                    {
                        rowroot.width-=10
                        rowroot.height-=10
                    }
                }
                Text{anchors.centerIn: parent; text:parent.width +"x"+parent.height}
            }
        }
        Text{text:"RowLayout"}
    }
    Rectangle
    {
        y:410
        id:gridroot
        implicitWidth: 300
        implicitHeight: 300
        color: "#EEEEEE"
        GridLayout
        {
            columns:3
            rowSpacing:0
            columnSpacing: 0
            //3X3
            Repeater
            {
                model:9
                Rectangle
                {
                    implicitWidth: 100
                    implicitHeight: 100
                    color: "lightgreen"
                    border.color: "pink"
                    border.width: 1
                }
            }
        }//GridLayout
        Text{text:"GridLayout"}
    }//gridroot
    Rectangle
    {
        x:310
        y:410
        id:stackroot
        implicitWidth: 300
        implicitHeight: 300
        color: "#EEEEEE"
        StackLayout
        {
            id:stack
            anchors.fill: parent
            currentIndex: 0 //当前显示哪个界面 从0开始
            Rectangle
            {
                color:"pink"
                Text{anchors.centerIn: parent; text:parent.width +"x"+parent.height}
            }
            Rectangle
            {
                color:"lightgreen"
                Text{anchors.centerIn: parent; text:parent.width +"x"+parent.height}
            }
            Rectangle
            {
                color:"lightblue"
                Text{anchors.centerIn: parent; text:parent.width +"x"+parent.height}
            }
        }//StackLayout
        MouseArea
        {
            anchors.fill: parent
            onClicked:
            {
                stack.currentIndex = (stack.currentIndex+1) % stack.count
            }
        }
    }//stackroot
}


相关文章
|
9月前
|
API 索引 容器
qml之布局管理器(Qt Quick Layouts)
qml之布局管理器(Qt Quick Layouts)
308 2
|
8月前
Qml:鼠标事件
Qml:鼠标事件
|
9月前
|
算法 程序员 人机交互
【QML 设置颜色】QML中的色彩魔法:从取色器到用户界面
【QML 设置颜色】QML中的色彩魔法:从取色器到用户界面
546 0
|
前端开发 C# 容器
WPF技术之控件布局
WPF提供了多种布局控件和技术,可以帮助开发人员轻松创建灵活的用户界面。
193 0
WPF技术之控件布局
|
XML C# 数据格式
WPF中用户控件和自定义控件
无论是在WPF中还是WinForm中,都有用户控件(UserControl)和自定义控件(CustomControl),这两种控件都是对已有控件的封装,实现功能重用。但是两者还是有一些区别,本文对这两种控件进行讲解。
368 0
Qt-QML-全新导航布局
哈哈,写了一个全新的导航布局,具体内容还没有完成,现在先把整个布局的屏幕划分分享出来
306 0
Qt-QML-全新导航布局
C#自定义控件
1 添加自定义属性
173 0
Qt-QML-隐藏鼠标指针
后面有一段时间需要到甲方爸爸那里去写个程序去,涉及大量界面界面的活,所以呢,得重新学习学习了, 在传统的Widget中,隐藏鼠标是很简单的,只需要一句话就可以了。如下
422 0
Qt-QML-隐藏鼠标指针
QML UI布局管理
概述 使用Qt做过UI的一定对QHBoxLayout, QVBoxLayout, 和QGridLayout这三个最重要也最常使用的layout managers非常熟悉。那么在QML中又是如何控制和管理UI布局的呢?那么我们这篇文章就为大家介绍这些基础知识。
1268 0