当前位置: 首页 > news >正文

Qml之基本控件

一.Qml常用控件

1.Text(显示普通文本和富文本)

1.1显示普通文本:

Window {

    visible: true

    width: 320

    height: 240

    title: qsTr("Hello World")

    Text {

          text: "Hello World!"

          font.family: "Helvetica"

          font.pointSize: 24

          color: "red"

    }

}

1.2显示富文本。

Window {

    visible: true

    width: 320

    height: 240

    title: qsTr("Hello World")

    Text {

        text: "<b>Hello</b> <i>World!</i>"

    }

}

2.Button(按钮控件)

需要导入QtQuick.Controls 2.xx,如import QtQuick.Controls 2.12。

Window {

    visible: true

    width: 200

    height: 120

    title: qsTr("Hello World")

    Button {

        text: "Ok"

        onPressed: { //下压

            console.log("pressed " + text)

        }

        onReleased: { //释放

            console.log("released " + text)

        }

        onClicked: { //单击,触发一次pressed和一次released

            console.log("click " + text)

        }

        onDoubleClicked: { //双击

            console.log("doubleClick " + text)

        }

        onPressAndHold: { //长按,下压后不松手一段时间后触发

            console.log("pressAndHold " + text)

        }

        onCanceled: { //下压后,在释放之前失去焦点

            console.log("cancel " + text)

        }

    }

}

onCanceled的触发方法:按住按钮不放,然后键盘按Alt+Tab,让它失去焦点。

3.RadioButton(单选按钮)

Window {

    visible: true; width: 200; height: 200

    ColumnLayout {

        RadioButton {

            checked: true

            text: "r1"

        }

        RadioButton {

            text: "r2"

        }

        RadioButton {

            text: "r3"

        }

    }

}

4.CheckBox(多选按钮)

import QtQuick 2.12

import QtQuick.Window 2.12

import QtQuick.Controls 2.12

import QtQuick.Layouts 1.12

Window {

    visible: true; width: 200; height: 200

    ColumnLayout {

        CheckBox {

            id: c1

            checked: true

            text: "c1"

        }

        CheckBox {

            id: c2

            checked: false

            text: "c2"

        }

        CheckBox {

            id: c3

            checked: true

            text: "c3"

        }

    }

    Component.onCompleted: {

        console.log(c1.checked)

        console.log(c2.checked)

        console.log(c3.checked)

    }

}

5.ComboBox(下拉选项)

import QtQuick 2.12

import QtQuick.Window 2.12

import QtQuick.Controls 2.12

Window {

    visible: true; width: 200; height: 200

    ComboBox {

        model: ["1111", "2222", "3333"]

        onCurrentIndexChanged: {

            console.log(currentIndex)

        }

    }

}

6.ListView(列表)

import QtQuick 2.12

import QtQuick.Window 2.12

import QtQuick.Controls 2.12

Window {

    visible: true; width: 400; height: 300

    Column {

        ListView {

            id: list

            width: 100

            height: 200

            model: [

                { name: '宝马', price: '10'},

                { name: '奔驰', price: '50'},

                { name: '大众', price: '100'}

            ]

            delegate: ItemDelegate {

                width: list.width

                text: modelData.name + ": " + modelData.price + (list.currentIndex === index ? ' √' : '')

                background: Rectangle {

                    color: getColor()

                    function getColor() {

                        return Qt.rgba(Math.random(), Math.random(), Math.random())

                    }

                }

                onClicked: {

                    list.currentIndex = index

                    console.log(JSON.stringify(modelData))

                }

            }

            ScrollBar.vertical: ScrollBar {}

        }

        Button {

            onClicked: {

                let model = list.model

                model.push({name: "123", price: "123"})

                list.model = model

            }

        }

    }

}

7.Timer(定时器)

import QtQuick 2.12

import QtQuick.Window 2.12

import QtQuick.Controls 2.12

Window {

    visible: true; width: 200; height: 120

    Label {

        Timer {

            interval: 1000

            repeat: true

            running: true

            triggeredOnStart: true

            onTriggered: {

                parent.text = Qt.formatDateTime(new Date(), 'yyyy-MM-dd hh:mm:ss')

            }

        }

    }

}

8.SwipeView(滑动窗口)

  说明:可以见手指滑动变换窗口

import QtQuick 2.12

import QtQuick.Window 2.12

import QtQuick.Controls 2.12

