首页 > 其他分享 >QML(二)-Item与Rectangle

QML(二)-Item与Rectangle

时间:2023-06-18 13:06:27浏览次数:45  
标签:控件 anchors width color height Item QML Rectangle

本文内容基于:QML教程-P2QML-Item与Rectangle

Qt助手

虽然我们可以在Qt Creator中来搜索内容

QML(二)-Item与Rectangle_Qt助手

但是也可以通过Qt助手来查询,Qt助手的位置就在Qt安装目录中,当然需要根据自己的编译方式不同选择不同目录下的Qt助手,例如,我选择使用mingw8.1版本的64位进行编译,我的就在mingw81_64目录下。

QML(二)-Item与Rectangle_Qt助手_02

Rectangle

rectangle顾名思义是矩形,我们在Qt助手中查看其Properties(属性),发现只有几个。

QML(二)-Item与Rectangle_qml_03

但同时,我们也可以发现其Inherits(继承)自Item,所以Rectangle也拥有Item的属性。

Item

从下图中可以看出,Item继承自QtObject,本身也拥有众多属性,因为特别多,所以下图并没有展示全。

QML(二)-Item与Rectangle_Item_04


今日练习

QML(二)-Item与Rectangle_Qt助手_05

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15

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

    Rectangle {
        x: 100
        y: 100
        z: 1
        width: 100
        height: 50
        gradient: Gradient {
            GradientStop { position: 0.0; color: "lightsteelblue" }
            GradientStop { position: 1.0; color: "blue" }
        }
    }

    Rectangle {
        x: 120
        y: 120
        width: 100
        height: 50
        gradient: Gradient {
            GradientStop { position: 0.0; color: "red" }
            GradientStop { position: 1.0; color: "pink" }
        }
    }

    Rectangle {
        id: conterRect
        anchors.centerIn: parent
        width: 200
        height: 50
        color: "#c4c4c4"
        Text {
            id: center
            text: qsTr("居中")
            color: "white"
            anchors.centerIn: parent
        }
    }

    Rectangle {
        anchors.horizontalCenter: parent.horizontalCenter
        width: 200
        height: 50
        color: Qt.rgba(77/255,76/255,167/255,1)
        Text {
            id: horizontal
            text: qsTr("水平居中")
            color: "white"
            anchors.centerIn: parent
        }
    }

    Rectangle {
        id: verticalRect
        anchors.verticalCenter: parent.verticalCenter
        width: 200
        height: 50
        color:"#0857C9"
        Text {
            id: vertical
            text: qsTr("垂直居中")
            color: "white"
            anchors.centerIn: parent
        }
    }


    Rectangle {
        anchors.top: verticalRect.bottom
        anchors.topMargin: 20
        width: 200
        height: 50
        color: "#7b7b7b"
        radius: 20
    }

    Rectangle {
        anchors.top: conterRect.bottom
        anchors.topMargin: 20
        anchors.left: verticalRect.right
        anchors.leftMargin: 20
        width: 200
        height: 50
        color: "#7b7b7b"
        radius: 20
        antialiasing: false
    }
    
    Rectangle {
        width: 100
        height: 20
        color: "#5de16b"
        scale: 2
        rotation: 30
    }
}

有注释版本:

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15

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

    Rectangle {                 // 矩形控件
        x: 100
        y: 100
        z: 1                    // z坐标,层级。默认为0,且后面控件覆盖前面控件
        width: 100
        height: 50
        gradient: Gradient {    // 用Gradient控件实现渐变效果
            GradientStop { position: 0.0; color: "lightsteelblue" }
            GradientStop { position: 1.0; color: "blue" }
        }
    }

    Rectangle {
        x: 120
        y: 120
        width: 100
        height: 50
        gradient: Gradient {
            GradientStop { position: 0.0; color: "red" }
            GradientStop { position: 1.0; color: "pink" }
        }
    }

    Rectangle {
        id: conterRect
        anchors.centerIn: parent        // 相对父控件水平垂直居中
        width: 200
        height: 50
        color: "#c4c4c4"
        Text {
            id: center
            text: qsTr("居中")
            color: "white"
            anchors.centerIn: parent
        }
    }

    Rectangle {
        anchors.horizontalCenter: parent.horizontalCenter       //  相对父控件水平居中
        width: 200
        height: 50
        color: Qt.rgba(77/255,76/255,167/255,1)
        Text {
            id: horizontal
            text: qsTr("水平居中")
            color: "white"
            anchors.centerIn: parent
        }
    }

    Rectangle {
        id: verticalRect
        anchors.verticalCenter: parent.verticalCenter           //  相对父控件垂直居中
        width: 200
        height: 50
        color:"#0857C9"
        Text {
            id: vertical
            text: qsTr("垂直居中")
            color: "white"
            anchors.centerIn: parent
        }
    }


    Rectangle {
        anchors.top: verticalRect.bottom                    //  本控件的上方是verticalRect控件的下方
        anchors.topMargin: 20                               //  本控件的上方外边距20
        width: 200
        height: 50
        color: "#7b7b7b"
        radius: 20
    }

    Rectangle {
        anchors.top: conterRect.bottom
        anchors.topMargin: 20
        anchors.left: verticalRect.right                    //  本控件的左方是verticalRect控件的右方
        anchors.leftMargin: 20                              //  本控件的左方外边距20
        width: 200
        height: 50
        color: "#7b7b7b"
        radius: 20                                          //  圆角
        antialiasing: false                                 //  是否抗锯齿
    }

    Rectangle {
        width: 100
        height: 20
        color: "#5de16b"
        scale: 2                    // 缩放倍数
        rotation: 30                // 旋转角度
    }
}

