Qt Qml自定义模态对话框

自带的messagedialog不好使,自定义一个,简单的:

DialogPop.qml

/**
 * @brief 功能:此文件实现了模态框
 * @author lanmanck
 * @date 2023-07-25
 * CopyRight (C) lanmanck
 */
import QtQuick 2.1
import QtQuick.Window 2.0
import QtQuick.Layouts 1.0
import QtQuick.Controls 1.0
import QtQuick.Controls.Styles 1.0
import QtQuick.Dialogs 1.3

Window{
    id:dialog
    visible: false
    flags: Qt.Dialog
    modality: Qt.WindowModal
    property alias text: content.text
    signal accepted()
    signal rejected()

    title: qsTr("提示")
    width:content.implicitWidth+80

    Text {
        id:content
        anchors.centerIn: parent
        font.pointSize: 10
        lineHeight: 1.5
        wrapMode: Text.WordWrap
        Layout.minimumWidth: 300
    }

    Row{
        spacing: 10
        anchors.bottom: parent.bottom
        anchors.right: parent.right
        anchors.bottomMargin: 10
        anchors.rightMargin: 10
        Button {
            text: qsTr("确定")
            onClicked: {
                accepted()
                close()
            }
        }
        Button {
            text: qsTr("取消")
            onClicked: {
                rejected()
                close()
            }
        }

    }
}

好人做到底,使用:

    // 定义    
    DialogPop{
        id:dialogPop
        onAccepted: {
            var map = dataSource.setCurrentTime();

            if(map === false)
                toast.show("操作失败")
        }
    }

    // 调用
    MultiButton {
        id: setTime
        text: "时间: "
        items: ["ALL"]
        enabled: true
        currentSelection: 0
        onSelectionChanged:{
            dialogPop.text = "确定要同步时间吗?"
            dialogPop.show()
        }
    }

你可能感兴趣的:(qt,数据库,qt,qml)