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

Qt Quick快速入门笔记

Qt Quick快速入门笔记

  • 基本的程序结构
int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endifQGuiApplication app(argc, argv);QQmlApplicationEngine engine;const QUrl url(QStringLiteral("qrc:/main.qml"));QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,&app, [url](QObject *obj, const QUrl &objUrl) {if (!obj && url == objUrl)QCoreApplication::exit(-1);}, Qt::QueuedConnection);// 引擎加载qmlengine.load(url);return app.exec();
}
import QtQuick 2.12
import QtQuick.Window 2.12Window {width: 640height: 480visible: truetitle: qsTr("Hello World")
}
  • Window

查看qml渲染效果

选中对应qml-> 顶部工具 ->外部-> Qt Quick -> Qt Qucik Preview

基础组件体验

Item

Item是Qt Quick所有可视元素的基类,定义了大部分属性,包括。x,y,z,width,height,anchors,按键响应等。可以将多个Item嵌套在一起形成复杂界面布局.z数值越大,越在图层上面

Item {focus: true// 回车按键响应Keys.onReturnPressed: console.log("Pressed return");
}

Rectangle

矩形区域,提供颜色、边框、圆角属性等

Rectangle {width: 100height: 100color: "red"// color: Qt.rgba(0.4,0.5,0.8,1)border.color: "black"border.width: 5radius: 10anchors.centerIn: parentComponent.onCompleted:{console.log("Rectangle Completed!")}}

anchors 锚布局

指定控件与其他控件之间的位置关系centerIn、left、right、top、bottom、horizontalCenter、verticalCenter等

Rectangle {id: testwidth: 100height: 100color: Qt.rgba(0.4,0.5,0.8,1)border.color: "black"border.width: 5radius: 10anchors.centerIn: parent}Button {// Button 左边靠着Rectangle 间隔20,顶部y持平anchors.left: test.rightanchors.leftMargin: 20anchors.top: test.toptext: "Ok"onClicked: console.log("click button")
}

Text

Text支持多种文本格式html、markdown

Text {text: "Hello World!"font.family: "Helvetica"color: "red"// 字体自适应fontSizeMode: Text.Fitfont.pixelSize:  50 // 字体最大尺寸minimumPixelSize: 12 // 字体最小尺寸
}

Label

Label
{id: usernamefont.pixelSize: 22font.italic: truecolor: "steelblue"text: "账号:"
}

TextField

TextField
{placeholderText: "请输入用户名"x: 10y: 10background: Rectangle{implicitHeight: 40implicitWidth: 200color: "red"border.color: "black"}// 密码样式echoMode: TextInput.Password
}

TextInput

TextInput与TextField类似

TextInput没有背景是透明的

TextField有背景色

MouseArea {anchors.fill: parentonClicked: () => {// 获取验证结果console.log("result = ", testValidator.acceptableInput)}
}TextInput
{id: testValidatorx: 10y: 10width: 100height: 30// 验证输入validator: IntValidator{ bottom: 100; top: 999}
}

TextEdit

MouseArea {anchors.fill: parentonClicked: () => {console.log("selectionStart = ", edit.selectionStart)console.log("selectionEnd = ", edit.selectionEnd)console.log("selectedText = ", edit.selectedText)// 搜索文本var tex = edit.getText(0, edit.text.length)console.log("tex = ", tex)let index = tex.indexOf("el")console.log("index = ", index)}
}TextEdit {id: editwidth: 240text: "<b>Hello</b> <i>World!</i>"font.family: "Helvetica"font.pointSize: 20color: "blue"focus: true// persistentSelection: true
}

TextArea

TextArea
{x: 10y: 20background: Rectangle{implicitWidth: 100implicitHeight: 80border.color: "blue"border.width: 2}placeholderText: "请输入多行文本"font.pixelSize: 18color: "red"
}// 滚动条
ScrollView {width: 100height: 200TextArea{x: 10y: 20background: Rectangle{implicitWidth: 100implicitHeight: 80border.color: "blue"border.width: 2}placeholderText: "请输入多行文本"font.pixelSize: 18color: "red"}
}

Button

Button系列包括:CheckBox(勾选框)、DelayButton(延时按钮)、RadioButton(单选框)、RoundButton(圆形按钮)、Switch(开关)、ToolButton

Button {anchors.centerIn: parenttext: "Ok"onClicked: console.log("click button")
}// buttonStyle案例
Component {id: mystyle// 自定义按钮外观ButtonStyle {background: Rectangle {implicitHeight: 45implicitWidth: 150radius: 10border.width: 2border.color: "red"//color: "blue"}}Button {text: "测试数据"style: mystyle}
}

CheckBox

// CheckBox 示例
Rectangle
{width: 150height: 100color: "yellow"border.color: "orange"border.width: 2CheckBox {x: 0y: 0text: "JavaScript"onCheckedChanged:{console.log("status = ", checked)}}CheckBox {x: 0y: 40text: "java"}
}// 互斥单选box
ButtonGroup
{id: testButtonGroupexclusive: true
}
CheckBox {ButtonGroup.group: testButtonGroupx: 0y: 0text: "JavaScript"
}CheckBox {ButtonGroup.group: testButtonGroupx: 0y: 40text: "java"
}

Repeater

Column
{spacing: 10width: 100height: 400anchors.centerIn: parentRepeater{id: testRepeatermodel: 5property var titles: ["ID:", "姓名", "班级"]property var values: ["02220455", "张三", "6班"]Row{spacing: 3Text {text: testRepeater.titles[index]color: "black"font: {bold: truepointSize: 16}}Text {text: testRepeater.values[index]color: "red"font: {bold: truepointSize: 16}}}}
}

GroupBox

GroupBox用于组织和分组其他控件的容器

Item {width: 300height: 200anchors.centerIn: parentGroupBox{title: "groupbox"anchors.centerIn: parentwidth: 250height: 180CheckBox {x: 0y: 0text: "JavaScript"}CheckBox {x: 0y: 40text: "java"}}
}

RadioButton

Item {width: 300height: 200anchors.centerIn: parentGroupBox{title: "groupbox"anchors.centerIn: parentwidth: 250height: 180RadioButton {x: 0y: 0text: "JavaScript"}RadioButton {x: 0y: 40text: "java"}}
}

Slider

Rectangle {width: 100height: 500Slider {id: sliderfrom: 1value: 25to: 100}MouseArea{anchors.fill: parentonClicked: () => {console.log("value = " , slider.value.toFixed(2))// position 范围是0-1console.log("position = " , slider.position)}}
}

Image

// 加载本地图片
Image {asynchronous: truesource: "qrc:/03.png"  // qrc中的图片资源
}// 充满父视图的背景
Image {anchors.fill: parentsource: ":/test/333.jpg"  // qrc中的图片资源}// 网络图片
Image {id: imageViewwidth: 100height: 40asynchronous: truesource: "http://www.baidu.com/img/pcdoodle_2a77789e1a67227122be09c5be16fe46.png"//fillMode: Image.PreserveAspectCroponStatusChanged:{console.log("imageView statu = ", imageView.status)}
}

BusyIndicator

// 加载器
// 图片还在加载的时候显示加载器
BusyIndicator {running: imageView.status === Image.Loadinganchors.centerIn: parent}Image {id: imageViewwidth: 100height: 40asynchronous: truesource: "http://www.baidu.com/img/pcdoodle_2a77789e1a67227122be09c5be16fe46.png"//fillMode: Image.PreserveAspectCroponStatusChanged:{console.log("imageView statu = ", imageView.status)}
}

FileDialog

FileDialog {id: fileDialogtitle: "Please choose a file"folder: shortcuts.homeonAccepted: {console.log("You chose: " + fileDialog.fileUrls)}onRejected: {console.log("Canceled")}
}Button
{text: "打开"onClicked: fileDialog.visible = true
}

ComboBox

ComboBox {// 默认选择currentIndex: 2displayText: "语言: " + currentTextmodel: ["java", "javaScript", "C++"]
}ComboBox {id: boxanchors.centerIn: parenttextRole: "key"valueRole: "value"model: ListModel{id: modelDataListElement {key: "first"; value: 1}ListElement {key: "second"; value: 2}ListElement {key: "third"; value: 3}}onActivated: (index) => {print("onActivated ", index)print("currentIndex ", currentIndex)print("currentValue ", currentValue)print("currentText ", currentText)}
}Button
{text: "删除"anchors.top: box.bottomanchors.topMargin: 10anchors.horizontalCenter: parent.horizontalCenteronClicked:{modelData.remove(box.currentIndex, 1)}
}

ListView

Rectangle {id: topViewwidth: 400height: 300color: "red"ScrollView{// 滚动条anchors.fill: parentListView{id: listViewanchors.fill: parentmodel:  ListModel{id: listDataListElement{name: "first"number: "123"}ListElement{name: "second"
http://www.xdnf.cn/news/11979.html

相关文章:

  • 国产三维CAD皇冠CAD在「金属压力容器制造」建模教程:蒸汽锅炉
  • 音乐播放器小程序设计与实现 – 小程序源码分享
  • typescript中的type如何使用
  • gitlab rss订阅失败
  • LeetCode 3226.使两个整数相等的位更改次数
  • SkyWalking架构深度解析:分布式系统监控的利器
  • Docker容器化的文件系统隔离机制解析
  • 解决com.jcraft.jsch.JSchException: Algorithm negotiation fail
  • CppCon 2015 学习:Beyond Sanitizers
  • 全角转半角函数(APP)
  • Viggle:开启视频人物替换新纪元
  • 书籍转圈打印矩阵(8)0604
  • Reids 如何处理缓存穿透、缓存击穿、缓存雪崩问题?
  • 使用ArcPy进行栅格数据分析
  • 麒麟+ARM架构安装mysql8的操作指南
  • 各个布局的区别以及示例
  • Sql Server 中常用语句
  • 计算机系统结构-第五章-目录式协议
  • psycopg2-binary、pgvector、 SQLAlchemy、 PostgreSQL四者的关系
  • 【无人机】无人机UAV、穿越机FPV的概念介绍,机型与工具,证书与规定
  • JavaScript性能优化实战:深入探讨JavaScript性能瓶颈与优化技巧
  • UE5 创建了一个C++类,现在我还有一个蓝图类,我想将编写的C++类中包含的功能加入到这个蓝图类里面,如何做到
  • 2025年渗透测试面试题总结-腾讯[实习]安全研究员(题目+回答)
  • P3156 【深基15.例1】询问学号
  • Windows系统工具:WinToolsPlus 之 SQL Server 日志清理
  • Centos 8系统ext4文件系统类型进行扩容缩容 (LVM)
  • FFMPEG 提取视频中指定起始时间及结束时间的视频,给出ffmpeg 命令
  • C#学习第27天:时间和日期的处理
  • 开发源码搭建一码双端应用分发平台教程:逐步分析注意事项
  • H5移动端性能优化策略(渲染优化+弱网优化+WebView优化)