qml之ui控件

news/2024/10/18 3:30:36/

文章目录

  • ui控件
  • 移动版风格
  • 嵌套页面
  • 并排界面

ui控件

Qt Quick控件用于创建由标准化组件(如按钮、标签、滑块等)构建的用户界面。

  • QtQuick.Controls:基本控件。
  • QtQuick.Templates:为控件提供行为化的、非可化视的基本类型。
  • QtQuick.Controls.Imagine:为Imagine主题风格提供支持。
  • QtQuick.Controls.Material:为Material主题风格提供支持。
  • QtQuick.Controls.Universal:为Universal主题风格提供支持。
  • Qt.labs.platform:支持常用对话框,如文件、颜色等,以及系统图标和标准路径。
    使用qml创建一个ApplicationWindow程序排版与GUI程序的QMainWindow类似。
    在这里插入图片描述
     我们主要要实现一个图片查看器的例子。
    在这里插入图片描述
    添加工具栏。在工具栏内部,添加了一个Flow元素,里面放置了一个工具按钮。
header: ToolBar { Flow { anchors.fill: parent ToolButton { text: qsTr("Open") icon.source: "images/open.png" onClicked: fileOpenDialog.open() } 
} 
} 

fileOpenDialog元素来自Qt.labs.platform模块。文件对话框可用于打开或保存文件。

    Platform.FileDialog{id:fileOpenDialogtitle: "Select an image file"folder: StandardPaths.writableLocation(StandardPaths.DocumentsLocation)nameFilters: ["Image files (*.png *.jpg)"]onAccepted: {image.source = fileOpenDialog.file}}

添加菜单栏。

    menuBar: MenuBar{Menu{title: "&File"MenuItem{text:"&Open..."icon.source:"images/open.png"onTriggered: fileOpenDialog.open()}}Menu{title: "&Help"MenuItem{text:"&About..."onTriggered: aboutDialog.open()}}}

完整代码

import QtQuick
import QtQuick.Controls
import Qt.labs.platform as PlatformApplicationWindow  {width: 640height: 480visible: truetitle: qsTr("Hello World")background: Rectangle{color:"darkGray"}Image {id: imageanchors.fill: parentfillMode: Image.PreserveAspectFitasynchronous: true}header: ToolBar{Flow{anchors.fill: parentToolButton{text:"打开"icon.source:"images/open.png"onClicked: fileOpenDialog.open()}}}Platform.FileDialog{id:fileOpenDialogtitle: "Select an image file"folder: StandardPaths.writableLocation(StandardPaths.DocumentsLocation)nameFilters: ["Image files (*.png *.jpg)"]onAccepted: {image.source = fileOpenDialog.file}}menuBar: MenuBar{Menu{title: "&File"MenuItem{text:"&Open..."icon.source:"images/open.png"onTriggered: fileOpenDialog.open()}}Menu{title: "&Help"MenuItem{text:"&About..."onTriggered: aboutDialog.open()}}}Dialog{id: aboutDialogwidth: 300;height:150anchors.centerIn: parenttitle:"About"Label{horizontalAlignment: Text.AlignHCentertext:"aaaaaaaaa\nbbbbbbbb"}standardButtons: Platform.StandardButton.Ok}
}

移动版风格

 主要是要实现一个类似手机版的图片查看器。
在这里插入图片描述
首先,需要改变样式。

QQuickStyle::setStyle("Material");

用侧滑菜单(Drawer)替换菜单。

    Drawer{id: drawerwidth: parent.width/3*2height: parent.heightListView{anchors.fill: parentmodel: ListModel{ListElement{text: "Open..."triggered:function(){fileOpenDialog.open();}}ListElement{text: "About..."triggered:function(){aboutDialog.open();}}}delegate: ItemDelegate{text:model.texthighlighted: ListView.isCurrentItemonClicked: {drawer.close()model.triggered()}}}}

完整代码

import QtQuick
import QtQuick.Controls
import Qt.labs.platform as Platform
import QtQuick.Controls.MaterialApplicationWindow  {width: 320height: 480visible: truetitle: qsTr("Image Viewer")background: Rectangle{color:"darkGray"}Image {id: imageanchors.fill: parentfillMode: Image.PreserveAspectFitasynchronous: true}header: ToolBar{Material.background: Material.OrangeToolButton{icon.source: "images/baseline-menu-24px"onClicked: drawer.open()}Label{text:"Image Viewer"font.pixelSize: 20anchors.centerIn: parent}}Platform.FileDialog{id:fileOpenDialogtitle: "Select an image file"nameFilters: ["Image files (*.png *.jpg)"]onAccepted: {image.source = fileOpenDialog.file}}Drawer{id: drawerwidth: parent.width/3*2height: parent.heightListView{anchors.fill: parentmodel: ListModel{ListElement{text: "Open..."triggered:function(){fileOpenDialog.open();}}ListElement{text: "About..."triggered:function(){aboutDialog.open();}}}delegate: ItemDelegate{text:model.texthighlighted: ListView.isCurrentItemonClicked: {drawer.close()model.triggered()}}}}Dialog{id: aboutDialogwidth: 300;height:150anchors.centerIn: parenttitle:"About"Label{horizontalAlignment: Text.AlignHCentertext:"aaaaaaaaa\nbbbbbbbb"}standardButtons: Platform.StandardButton.Ok}
}

