QML-学习note1

1. QML基础知识部分

QML是一种描述性的脚本语言,文件格式以.qml结尾。支持javascript形式的编程控制。QML是Qt推出的Qt Quick技术当中的一部分。

2.QML常用元素

2.1Image(图像元素)

实例:

    Column{
        anchors.centerIn: parent
        spacing:8 //间隔
        Image{
            width: 60
            height: 60
            source: "/new/prefix1/qq.jpg" //加载资源文件路径
            fillMode: Image.Pad //填充模式不会变形
        }
        Image{
            width: 20
            height:40
            source: "/new/prefix1/qq.jpg" //加载资源文件路径
            fillMode: Image.PreserveAspectFit //图像填充模式:按照比例缩放填充,不裁剪
        }
        Image{
            width: 100
            height:100
            source: "/new/prefix1/qq.jpg" //加载资源文件路径
            fillMode: Image.TileVertically//图像填充模式:水平填充,垂直复制
        }
        Image{
            width: 200
            height:200
            source: "/new/prefix1/qq.jpg" //加载资源文件路径
            fillMode: Image.TileHorizontally//图像填充模式:垂直填充,水平复制
        }

    }

运行效果:

2.2 Rectangle(矩形元素)

实例:

Rectangle{
        width:640
        height:480
        color:"yellow"

        Rectangle{
            width:30
            height:30
            color:"lightgreen"
        }
    }
    Rectangle{
        x:30
        y:100
        width:300
        height:300
        color:"red"


        Rectangle{
            x:33
            y:44
            width:30
            height:30
            color:"black"
        }
    }

运行效果:

2.3Text(文本元素)

实例:

Rectangle{
        x:40
        y:40
        width: 250
        height: 75
        color: "lightgreen"
        border.color:"red"
        border.width: 1

        Text{
            x:20
            y:20
            text:"勇敢加油干"
            font.family: "楷体"//字体名称
            font.pixelSize: 30//字体大小
            font.bold: true//粗体设置

        }
    }

运行效果:

2.4 Button(按钮元素)

实例:

 //定义变量统计单击次数
    property  int  count: 1

    Button{
        id:mybutton1
        x:200
        y:150
        width:120
        height: 40

        text:"测试点击按钮次数"

        //信号槽连接:单击信号
        onClicked: {
            console.log("你好,mybutton1按钮已经被你单击:"+ count++)
        }
    }

运行效果:

2.5 BusyIndicator(繁忙指示器)

实例:

BusyIndicator{
        id:mybusyIndicator

        //anchors:锚点布局
        anchors.centerIn: parent
        implicitHeight: 200
        implicitWidth: 200

        contentItem: Item{
            Rectangle{
                id:myrect
                width: parent.width
                height: parent.height
                color: Qt.rgba(0,0,0,0);
                radius: width/2;
                border.width: width/8;
                visible: false;//边框是否可见
            }
            ConicalGradient{
                width:myrect.width
                height: myrect.height

                gradient: Gradient{
                    GradientStop {position: 0.0;color: "chartreuse"} //圆圈颜色
                    GradientStop {position: 1.0; color: "orange"}//跟踪圆圈旋转后面的颜色
                }
                source: myrect
                Rectangle{
                    anchors.top:parent.top
                    anchors.horizontalCenter: parent.horizontalCenter
                    width: myrect.border.width
                    height: width
                    radius: width/2
                    color: "red" //红色圆圈旋转
                }
                RotationAnimation on rotation {
                    from:0
                    to:360
                    duration: 2000
                    loops: Animation.Infinite //循环
                }
            }
        }
    }

运行效果:

3.QML鼠标与键盘事件处理

3.1 QML鼠标事件处理

QML直接提供MouseArea来捕获鼠标事件,该操作必须配合Rectangle获取指定区域内的鼠标事件。

实例:

Rectangle{
        id:mouseareatest

        //anchors.centerIn: parent //通过锚将矩形定位到窗口正中心
        x:30
        y:30
        width:150
        height:150
        color:"azure"

        radius: 100 //0:直角   >0:圆角

        MouseArea{
            anchors.fill:parent
            acceptedButtons: Qt.AllButtons

            hoverEnabled: true //此属性为false,则鼠标在矩形区域内的进入、离开、移动动作捕获不到

            onPositionChanged: {
                console.log("监控区域提示:你当前移动的鼠标坐标为:("+mouseX+","+mouseY+")")
            }

            //点击事件
            onClicked: {
                if(mouse.button === Qt.LeftButton){
                    console.log("监控区域提示:你已经按下鼠标左键");
                }else if(mouse.button === Qt.RightButton){
                    console.log("监控区域提示:你已经按下鼠标右键");
                }else if(mouse.button === Qt.MidButton){
                    console.log("监控区域提示:你已经按下鼠标中间键");
                }
            }
        }

    }

运行效果:

3.2QML键盘事件处理

QML直接通过键盘事件Keys监控键盘任意按键,并且获取对应的消息

运行效果:

