1.消息提示框
API:
属性 | 类型 | 默认值 | 说明 |
text | string | "" | 对话框文本 |
duration | int | 1500 | 对话框持续时间 |
type | string | "" | 对话框类型,值为空使用默认类型, “success”, “error”, “warning”, “info” |
测试代码:
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import QtGraphicalEffects 1.15
import "qrc:/components_qml/ShowToast"
ApplicationWindow {
id: mainWindow
width: 1000
height: 700
visible: true
title: "QML消息对话框"
color: "#F5F7FA"
//引用消息对话框
ShowToast {
id: toastDialog
}
// 主界面内容
ColumnLayout {
anchors.centerIn: parent
spacing: 30
Text {
text: "消息对话框演示"
font.pixelSize: 32
font.weight: Font.Bold
color: "#2C3E50"
Layout.alignment: Qt.AlignHCenter
}
// 对话框类型选择
RowLayout {
spacing: 15
Layout.alignment: Qt.AlignHCenter
Rectangle {
width: 10
height: 10
radius: 5
color: "#3498DB"
}
Text {
text: "选择对话框类型:"
font.pixelSize: 18
color: "#34495E"
}
}
// 按钮区域
GridLayout {
columns: 3
columnSpacing: 20
rowSpacing: 20
Layout.alignment: Qt.AlignHCenter
// 基础对话框
DialogButton {
text: "基础对话框"
bgColor: "#3498DB"
onClicked: {
toastDialog.text = "这是一个字符串";
toastDialog.show();
}
}
DialogButton {
text: "成功对话框"
bgColor: "#2ECC71"
onClicked: {
toastDialog.text = "操作成功!";
toastDialog.type = "success";
toastDialog.show();
}
}
DialogButton {
text: "错误对话框"
bgColor: "#E74C3C"
onClicked: {
toastDialog.text = "操作失败,请重试!";
toastDialog.type = "error";
toastDialog.show();
}
}
DialogButton {
text: "警告对话框"
bgColor: "#F39C12"
onClicked: {
toastDialog.text = "警告:资源不足";
toastDialog.type = "warning";
toastDialog.show();
}
}
DialogButton {
text: "信息对话框"
bgColor: "#3498DB"
onClicked: {
toastDialog.text = "您有3条新消息";
toastDialog.type = "info";
toastDialog.show();
}
}
}
}
// 对话框按钮组件
component DialogButton: Rectangle {
property alias text: buttonText.text
property alias icon: iconImage.source
property alias bgColor: bgRect.color
signal clicked
width: 250
height: 80
radius: 16
color: "transparent"
Rectangle {
id: bgRect
anchors.fill: parent
radius: 16
layer.enabled: true
layer.effect: DropShadow {
verticalOffset: 3
radius: 8
samples: 16
color: "#30000000"
}
}
RowLayout {
anchors.centerIn: parent
spacing: 15
Image {
id: iconImage
sourceSize: Qt.size(28, 28)
}
Text {
id: buttonText
font.pixelSize: 18
font.weight: Font.Medium
color: "white"
}
}
MouseArea {
anchors.fill: parent
cursorShape: Qt.PointingHandCursor
onClicked: parent.clicked()
}
}
}
2.加载提示框
API:
属性 | 类型 | 默认值 | 说明 |
text | string | "" | 对话框文本 |
showProgress | bool | false | 是否显示进度值 |
progress | int | 0 | 进度值 |
opaqueBackground | bool | true | 不透明背景 |
测试代码:
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import QtGraphicalEffects 1.15
import "qrc:/components_qml/ShowLoading"
ApplicationWindow {
id: mainWindow
width: 1000
height: 700
visible: true
title: "QML消息对话框"
color: "#F5F7FA"
//引用加载提示框
ShowLoading {
id: loadingIndicator
}
// 主界面内容
ColumnLayout {
anchors.centerIn: parent
spacing: 30
Text {
text: "消息对话框演示"
font.pixelSize: 32
font.weight: Font.Bold
color: "#2C3E50"
Layout.alignment: Qt.AlignHCenter
}
// 对话框类型选择
RowLayout {
spacing: 15
Layout.alignment: Qt.AlignHCenter
Rectangle {
width: 10
height: 10
radius: 5
color: "#3498DB"
}
Text {
text: "选择对话框类型:"
font.pixelSize: 18
color: "#34495E"
}
}
// 按钮区域
GridLayout {
columns: 3
columnSpacing: 20
rowSpacing: 20
Layout.alignment: Qt.AlignHCenter
DialogButton {
text: "显示加载提示框"
bgColor: "#3498DB"
onClicked: {
loadingIndicator.text = "正在加载数据...";
loadingIndicator.show();
// 模拟加载过程
loadingTimer.start();
}
}
DialogButton {
text: "带进度的加载框"
bgColor: "#9B59B6"
onClicked: {
loadingIndicator.text = "正在处理...";
loadingIndicator.showProgress = true;
loadingIndicator.show();
progressTimer.start();
}
}
DialogButton {
text: "透明加载框"
bgColor: "#1ABC9C"
onClicked: {
loadingIndicator.text = "后台处理中...";
loadingIndicator.opaqueBackground = false;
loadingIndicator.show();
loadingTimer.start()
}
}
}
}
// 定时器 - 模拟加载过程
Timer {
id: loadingTimer
interval: 2000
onTriggered: {
loadingIndicator.hide()
}
}
// 定时器 - 模拟进度更新
Timer {
id: progressTimer
interval: 100
repeat: true
property int progress: 0
onTriggered: {
progress += 2
loadingIndicator.progress = progress
if (progress >= 100) {
progress = 0
progressTimer.stop()
loadingIndicator.hide()
}
}
}
// 对话框按钮组件
component DialogButton: Rectangle {
property alias text: buttonText.text
property alias icon: iconImage.source
property alias bgColor: bgRect.color
signal clicked
width: 250
height: 80
radius: 16
color: "transparent"
Rectangle {
id: bgRect
anchors.fill: parent
radius: 16
layer.enabled: true
layer.effect: DropShadow {
verticalOffset: 3
radius: 8
samples: 16
color: "#30000000"
}
}
RowLayout {
anchors.centerIn: parent
spacing: 15
Image {
id: iconImage
sourceSize: Qt.size(28, 28)
}
Text {
id: buttonText
font.pixelSize: 18
font.weight: Font.Medium
color: "white"
}
}
MouseArea {
anchors.fill: parent
cursorShape: Qt.PointingHandCursor
onClicked: parent.clicked()
}
}
}
3.模态弹窗
API:
属性名 | 类型 | 默认值 | 说明 |
name | string | "" | 模态弹窗名称 |
title | string | "对话框" | 标题文本 |
content | string | “” | 对话框内容 |
confirmText | string | “确定” | 确认按钮文本 |
cancelText | string | "取消" | 取消按钮文本 |
type | string | "info" | 对话框类型: info, success, error, warning, input, select |
inputText | string | "" | 输入对话框文本 |
selectOptions | array | [] | 选择对话框列表 |
currentIndex | int | 0 | 当前选择对话框索引 |
返回值:
属性名 | 类型 | 说明 |
name | string | 当前操作模态窗名称 |
clicked | string | 点击的按钮类型,“confirm”,“cancel” |
inputText | string | 输入对话框内容 |
selectedIndex | int | 选择对话框索引 |
测试代码:
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import QtGraphicalEffects 1.15
import "qrc:/components_qml/ShowModal"
ApplicationWindow {
id: mainWindow
width: 1000
height: 700
visible: true
title: "QML模态对话框"
color: "#F5F7FA"
//引用模态对话框
ShowModal {
id: modalDialog
onBtClicked: (res)=> {
console.log(res.name);
if(res.clicked==="confirm")
console.log("点击了确认按钮");
else if(res.clicked==="cancel")
console.log("点击了取消按钮");
}
}
// 主界面内容
ColumnLayout {
anchors.centerIn: parent
spacing: 30
Text {
text: "模态对话框演示"
font.pixelSize: 32
font.weight: Font.Bold
color: "#2C3E50"
Layout.alignment: Qt.AlignHCenter
}
// 对话框类型选择
RowLayout {
spacing: 15
Layout.alignment: Qt.AlignHCenter
Rectangle {
width: 10
height: 10
radius: 5
color: "#3498DB"
}
Text {
text: "选择对话框类型:"
font.pixelSize: 18
color: "#34495E"
}
}
// 按钮区域
GridLayout {
columns: 3
columnSpacing: 20
rowSpacing: 20
Layout.alignment: Qt.AlignHCenter
// 基础对话框
DialogButton {
text: "基础对话框"
bgColor: "#3498DB"
onClicked: {
modalDialog.name = "基础对话框";
modalDialog.title = "确认操作";
modalDialog.content = "您确定要删除这个项目吗?此操作不可撤销。";
modalDialog.confirmText = "删除";
modalDialog.cancelText = "取消";
modalDialog.type = "info";
modalDialog.show();
}
}
// 警告对话框
DialogButton {
text: "警告对话框"
bgColor: "#F39C12"
onClicked: {
modalDialog.name = "警告对话框";
modalDialog.title = "磁盘空间不足";
modalDialog.content = "您的系统磁盘空间已使用95%,请清理空间以避免系统运行缓慢。";
modalDialog.confirmText = "立即清理";
modalDialog.cancelText = "稍后提醒";
modalDialog.type = "warning";
modalDialog.show();
}
}
// 错误对话框
DialogButton {
text: "错误对话框"
bgColor: "#E74C3C"
onClicked: {
modalDialog.name = "错误对话框";
modalDialog.title = "操作失败";
modalDialog.content = "无法完成请求的操作,请检查您的网络连接或系统权限设置。";
modalDialog.confirmText = "重试";
modalDialog.cancelText = "取消";
modalDialog.type = "error";
modalDialog.show();
}
}
// 成功对话框
DialogButton {
text: "成功对话框"
bgColor: "#2ECC71"
onClicked: {
modalDialog.name = "成功对话框";
modalDialog.title = "操作成功";
modalDialog.content = "您的文件已成功上传到云端存储。您可以通过文件管理器访问它。";
modalDialog.confirmText = "查看文件";
modalDialog.cancelText = "关闭";
modalDialog.type = "success";
modalDialog.show();
}
}
// 输入对话框
DialogButton {
text: "输入对话框"
bgColor: "#9B59B6"
onClicked: {
modalDialog.name = "输入对话框";
modalDialog.title = "创建新项目";
modalDialog.content = "请输入新项目的名称:";
modalDialog.confirmText = "创建";
modalDialog.cancelText = "取消";
modalDialog.type = "input";
modalDialog.inputText = "未命名项目";
modalDialog.show();
}
}
// 选择对话框
DialogButton {
text: "选择对话框"
bgColor: "#1ABC9C"
onClicked: {
modalDialog.name = "选择对话框";
modalDialog.title = "选择文件类型";
modalDialog.content = "请选择您要导出的文件格式:";
modalDialog.confirmText = "导出";
modalDialog.cancelText = "取消";
modalDialog.type = "select";
modalDialog.selectOptions = ["PDF文档 (.pdf)", "Word文档 (.docx)", "Excel表格 (.xlsx)", "图片 (.png)"];
modalDialog.currentIndex = 0;
modalDialog.show();
}
}
}
}
// 对话框按钮组件
component DialogButton: Rectangle {
property alias text: buttonText.text
property alias icon: iconImage.source
property alias bgColor: bgRect.color
signal clicked
width: 250
height: 80
radius: 16
color: "transparent"
Rectangle {
id: bgRect
anchors.fill: parent
radius: 16
layer.enabled: true
layer.effect: DropShadow {
verticalOffset: 3
radius: 8
samples: 16
color: "#30000000"
}
}
RowLayout {
anchors.centerIn: parent
spacing: 15
Image {
id: iconImage
sourceSize: Qt.size(28, 28)
}
Text {
id: buttonText
font.pixelSize: 18
font.weight: Font.Medium
color: "white"
}
}
MouseArea {
anchors.fill: parent
cursorShape: Qt.PointingHandCursor
onClicked: parent.clicked()
}
}
}
组件下载:
https://download.youkuaiyun.com/download/xiaolanglang_/91034525
使用说明:将文件解压到项目目录下,在.pro文件中添加
include($$PWD/components/components.pri)
如有不懂的问题请私信于我,或添加QQ(1980958078)