标签:控件,anchors,width,color,height,Item,QML,Rectangle
From: https://blog.51cto.com/zhen/6508128

相关文章

  • WPF ListBoxItem 取消hover高亮,仅显示选中高亮
     ListBoxItem取消hover高亮,仅显示选中高亮;其他控件与此类似 <StyleTargetType="{x:TypeListBoxItem}"><SetterProperty="Background"Value="Transparent"/><SetterProperty="Template">......
  • MySQL如何初始化常量Item?
    MySQL中的一切表达式都是继承自Item类,常量也不外乎如此。以Item_float为例子说明MySQL如何初始化常量Item。首先在Parser里面:NUM_literal:NUM{interror;$$=new(YYTHD->mem_root)Item_int($1,......
  • 使用sessionStorage获取值和设置值 sessionStorage.setItem('key','value') sessionS
    使用sessionStorage获取值和设置值sessionStorage.setItem('key','value')sessionStorage.getItem('myname')https://www.shuzhiduo.com/A/lk5a4ZL2J1/<body><buttonid="btn1">设置值</button><buttonid="btn2&......
  • 【阿里巴巴中国站API接口系列】获得1688商品详情信息-item_get-获得1688商品详情调用
    ​    1688有开放商品详情API接口,使用前需要注册成为开发者并申请API权限。以下是简单的API使用步骤:1. 获取授权key和secret:在开放平台注册获取key和secret接入。2. 构建请求:通过API接口文档构建请求,包括传递必要参数和权限设置等。3. 发送请求:使用HTTP GET或POS......
  • 【淘宝api开发系列】获得商品详情API|item_get-获得淘宝商品详情调用示例教程
    ​淘宝商品详情是指在淘宝上展示的一个商品的详细信息,包括商品的名称、图片、价格、规格参数、用户评价等内容。在商家上传商品时,一般会根据实际情况填写商品信息,并可以添加多张图片来展示商品的外观和功能特点。同时,商家也可以在商品详情中编写文字描述,详细介绍商品的特点、优势......
  • vue使用localStorage.setItem()存储对象数据的注意事项
    如数据对象:ruleForm:{name:'',password:'',},使用localStoragelocalStorage.setItem("person",JSON.stringify(this.ruleForm));取值localStorage.getItem("person")如果不将对象转换为string类型,在取值的时候就只会取到类似[Object,......
  • Go语言中的omitempty字段
    packagemainimport(  "encoding/json"  "fmt")typePersonstruct{  Namestring`json:"name"`  Age int  `json:"age"`  Addrstring`json:"addr,omitempty"`}funcmain(){  p1:=Person{   ......
  • Item 1: Consider static factory methods instead of constructors
    实际应用:packagejava.lang;publicfinalclassBooleanimplementsjava.io.Serializable,Comparable<Boolean>{publicstaticfinalBooleanTRUE=newBoolean(true);publicstaticfinalBooleanFALSE=newBoolean(false);....privatef......
  • 在DevExpress的GridView的列中,使用RepositoryItemSearchLookUpEdit控件实现产品列表信
    有时候,我们为了方便,我们往往使用扩展函数的代码方式创建很多GridView的操作功能,如在随笔《在DevExpress中使用BandedGridView表格实现多行表头的处理》中介绍过多行表头的创建及绑定处理,在《基于DevExpress的GridControl实现的一些界面处理功能》也介绍了一些特殊的展示效果,本篇随......
  • __getitem__方法
    当实例对象做p[key]运算时,会调用类中的方法__getitem__形式一:__getitem__(self,index)形式二:__getitem__(self,key)魔法方法__getitem__可以让对象实现迭代功能,这样就可以使用for…in…来迭代该对象了在用for…in…迭代对象时,如果对象没有实现__iter__、__next__......