Rectangle{
        id:keysrecttest
        anchors.centerIn: parent

        width: 200
        height: 200
        color: "blue"

        radius: 100
        focus:true//不设置焦点,获取不了键盘事件
        Keys.onPressed: {
            console.log("key:"+event.key)
            console.log("scancode:"+event.nativeScanCode)
            console.log("text:"+event.text)
        }
        Keys.onTabPressed: {
            console.log("监控区域提示:你已经按下Tab键!")
        }
        Keys.onSpacePressed: {
            console.log("监控区域提示:你已经按下空格键!")
        }

    }

4.QML元素布局

4.1 锚(anchors)布局

锚布局是一种相对布局

实例:

    //Text靠最上边,水平居中
    Text{
        id:mytext

        anchors.horizontalCenter: parent.horizontalCenter;
        anchors.topMargin: 300;
        text:"text-anchors-test";
        color:"blue";
        font.pixelSize:25;//像素大小
    }

    Rectangle{
        id:myrect
        x:50
        y:100
        //anchors.left:mytext.left
        //anchors.right: mytext.right
        anchors.horizontalCenter: mytext.horizontalCenter;

        width: 300
        height: 100
        color: "cyan"; //矩形填充颜色
        border.width: 5;//矩形边框粗细程度
        border.color: "orange";//矩形边框颜色
        radius: 10;//圆角

        TextInput{
            id:mytext01
            width:100;
            height: 30;
            color: "black";
            focus: true;//给矩形设置焦点
            x:20;
            y:20;
        }
    }

    //显示图像
    Image{
        x:50
        y:300

        anchors.horizontalCenter: mytext.horizontalCenter;
        anchors.top:myrect.bottom;
        anchors.topMargin: 20;

        width: 400;
        height: 250;

        source:"file:/e:/picture/OIP-C.jfif"
        fillMode: Image.PreserveAspectFit;//按照比例填充

    }

运行效果:

4.2  常用布局方式

常用的布局方式:Row(行布局)、Cloumn(列布局)、Grid(网格布局)、Flow(流方布局)

实例:

//row布局
    Rectangle{
        id:myrowrect1
        x:30
        y:20
        width: 200
        height: 50
        border.width: 4
        border.color: "black"
        radius: 3   //圆角处理

        Row{
            anchors.centerIn: parent //相对背景父窗口居中
            spacing: 20 //设置行与行之间的间距
            Rectangle{color: "blue";width: 20;height: 20}
            Rectangle{color: "yellow";width: 20;height: 20}
            Rectangle{color: "red";width: 20;height: 20}
            Rectangle{color: "cyan";width: 20;height: 20}
            Rectangle{color: "green";width: 20;height: 20}
        }
    }

    //Column布局
    Rectangle{
        id:myrowrect2
        x:30
        y:80
        width: 50
        height: 200
        border.width: 4
        border.color: "red"
        radius: 3   //圆角处理

        Column{
            anchors.centerIn: parent //相对背景父窗口居中
            spacing: 10 //设置行与行之间的间距
            Rectangle{color: "blue";width: 20;height: 20}
            Rectangle{color: "yellow";width: 20;height: 20}
            Rectangle{color: "red";width: 20;height: 20}
            Rectangle{color: "cyan";width: 20;height: 20}
            Rectangle{color: "green";width: 20;height: 20}
        }
    }

    //Grid布局
    Rectangle{
        id:myrowrect3
        x:100
        y:80
        width: 200
        height: 100
        border.width: 4
        border.color: "black"
        radius: 3   //圆角处理

        Grid{
            anchors.centerIn: parent //相对背景父窗口居中
            spacing: 10 //设置行与行之间的间距
            rows:3
            Rectangle{color: "lime";width: 20;height: 20}
            Rectangle{color: "maroon";width: 20;height: 20}
            Rectangle{color: "aqua";width: 20;height: 20}
            Rectangle{color: "lightpink";width: 20;height: 20}
            Rectangle{color: "blue";width: 20;height: 20}
            Rectangle{color: "yellow";width: 20;height: 20}
            Rectangle{color: "red";width: 20;height: 20}
            Rectangle{color: "cyan";width: 20;height: 20}
            Rectangle{color: "green";width: 20;height: 20}
            Rectangle{color: "orange";width: 20;height: 20}
            Rectangle{color: "black";width: 20;height: 20}
            Rectangle{color: "brown";width: 20;height: 20}
            Rectangle{color: "burlywood";width: 20;height: 20}
            Rectangle{color: "moccasin";width: 20;height: 20}
            Rectangle{color: "magenta";width: 20;height: 20}
            Rectangle{color: "darkgreen";width: 20;height: 20}
            Rectangle{color: "aqua";width: 20;height: 20}
            Rectangle{color: "lightpink";width: 20;height: 20}
        }
    }

运行效果:

