qml消息、加载、模态弹窗组件

1.消息提示框

 API:

属性类型默认值说明
textstring""对话框文本
durationint1500对话框持续时间
typestring""

对话框类型,值为空使用默认类型,

“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:

属性类型默认值说明
textstring""对话框文本
showProgressboolfalse是否显示进度值
progressint0进度值
opaqueBackgroundbooltrue不透明背景

测试代码:

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:

属性名类型默认值说明
namestring""模态弹窗名称
titlestring"对话框"标题文本
contentstring“”对话框内容
confirmTextstring“确定”确认按钮文本
cancelTextstring"取消"取消按钮文本
typestring"info"对话框类型: info, success, error, warning, input, select
inputTextstring""输入对话框文本
selectOptionsarray[]选择对话框列表
currentIndexint0当前选择对话框索引

返回值:

属性名类型说明
namestring当前操作模态窗名称
clickedstring点击的按钮类型,“confirm”,“cancel”
inputTextstring输入对话框内容
selectedIndexint选择对话框索引

 测试代码:

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小浪浪、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值