QT quick基础QML学习2

文章介绍了Qt中的布局管理,包括Row、Column、Grid布局以及锚点布局的使用方法,展示了QML代码示例,如红、绿、蓝矩形的排列。同时提到了在VS中开发QML时可能遇到的问题,暗示可能需要检查JavaScript环境的支持。
摘要由CSDN通过智能技术生成

目录

布局简述:Qt布局有可以分为

Row(行)、Column(列(不演示了))

Grid(网格)

锚(anchors) 布局

布局管理器(GridLayout、RowLayout、ColumnLayout)

RowLayout

 GridLayout

VS开发QML问题


布局简述:Qt布局有可以分为

  1. 行、列、网格
  2. 水平、垂直、网格
  3. x、y(这里不再讨论)

Row(行)、Column(列(不演示了))

下面我们实操一下布局1,线上代码(编码是vs创建QML)

//GreenRectangle.qml
import QtQuick 2.3    //QtQuick 版本

Rectangle {
    width: 200
    height: 100
    color: "red"

    Text {
        anchors.centerIn: parent    //将text放在控件正中心,这里并是程序界面正中心哦
        text: "Hello, World!"
    }
}
//RedRectangle.qml
import QtQuick 2.3

Rectangle {
    width: 48
    height: 48
    color: "red"
    border.color: Qt.lighter(color)    //边框色,跟Qt.lighter里的颜色保持一致
}
//BlueRectangle.qml
import QtQuick 2.3

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

Window {
    id: roots    //程序里程序标识
    visible: true    //是否可见
    width: 640
    height: 480
    title: qsTr("Hello World")    //名称
   

    Column {
        id: row
        anchors.centerIn: parent
        spacing: 8    //间距
        RedRectangle { }
        GreenRectangle { width: 96 }    
        BlueRectangle { }
    }
}

效果如下(其他的就不演示了):

Grid(网格)

Window {
    id: roots
    visible: true

    width: 640
    height: 480
    title: qsTr("Hello World")
    Grid{
        anchors.centerIn: parent
        rows:2
        columns:2
        layoutDirection: Qt.RightToLeft    // 排序方式默认是Qt.LeftToRight
        //flow: Grid.TopToBottom
        rowSpacing:10
        columnSpacing:10
        RedRectangle {}
        GreenRectangle { width: 96 }    
        BlueRectangle { }
    }
}

锚(anchors) 布局

锚(可以认为是位置基准)可以分着下图几种方式,

 以上面例子:

Window {
    id: roots
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    RedRectangle { 
            anchors.right:roots.right
            anchors.verticalCenter:parent.verticalCenter //这里不能是roots
        }

    Column {
        id: row
        anchors.centerIn: parent
        spacing: 8
        RedRectangle { 
            anchors.right:roots.right
        }
        GreenRectangle { width: 96 }    
        BlueRectangle { }
    }
}

效果如下:

布局管理器(GridLayout、RowLayout、ColumnLayout)

属性解释
anchors.topMargin元素上边距,需要先设置好anchors.top属性的值
anchors.bottomMargin元素下边距,需要先设置好anchors.bottom属性的值
anchors.leftMargin元素左边距,需要先设置好anchors.left属性的值
anchors.rightMargin元素右边距,需要先设置好anchors.right属性的值
anchors.margins元素上下左右四个边距(同等距离),需要先设置好anchors.top,anchors.bottom, anchors.left和anchors.right属性的值。如果元素只元设置了anchors.top,那只有上边距起作用。
anchors.horizontalCenterOffset相对于水平居中线位置的偏移量,需要先设置好anchors.horizontalCenter属性的值
anchors.vrerticalCenterOffset相对于垂直居中线位置的偏移量,需要先设置好anchors.vrerticalCenter属性的值
anchors.centerIn将当前元素放在其他元素的正中位置
anchors.fill让当前元素填充到其他元素中

RowLayout

Window {
    id: roots
    visible: true

    width: 640
    height: 480
    title: qsTr("Hello World")

    RowLayout{
        anchors.centerIn:parent
        width:roots.width
        height:roots.height

        RedRectangle {}
        GreenRectangle { width: 96 }
        BlueRectangle { }
    }
}

ColumnLayout 与RowLayout差不多,这里不再概述

 GridLayout

GridLayout与Grid差不多,但是会比RowLayout多几个属性

属性解释
Layout.row元素所在的行索引(0表示第1行)
Layout.column元素所在的列索引(0表示第1列)
Layout.rowSpan元素占据的行数(默认是1)
Layout.columnSpan元素占据的列数(默认是1)
    GridLayout{
        anchors.centerIn: parent
        rows:2
        columns:2
        rowSpacing:10
        columnSpacing:10
        layoutDirection: Qt.RightToLeft
        //flow: Grid.TopToBottom
        RedRectangle {
            Layout.row: 0
            Layout.rowSpan: 1

        }
        GreenRectangle { width: 96
            Layout.row: 1
            Layout.column: 0
        }
        BlueRectangle {
            Layout.row: 1
            Layout.column:1
        }
    }

VS开发QML问题

 关于这里插件开发是用vs2019开的,但是编程感觉支持不太好,可能是我环境没有装javascript原因吧,这里说下怎么vs开发QML问题

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路奇怪

有钱出钱,没钱多出编程主意啊

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值