QML布局元素

40 篇文章 84 订阅
32 篇文章 7 订阅
本文介绍了QML作为QtQuick的声明式脚本语言,用于创建用户界面。QML适用于移动、桌面和Web应用的开发,提供了灵活的元素布局方式,如填充、靠左对齐、靠右对齐、水平和垂直居中等,并通过实例演示了如何使用这些布局策略。
摘要由CSDN通过智能技术生成

目录

一 QML介绍

二 QML的使用场合

三 实例演示

一 元素布局介绍

二 子控件充满父控件

三 基于父控件靠左对齐

四 基于父控件靠右对齐

五 水平居中对齐

六 垂直居中对齐

七   相对于父控件居中

八 相对于水平居中向右偏移20,此属性配合horizontalCenter使用才有效


一 QML介绍

QML是Qt Quick的缩写,它是一种新型的、面向对象的、跨平台的脚本语言,可以用来描述用户界面或应用程序的交互逻辑。QML可以在Qt应用程序中使用,也可以在其他JavaScript应用程序中使用。

QML使用XML语法来描述应用程序的用户界面,其中包括各种组件、布局、控件和事件处理程序等。这种语言非常易于学习和使用,因为它具有简单的语法、清晰的结构和易于理解的类型系统。此外,QML还支持自定义组件和自定义控件,使开发人员能够根据需要灵活地设计和重构用户界面。

QML可以帮助开发人员快速构建原生桌面应用程序、移动应用程序和Web应用程序等。由于它是Qt框架的一部分,因此可以利用Qt提供的丰富功能和工具,如Qt Creator、Qt Widgets等。因此,使用QML可以大大提高开发效率和应用程序的质量。

二 QML的使用场合

QML是一种用于描述应用程序用户界面的声明式编程语言,主要应用于移动应用程序、桌面应用程序和Web应用程序等领域。以下是QML主要应用场景:

  1. 移动应用程序:QML可以帮助开发人员快速构建原生移动应用程序,如游戏、音乐播放器、地图应用等。由于QML可以将用户界面分解为一个个小的元素,并且可以对这些元素进行美化和自定义,因此非常适合构建移动应用程序。
  2. 桌面应用程序:QML可以用于开发桌面应用程序,如窗口管理器、文本编辑器、数据分析工具等。QML可以将界面分解为各个小的部件,并且可以使用Qt提供的各种组件和工具来构建高效的桌面应用程序。
  3. Web应用程序:QML可以用于开发Web应用程序,如网页浏览器、表单验证器、媒体播放器等。由于QML可以将界面分解为小的元素,并且可以使用JavaScript来操作这些元素,因此非常适合构建Web应用程序。

QML是一种非常灵活和易于使用的编程语言,可以帮助开发人员快速构建高效的用户界面,并且可以在不同的应用程序领域中使用。

三 实例演示

一 元素布局介绍

QML使用anchors(锚)对元素进行布局。anchoring(锚定)是基础元素对象的基本属性,可以被所有的可视化QML元素使用。一个anchors(锚)就像一个协议,并且比几何变化更加强大。Anchors(锚)是相对关系的表达式,你通常需要与其它元素搭配使用。

二 子控件充满父控件

import QtQuick
import QtQuick.Window

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

    Rectangle {
        width: 200
        height: 200
        color: "red"
            Rectangle {
                width: 100
                height: 100
                //有了锚定anchors属性之后,即便设置长宽也不生效
                anchors.fill: parent  //填充负控件
                //anchors.left: parent.left //基于父控件靠左对齐
                //anchors.right: parent.right  //基于父控件靠右对齐
                //anchors.horizontalCenter: parent.horizontalCenter //水平居中对齐
                //anchors.verticalCenter: parent.verticalCenter //垂直居中对齐
                //anchors.centerIn: parent  //相对于父控件居中
                //anchors.horizontalCenterOffset: 20 //相对于水平居中向右偏移20,此属性配合horizontalCenter使用才有效
                anchors.margins: 8

            }
        }
}

三 基于父控件靠左对齐

import QtQuick
import QtQuick.Window

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

    Rectangle {
        width: 200
        height: 200
        color: "red"
        anchors.centerIn: parent
            Rectangle {
                width: 100
                height: 100
                //有了锚定anchors属性之后,即便设置长宽也不生效
                //anchors.fill: parent  //填充负控件
                anchors.left: parent.left //基于父控件靠左对齐
                //anchors.right: parent.right  //基于父控件靠右对齐
                //anchors.horizontalCenter: parent.horizontalCenter //水平居中对齐
                //anchors.verticalCenter: parent.verticalCenter //垂直居中对齐
                //anchors.centerIn: parent  //相对于父控件居中
                //anchors.horizontalCenterOffset: 20 //相对于水平居中向右偏移20,此属性配合horizontalCenter使用才有效
                anchors.margins: 8

            }
        }
}

 

四 基于父控件靠右对齐

import QtQuick
import QtQuick.Window

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

    Rectangle {
        width: 200
        height: 200
        color: "red"
        anchors.centerIn: parent
            Rectangle {
                width: 100
                height: 100
                //有了锚定anchors属性之后,即便设置长宽也不生效
                //anchors.fill: parent  //填充负控件
                //anchors.left: parent.left //基于父控件靠左对齐
                anchors.right: parent.right  //基于父控件靠右对齐
                //anchors.horizontalCenter: parent.horizontalCenter //水平居中对齐
                //anchors.verticalCenter: parent.verticalCenter //垂直居中对齐
                //anchors.centerIn: parent  //相对于父控件居中
                //anchors.horizontalCenterOffset: 20 //相对于水平居中向右偏移20,此属性配合horizontalCenter使用才有效
                anchors.margins: 8
                color: "blue"

            }
        }
}

 

