qml布局

qml的Column、Row、Grid布局与QTc++的布局一致,还有Flow布局,会自动排列不会重叠,Repeater用于用于创建大量类似的项目,比如格子什么的。锚线用于定位部件在父部件中的位置,anchor下的horizontalCenter、VertualCenter、left、top、right、bottom等描述位置,LeftMargin、topMargin、rightMargin、bottomMargin描述边距。
main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    engine.load(QUrl(QLatin1String("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;
    return app.exec();
}

main.qml

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
ApplicationWindow
{
    id:root
    visible: true
    width: 640
    height: 480
    title: qsTr("Layout Manage")
    color: "lightgray"
    Column
    {
        //间隔
        spacing: 10
        //竖直方向的
        Column
        {
            spacing: 5
            Rectangle{color:"red";width:40;height:40}
            Rectangle{color:"green";width:30;height:30}
            Rectangle{color:"blue";width:20;height:20}
        }
        //水平方向的
        Row
        {
            spacing: 5
            Rectangle{color:"red";width:40;height:40}
            Rectangle{color:"green";width:30;height:30}
            Rectangle{color:"blue";width:20;height:20}
        }
        //Grid
        Grid
        {
            //行数
            columns: 2
            spacing: 5
            Rectangle{color:"red";width:50;height:50}
            Rectangle{color:"green";width:40;height:40}
            Rectangle{color:"blue";width:30;height:30}
            Rectangle{color:"yellow";width:20;height:20}
            Rectangle{color:"lightgreen";width:10;height:10}
        }
        Rectangle
        {
            id:rect
            height:root.height/3
            width:root.width/2
            color:"gray"
            //Grid布局
            Grid
            {
                //左边、上边的间距
                leftPadding: 15
                topPadding: 15
                //5 x 5个格子
                columns: 5
                rows:5
                spacing: 2
                //Repeater
                Repeater
                {
                    model: 25
                    Rectangle
                    {
                        //Repeater里边是个方块
                        width: rect.width/6
                        height: rect.height/6
                        color: "lightgreen"
                        Text
                        {
                            anchors.centerIn: parent
                            //文本是 1-25
                            text:index + 1
                        }
                    }
                }
            }
        }
        //锚布局
        Rectangle
        {
            id:rect1
            width:root.width/5
            height:root.height/5
            color:"red"
            Rectangle
            {
                //水平、竖直居中
                anchors.horizontalCenter: rect1.horizontalCenter
                anchors.verticalCenter: rect1.verticalCenter
                width:rect1.width/3
                height:rect1.height/3
                color:"green"
            }
        }
    }
    //Flow,像单词堆叠一样
    Flow
    {
        anchors.fill: parent
        //从左往右
        flow: Flow.LeftToRight
        //从上到下
        //flow: Flow.TopToBottom
        spacing: 10
        Text{text:"this";font.pixelSize: 40}
        Text{text:"is";font.pixelSize: 40}
        Text{text:"a";font.pixelSize: 40}
        Text{text:"qml";font.pixelSize: 40}
        Text{text:"program";font.pixelSize: 40}
        Text{text:"to";font.pixelSize: 40}
        Text{text:"show";font.pixelSize: 40}
        Text{text:"qml";font.pixelSize: 40}
        Text{text:"layout";font.pixelSize: 40}
        Text{text:"management";font.pixelSize: 40}
        //动画效果
        move:Transition
        {
            NumberAnimation
            {
                properties: "x,y"
                //ease: "easeOutBounce"
            }
        }
    }
}

效果:
这里写图片描述
代码:https://github.com/yangyang0312/cpp/tree/master/Qt/qml/layoutManage

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值