//Flow流方布局
    Flow{
        x:100
        y:200
        anchors.fill:parent //填充方式
        anchors.margins: 5
        spacing: 10 //设置行与行之间的间距
        Rectangle{color: "lime";width: 20;height: 20}
        Rectangle{color: "maroon";width: 20;height: 20}
        Rectangle{color: "aqua";width: 20;height: 20}
        Rectangle{color: "lightpink";width: 20;height: 20}
        Rectangle{color: "blue";width: 20;height: 20}
        Rectangle{color: "yellow";width: 20;height: 20}
        Rectangle{color: "red";width: 20;height: 20}
        Rectangle{color: "cyan";width: 20;height: 20}
        Rectangle{color: "green";width: 20;height: 20}
        Rectangle{color: "orange";width: 20;height: 20}
        Rectangle{color: "black";width: 20;height: 20}
        Rectangle{color: "brown";width: 20;height: 20}
        Rectangle{color: "burlywood";width: 20;height: 20}
        Rectangle{color: "moccasin";width: 20;height: 20}
        Rectangle{color: "magenta";width: 20;height: 20}
        Rectangle{color: "darkgreen";width: 20;height: 20}
        Rectangle{color: "aqua";width: 20;height: 20}
        Rectangle{color: "lightpink";width: 20;height: 20}
    }

Flow布局,元素会根据窗口的大小自动调整位置

运行效果:

5. 布局管理器-Qt Quick Layout

QML界面排列布局提供有两种:Anchors和Layout。Layout布局适用元素并列排布场景,元素之间没有重叠关系。Type:GridLayout、RowLayout、ColumnLayout等。

6. Quick Canvans 元素

Qt引入Canvans元素,它提供与分辨率无关的位图绘制机制,基本思想使用一个2D上下文对象渲染路径,包含所需的绘制函数,Canvans则像画板,直接填充、渐变、文本等诸多函数。

  • 8
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: QML-QianWindow-V1版本界面是一款用户界面设计程序,它为开发人员提供了大量可自定义的UI组件,使得开发者可以轻松地创建美观、直观且易于使用的界面。QML-QianWindow-V1版本界面具有界面美观,易用性强以及可扩展性强的特点。该程序提供了多种UI组件如窗口、按钮、文本框、下拉框、进度条等等。每个组件都可以基于开发需求进行个性化设计,提高了程序的可读性和可维护性。 QML-QianWindow-V1版本界面的窗口设计功能令人称赞。用户可以定制窗口的样式、位置和大小等属性,使其符合自己的需求。同时,该程序支持类似IOS与Android风格的Tab、Panel、Dialog等视图控件,让应用的界面更具丰富性和交互性,提升用户体验。 此外,QML-QianWindow-V1版本界面可让用户通过实用的数据模型,快速地创建和管理表格和列表,包括多行、多列的表格和单选或多选列表,使得用户可以方便地处理大量数据。 总之,QML-QianWindow-V1版本界面通过提供易用性和可扩展性强的UI组件来简化界面设计过程,使得开发人员可以迅速开发出优秀的桌面应用程序界面,以满足用户高品质的界面需求。 ### 回答2: QML-Qianwindow-v1版本是一款基于QML语言开发的桌面应用程序,其主要特点是界面设计美观、简单易用,适用于多种应用场景。 该软件的主界面包含五个主要区域:侧边栏、消息中心、主视图、小部件和设置菜单。 侧边栏主要用于导航,其中包含主要的应用程序和功能,使用户可以轻松地进入不同的模块。 消息中心用于显示来自其他应用程序的通知,例如电子邮件或要完成的任务。 主视图是QML-Qianwindow-v1的核心区域,其中包含常见的功能和工具,例如打开的文件、书签和搜索栏。 小部件区域用于显示用户选择的小部件,例如天气预报和日历。 设置菜单区域包含应用程序的设置菜单,用户可以在此处更改应用程序的选项、主题和常规设置。 总体来说,QML-Qianwindow-v1版本的界面设计非常简洁,用户友好,易于使用。它增强了用户的生产力和效率,为用户提供了一种令人愉悦的使用体验。 ### 回答3: QML-QianWindow-v1是一种基于Qt QML开发的简单易用的窗口界面库。其设计风格简洁明了,适用于各种桌面应用开发。 该库主要由三个部分组成:Title Bar、Main Body和Sidebar。其中,Title Bar是窗口的标题栏,可以自定义标题、图标、按钮、菜单等。Main Body是页面的主体内容,可以显示各种控件、窗口和布局。Sidebar是侧边栏,可以显示常用的菜单、工具、快捷方式等。 在QML-QianWindow-v1中,可以使用QianWindow来创建窗口。QianWindow提供了常用的窗口操作方法,例如最小化、最大化、关闭等。同时,该库还提供了很多常用的QML组件,例如按钮、文本框、下拉框、列表框等,这些组件可以方便快捷地进行页面设计。此外,在QML-QianWindow-v1中还支持自定义样式,可以根据需要修改组件的颜色、形状、大小等。 QML-QianWindow-v1的界面设计风格简洁明了,同时具有一定的美观度。尤其是Title Bar部分的设计非常灵活,可以根据需要自由定制。Main Body部分支持多种布局方式,可以满足不同页面设计的需求。Sidebar部分则提供了丰富的快捷操作方式,提高了用户的使用效率。 总的来说,QML-QianWindow-v1是一款非常实用的界面库,可以方便快捷地进行桌面应用开发。无论是初学者还是有经验的开发者,都可以轻松使用该库进行界面设计。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值