五 水平居中对齐

import QtQuick
import QtQuick.Window

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

    Rectangle {
        width: 200
        height: 200
        color: "red"
        anchors.centerIn: parent
            Rectangle {
                width: 100
                height: 100
                //有了锚定anchors属性之后,即便设置长宽也不生效
                //anchors.fill: parent  //填充负控件
                //anchors.left: parent.left //基于父控件靠左对齐
                //anchors.right: parent.right  //基于父控件靠右对齐
                anchors.horizontalCenter: parent.horizontalCenter //水平居中对齐
                //anchors.verticalCenter: parent.verticalCenter //垂直居中对齐
                //anchors.centerIn: parent  //相对于父控件居中
                //anchors.horizontalCenterOffset: 20 //相对于水平居中向右偏移20,此属性配合horizontalCenter使用才有效
                anchors.margins: 8
                color: "blue"

            }
        }
}

 

六 垂直居中对齐

import QtQuick
import QtQuick.Window

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

    Rectangle {
        width: 200
        height: 200
        color: "red"
        anchors.centerIn: parent
            Rectangle {
                width: 100
                height: 100
                //有了锚定anchors属性之后,即便设置长宽也不生效
                //anchors.fill: parent  //填充负控件
                //anchors.left: parent.left //基于父控件靠左对齐
                //anchors.right: parent.right  //基于父控件靠右对齐
                //anchors.horizontalCenter: parent.horizontalCenter //水平居中对齐
                anchors.verticalCenter: parent.verticalCenter //垂直居中对齐
                //anchors.centerIn: parent  //相对于父控件居中
                //anchors.horizontalCenterOffset: 20 //相对于水平居中向右偏移20,此属性配合horizontalCenter使用才有效
                anchors.margins: 8
                color: "blue"

            }
        }
}

七   相对于父控件居中

import QtQuick
import QtQuick.Window

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

    Rectangle {
        width: 200
        height: 200
        color: "red"
        anchors.centerIn: parent
            Rectangle {
                width: 100
                height: 100
                //有了锚定anchors属性之后,即便设置长宽也不生效
                //anchors.fill: parent  //填充负控件
                //anchors.left: parent.left //基于父控件靠左对齐
                //anchors.right: parent.right  //基于父控件靠右对齐
                //anchors.horizontalCenter: parent.horizontalCenter //水平居中对齐
                //anchors.verticalCenter: parent.verticalCenter //垂直居中对齐
                anchors.centerIn: parent  //相对于父控件居中
                //anchors.horizontalCenterOffset: 20 //相对于水平居中向右偏移20,此属性配合horizontalCenter使用才有效
                anchors.margins: 8
                color: "blue"

            }
        }
}

 

八 相对于水平居中向右偏移20,此属性配合horizontalCenter使用才有效

import QtQuick
import QtQuick.Window

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

    Rectangle {
        width: 200
        height: 200
        color: "red"
        anchors.centerIn: parent
            Rectangle {
                width: 100
                height: 100
                //有了锚定anchors属性之后,即便设置长宽也不生效
                //anchors.fill: parent  //填充负控件
                //anchors.left: parent.left //基于父控件靠左对齐
                //anchors.right: parent.right  //基于父控件靠右对齐
                anchors.horizontalCenter: parent.horizontalCenter //水平居中对齐
                //anchors.verticalCenter: parent.verticalCenter //垂直居中对齐
                //anchors.centerIn: parent  //相对于父控件居中
                anchors.horizontalCenterOffset: 20 //相对于水平居中向右偏移20,此属性配合horizontalCenter使用才有效
                anchors.margins: 8
                color: "blue"

            }
        }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
QML是一种基于JavaScript语法的用户界面描述语言,它使用了一种类似CSS的布局方式来管理UI元素的位置和大小。这种布局方式称为QML布局管理,它使得UI设计师可以更加方便地创建和管理UI布局。 以下是QML布局管理使用说明: 1. 容器元素QML布局管理中,容器元素是最基本的元素之一。它们用于包含其他UI元素,从而创建整个UI界面。常见的容器元素包括Rectangle、Item、ColumnLayout、RowLayout等。 2. 布局属性 QML布局管理提供了一些布局属性,用于控制UI元素的位置和大小。常见的布局属性包括x、y、width、height、anchors等。 其中,x和y属性用于控制UI元素的位置,width和height属性用于控制UI元素的大小,anchors属性用于将UI元素锚定到其他UI元素上。 3. 布局方式 QML布局管理提供了多种布局方式,包括水平布局、垂直布局、网格布局等。这些布局方式可以通过使用不同的容器元素布局属性来实现。 例如,使用ColumnLayout容器元素和Layout.alignment属性可以创建垂直布局,使用RowLayout容器元素和Layout.alignment属性可以创建水平布局,使用GridLayout容器元素可以创建网格布局。 4. 响应式设计 QML布局管理支持响应式设计,可以根据不同的设备和屏幕尺寸来自动调整UI元素的位置和大小。这可以通过使用Layout.preferredWidth和Layout.preferredHeight属性来实现。 例如,为了使UI元素在不同的屏幕尺寸下都能够自适应,可以设置Layout.preferredWidth和Layout.preferredHeight属性为相对值,如:Layout.preferredWidth: parent.width * 0.5。 总之,QML布局管理是一种非常方便和灵活的UI布局方式,可以帮助UI设计师更加高效地创建和管理UI界面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值