Window {

    visible: true; width: 200; height: 120

    SwipeView {

        id: view

        anchors.fill: parent

        Repeater {

            model: 3

            Rectangle {

                color: view.currentIndex == 0 ? 'red' : view.currentIndex == 1 ? 'yellow' : 'white'

                Text {

                    anchors.centerIn: parent

                    text: 'text' + view.currentIndex

                }

            }

        }

    }

    PageIndicator {

        anchors.bottom: view.bottom

        count: view.count

        currentIndex: view.currentIndex

    }

}

9.输入框

 TextInput{

                id:intpu_Z

                anchors.left: parent.left

                anchors.leftMargin: 5

                anchors.right: parent.right

                anchors.rightMargin: 5

                anchors.verticalCenter: parent.verticalCenter

                color: "white"

                font{

                    pointSize: maps_Root.fontSize

                    bold: true

                }

                clip: true

            }

10.Item

   Item是基本的QML类型,所有可视类型都是Item的子类。  

   Item是QtQuick所有可视元素的基类,其属性有 x、y、width、height 、anchoring、key、z、rotation等,具体的使用如下,修改自定义Rectangle的显示位置等信息;

11.Rectangle

   在 QML 中,Rectangle 是一个常用的基础图形元素,用于绘制矩形形状。它继承自 Item 并提供了额外的属性来定义矩形的尺寸、颜色、边框等。

以下是一些 Rectangle 元素的关键属性和用法示例:

关键属性

width 和 height:定义矩形的宽度和高度。

color:定义矩形的填充颜色。

border.color:定义矩形边框的颜色。

border.width:定义矩形边框的宽度。

radius:定义矩形角的圆度,用于创建圆角矩形。

gradient:定义矩形的渐变填充,而不是纯色填充。

用法示例

以下是一个简单的 QML 示例,展示了如何使用 Rectangle 元素:

import QtQuick 2.15

Rectangle {

    width: 200

    height: 100

    color: "blue"

    border.color: "black"

    border.width: 4

    radius: 10

    // 在矩形中添加文本

    Text {

        text: "Hello, Rectangle!"

        color: "white"

        anchors.centerIn: parent // 使文本在矩形中居中

    }

}    

12.Dialog

import QtQuick 2.10

import QtQuick.Window 2.10

import QtQuick.Controls 2.3

ApplicationWindow{

    visible: true;

    width: 1280;

    height: 720;

    Dialog {

        id: dialog

        x:(parent.width-width)/2

        y:(parent.height-height)/2

        implicitWidth: 500;

        implicitHeight: 300;

        title: "Title"

        modal: true;

        standardButtons: Dialog.Ok | Dialog.Cancel

        onAccepted: console.log("Ok clicked")

        onRejected: console.log("Cancel clicked")

    }

    Button{

        text: "open";

        onClicked: {

            dialog.open();

        }

    }

}

http://www.lryc.cn/news/497539.html

相关文章:

  • 【Java从入门到放弃 之 Stream API】
  • Ruby On Rails 笔记1——Rails 入门
  • 高效开发 Python Web 应用:FastAPI 数据验证与响应体设计
  • 基于“开源 2+1 链动 O2O 商城小程序”的门店拉新策略与流程设计
  • 33.5 remote实战项目之设计prometheus数据源的结构
  • 微服务springboot详细解析(一)
  • 深入探讨Go语言中的双向链表
  • Fastapi + vue3 自动化测试平台---移动端App自动化篇
  • ElasticSearch easy-es 聚合函数 group by 混合写法求Top N 词云 分词
  • 在 ASP.NET C# Web API 中实现 Serilog 以增强请求和响应的日志记录
  • 2024年顶级小型语言模型前15名
  • 精通 Python 网络安全(一)
  • 【python自动化二】pytest集成allure生成测试报告
  • 网络版本的通讯录青春版(protobuf)
  • 开源模型应用落地-安全合规篇-用户输入价值观判断(三)
  • 神经网络入门实战:(十四)pytorch 官网内置的 CIFAR10 数据集,及其网络模型
  • 【Rust在WASM中实现pdf文件的生成】
  • 在MySQL中执行sum case when报错:SUM does not exist
  • 【openssl】相关指令
  • 实例分割详解
  • D87【python 接口自动化学习】- pytest基础用法
  • 浅谈MySQL路由
  • matlab中disp,fprintf,sprintf,display,dlmwrite输出函数之间的区别
  • 30.100ASK_T113-PRO 用QT编写视频播放器(一)
  • Linux-GPIO应用编程
  • opencvocr识别手机摄像头拍摄的指定区域文字,文字符合规则就语音报警
  • 微服务即时通讯系统(5)用户管理子服务,网关子服务
  • postgreSQL安装后启动有The application server could not be contacted问题
  • 架构05-架构安全性
  • 虚幻引擎---材质篇