嵌套页面

我们将创建一个页面树,可以通过上级页面访问下级页面。
在这里插入图片描述
该用户界面的关键组件是StackView。它允许我们将页面放在一个堆栈(stack)上,当用户想要返回时,可以弹出(pop)该堆栈。
在这里插入图片描述

import QtQuick
import QtQuick.Window
import QtQuick.ControlsApplicationWindow {id:windowwidth: 320height: 480visible: truetitle: qsTr("Stack")header: ToolBar{ToolButton{text:stackView.depth>1?"\u25C0":"\u2630"font.pixelSize: Qt.application.font.pixelSize*1.6onClicked: {if(stackView.depth>1){stackView.pop()}else{drawer.open()}}}Label{text:stackView.currentItem.titleanchors.centerIn: parent}}Drawer{id:drawerwidth: window.width*0.66;height: window.heightColumn{anchors.fill:parentItemDelegate{text:"Profile"width: parent.widthonClicked: {stackView.push("Profile.qml")drawer.close()}}ItemDelegate{text:"About"width: parent.widthonClicked: {stackView.push(aboutPage)drawer.close()}}}}StackView{id :stackViewanchors.fill : parentinitialItem: Home{}}Component{id:aboutPageAbout{}}
}

这边贴出main.qml的代码,具体控件可以查看底部仓库代码链接。

并排界面

 对于这个示例,我们创建了一个用户界面,该界面由三个页面组成,用户可以在其中切换。
在这里插入图片描述
 该用户界面的关键组件是SwipeView。PageIndicator(底部的三个点)显示用户当前处于活动状态的页面,这有助于导航。

import QtQuick
import QtQuick.Window
import QtQuick.ControlsApplicationWindow {id:windowwidth: 320height: 480visible: truetitle: qsTr("sidebyside")SwipeView{id :swipeViewanchors.fill : parentHome{}About{}EditProfile{}Profile{}}PageIndicator{anchors.bottom: parent.bottomanchors.horizontalCenter: parent.horizontalCentercurrentIndex: swipeView.currentIndexcount: swipeView.count}
}

完整代码链接


http://www.ppmy.cn/news/1185660.html

相关文章

Django 尝试SSE报错 AssertionError: Hop-by-hop headers not allowed 的分析

情况描述 近期计划测试一下django对日志打印的支持,一般都是用websocket的方式,想测试一下SSE (Server-sent events)的服务端推送,发现过程中存在报错: Traceback (most recent call last):File "D:\Software\Anaconda3\li…

Flask后端开发(二) - 功能实现和项目总结

目录 1. 功能1:修改文件参数值1.1. 获取网页端传参1.2. 读取文件1.2.1. 一般文件读取方式1.2.2. 特殊文件 —— mlx文件1.2.3. 特殊文件 —— .xlx文件1.3. 查找数据修改位置,替换数据2. 功能2:读取结果数据2.1. 实时数据展示如何存储相关数据?2.2. 读取相关数据,整理、打…

550MW发电机变压器组继电保护的整定计算及仿真

摘要 电力系统继电保护设计是根据系统接线图及要求选择保护方式,进行整定计算,电力系统继电保护的设计与配置是否合理直接影响到电力系统的安全运行。如果设计与配置不当,保护将不能正确工作,会扩大事故停电范围,造成…

MySQL数据库干货_07—— MySQL中的约束

MySQL中的约束 本专栏从本篇开始正式介绍MySQL中的约束内容,这是关系型数据库的一个重点,在接下来的几篇博文中我会详细介绍每种约束,包括概念,创建方式,应用场景等等,希望小伙伴们关注!约束概…

深度强化学习用于博弈类游戏-基础测试与说明【1】

深度强化学习用于博弈类游戏-基础【1】 1. 强化学习方法2. 强化学习在LOL中的应⽤2.1 环境搭建2.2 游戏特征元素提取1)小地图人物位置:2)人物血量等信息3)在整个图像上寻找小兵、防御塔的位置4)自编码器提取3. 策略梯度算法简介参考资料1. 强化学习方法 伴随着人工智能的潮起…

springboot--基本特性--自定义 Banner

SpringApplication的使用 前言效果1.1 自定义banner1.2 自定义SpringApplication配置文件优先级高于程序化调整的优先级启动自定义banner关闭自定义banner 1.3 FluentBuilder API 前言 修改启动时候的修改banner 效果 1.1 自定义banner banner制定官网链接 在配置文件中设置…

【数据结构】 队列详解!庖丁解牛般细致讲解!

🎥 屿小夏 : 个人主页 🔥个人专栏 : 数据结构解析 🌄 莫道桑榆晚,为霞尚满天! 文章目录 📑前言🌤️队列的概念剖析☁️什么是队列☁️队列的特性☁️队列的图解 &#x1…

Power BI 傻瓜入门 17. 共享和Power BI工作区

本章内容包括: 设置与Power BI服务的共享和协作使用监控和性能工具加快业务运营通过查看数据联机排除数据故障 在经历了跨数据源的整个数据生命周期、构建可视化、了解DAX和发布报告之后,作为power BI的高级用户,您的下一步是与业务中的所有…