分享一个qml开发的Dialog

一、效果预览

在这里插入图片描述

二、源码分享

PopwindowWidget.qml

import QtQuick
import QtQuick.Controls
import QtQuick.Layouts


ApplicationWindow {
   
    id:self
    width: 470
    height: 250
    visible: false
    color: "#00000000"
    flags: Qt.Tool | Qt.FramelessWindowHint|Qt.MSWindowsFixedSizeDialogHint

    signal accept()
    signal reject()

    enum MessageType {
   Ask, Error,Notice,Wait,Warning}

    Rectangle{
   
        id:background
        anchors.fill: parent
        radius: 15
        gradient: Gradient{
   
            GradientStop {
    position: 0.0; color: "#ffff6111" }
            GradientStop {
    position: 1.0; color: "#50ff6111" }
        }

        ColumnLayout{
   
            anchors.fill: parent
            Item {
   
                Layout.fillWidth: true
                Layout.preferredHeight: background.height/4*3
                Row{
   
                    anchors.fill: parent
                    Image {
   
                        id:imageIcon
                        height: 100
                        width: 100
                        anchors.verticalCenter: parent.verticalCenter
                        fillMode: Image.Stretch
                        source: "qrc:/image/images/messageDialog/wait.svg"
                    }
                    Column{
   
                        width: parent.width-100
                        height: parent.height
                        leftPadding: 20
                        Text {
   
                            id: textTitle
                            width: parent.width
                            height: parent.height/3
                            text: qsTr("text")
                            font{
   
                                pointSize: 20
                            }
                            verticalAlignment: Qt.AlignVCenter
                        }
                        Text {
   
                            id: textMessage
                            width: parent.width
                            height: parent.height/3*2
                            text: qsTr("text")
                            wrapMode: Text.WordWrap
                            font{
   
                                pointSize: 12
                            }
                            verticalAlignment: Qt.AlignVCenter
                        }
                    }
                }
            }
            Item {
   
                id:itemBtn
                Layout.fillWidth: true
                Layout.preferredHeight: background.height/4
                Row{
   
                    id:rowBtn
                    anchors.fill: parent
                    anchors.margins: 10
                    spacing: 3
                    Button{
   
                        id:btnReject
                        width: 120
                        height: parent.height
                        text: qsTr("取消")
                        hoverEnabled: false
                        background:Rectangle{
   
                            color: btnReject.down?"#ffb6350a":"#50b6350a"
                            radius: 10
                            MouseArea{
   
                                anchors.fill: parent
                                cursorShape: Qt.PointingHandCursor;
                            }
                        }

                        Behavior on x{
   
                            NumberAnimation{
   
                                id:animBtnReject
                                duration: 500
                            }
                        }
                        onClicked: {
   
                            close()
                            self.reject()
                        }
                    }
                    Button{
   
                        id:btnAccept
                        width: 120
                        height: parent.height
                        text: qsTr("确定")
                        hoverEnabled: false
                        background:Rectangle{
   
                            color: btnAccept.down?"#ff777a05":"#50777a05"
                            radius: 10
                            MouseArea{
   
                                anchors.fill: parent
                                cursorShape: Qt.PointingHandCursor;
                            }
                        }
                        Behavior on x{
   
                            NumberAnimation{
   
                                id:animBtnAccept
                                duration: 300
                            }
                        }
                        onClicked: {
   
                            close()
                            self.accept()
                        }
                    }
                }
            }
        }
    }

    OpacityAnimator{
   
        id:showAnim
        target: background
        from: 0
        to:1
        duration: 500
        onFinished: {
   
            animBtnReject.duration = 500
            animBtnAccept.duration = 300
            btnReject.x = rowBtn.width-btnReject.width-btnAccept.width-rowBtn.spacing
            btnAccept.x = rowBtn.width-btnAccept.width
        }
    }
    OpacityAnimator{
   
        id:closeAnim
        target: background
        from: 1
        to:0
        duration: 500
        onFinished: {
   
            self.visible = false

        }
    }

    function show(messageType = PopwindowWidget.MessageType.Error,title=qsTr("未知标题"),message=qsTr("未知内容")){
   
        if(self.visible)
            return
        self.visible = true
        textTitle.text = title
        textMessage.text = message
        switch(messageType){
   
            case PopwindowWidget.MessageType.Ask:imageIcon.source = "qrc:/image/images/messageDialog/ask.svg"; break
            case PopwindowWidget.MessageType.Error:imageIcon.source = "qrc:/image/images/messageDialog/error.svg"; break
            case PopwindowWidget.MessageType.Notice:imageIcon.source = "qrc:/image/images/messageDialog/notice.svg"; break
            case PopwindowWidget.MessageType.Wait:imageIcon.source = "qrc:/image/images/messageDialog/wait.svg"; break
            case PopwindowWidget.MessageType.Warning:imageIcon.source = "qrc:/image/images/messageDialog/warning.svg"; break
        }

        showAnim.start()
    }
    function close(){
   
        if(closeAnim.running || !self.visible)
            return
        animBtnReject.duration = 300
        animBtnAccept.duration = 500
        btnReject.x = 0
        btnAccept.x = btnReject.width+rowBtn.spacing
        closeAnim.start()
    }
}

三、使用方法

main.qml

import QtQuick
import QtQuick.Controls
import Qt.labs.qmlmodels

ApplicationWindow {
   
    width: 700
    height: 400
    visible: true

    PopwindowWidget{
   
        id:popwindow
        onAccept: {
   
            console.log("onAccept")
        }
        onReject: {
   
            console.log("onReject")
        }
    }
    Row{
   
        Button{
   
            width: 200
            height: 200
            text: "show"
            onClicked: {
   
                popwindow.show()
            }
        }
        Button{
   
            width: 200
            height: 200
            text: "close"
            onClicked: {
   
                popwindow.close()
            }
        }
    }


}

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-29 22:10:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-29 22:10:02       101 阅读
  3. 在Django里面运行非项目文件

    2023-12-29 22:10:02       82 阅读
  4. Python语言-面向对象

    2023-12-29 22:10:02       91 阅读

热门阅读

  1. 洛谷 P8664 [蓝桥杯 2018 省 A] 付账问题

    2023-12-29 22:10:02       62 阅读
  2. 【QT】跨平台区分32位和64位的宏

    2023-12-29 22:10:02       47 阅读
  3. Python3 数据类型转换

    2023-12-29 22:10:02       51 阅读
  4. 系统学习申论

    2023-12-29 22:10:02       53 阅读
  5. 《微信小程序开发从入门到实战》学习六十五

    2023-12-29 22:10:02       52 阅读
  6. vue&react中的副作用

    2023-12-29 22:10:02       56 阅读
  7. MySQL 索引详解

    2023-12-29 22:10:02       59 阅读