使用Item 自定义Dialog,解决 qml 自带输入法不能输入问题

本文介绍了一种自定义QML对话框的方法,详细展示了如何通过QML代码创建一个可配置的弹出对话框,包括标题、内容、按钮等元素,并实现了淡入动画效果。此外,还提供了在main.qml中调用自定义对话框的示例。

//自定义item dialog 代码如下:

import QtQuick 2.0
Item {

    id: dialogComponent

    anchors.fill: parent

    property alias titleText: titleLabel.text

    property alias cancelText: cancelButton.text

    property alias okText: okButton.text

    property Component contentItem:null

    property alias contentComponent:contentComponent
    property bool showFooter: true
    signal accept()

    signal reject()


    // Add a simple animation to fade in the popup
    // let the opacity go from 0 to 1 in 400ms
    PropertyAnimation { target: dialogComponent; property: "opacity";
        duration: 400; from: 0; to: 1;
        easing.type: Easing.InOutQuad ; running: true }

    // backgroud
    // This rectange is the a overlay to partially show the parent through it
    // and clicking outside of the 'dialog' popup will do 'nothing'
    Rectangle {
        anchors.fill: parent

        id: overlay
        color: "#000000"
        opacity: 0.2
        // add a mouse area so that clicks outside
        // the dialog window will not do anything
        MouseArea {
            anchors.fill: parent
            onClicked: {
                console.log("backGroud clicked")
            }
        }
    }

    // dialog 区域
    // This rectangle is the actual popup
    Rectangle {
        id: dialogWindow
        width: parent.width * 0.7
        implicitHeight:headerItem.implicitHeight + implicitHeight+40

        height: Math.min(implicitHeight, parent.height * 0.8)

        color: '#232732'
        anchors.centerIn: parent
        focus: true

        // title 头
        Item{

            id: headerItem
            width: parent.width
            implicitHeight: (titleText === ''  0 : titleLabel.implicitHeight + 40)
            DefaultText {
                id: titleLabel
                anchors.top: parent.top
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.topMargin: 20
            }

        }
        // 中间文本
        Item{
            id : content
            width: parent.width
            anchors.top: headerItem.bottom
            anchors.topMargin: 20

            anchors.bottom: footerItem.top
            Loader{
                id : contentComponent
                anchors.top: content.top
                anchors.bottom: content.bottom
                anchors.left: content.left
                anchors.right: content.right
                sourceComponent:contentItem
            }
        }


        // 底部
        Item {
            id: footerItem
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 2
            width: parent.width

            visible: showFooter
            implicitHeight: (showFooter  cancelButton.implicitHeight + 40 : 0)

            DefaultButton {
                id: cancelButton
                anchors.left: parent.left
                anchors.leftMargin: 20
                anchors.top: parent.top
                anchors.topMargin: 20
                width: footerItem.width / 2 - 30
                text: qsTr('取 消')
                onClicked: {
                    console.log("cancel clicked")
                    reject()
                }
            }


            DefaultButton {
                id: okButton

                anchors.right: parent.right
                anchors.rightMargin: 20

                anchors.top: parent.top

                anchors.topMargin: 20
                width: footerItem.width / 2 - 30

                text: qsTr('确 认')

                onClicked: {
                    console.log("ok clicked");

                    accept()

                }
            }

        }


    }


}


其中中间部分的自定义使用 property Component contentItem:null
property alias contentComponent:contentComponent
这两个将其引用出去的。

        调用main.qml 文件如下 :

        import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.VirtualKeyboard 2.2

Window {
    id: window
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    MouseArea{
        id: closeButton
        anchors.fill:parent

        onClicked: {

            console.log("close button clicked.");
            addAddress.visible = true;

        }

    }


    DefaultItemDialog{
        id : addAddress
        visible: false
        titleText: qsTr('输入信息')
        showFooter: true
        contentItem: Rectangle {
            id : content
            width: parent.width
            implicitHeight: moneyInput.implicitHeight + 20
            color: '#393f47'
            property alias txt: moneyInput.text
            TextInput {
                id: moneyInput
                anchors.verticalCenter: parent.verticalCenter
                width: parent.width
                maximumLength: 20
                color: 'white'
            }

        }
        onAccept:
        {
            var contentItem = contentComponent.item
            console.log(contentItem)
            console.log(contentItem.txt)
            contentItem.txt = ""
            addAddress.visible = false
        }
    }

    InputPanel {
        id: inputPanel
        z: 99
        x: 0
        y: window.height
        width: window.width

        states: State {
            name: "visible"
            when: inputPanel.active
            PropertyChanges {
                target: inputPanel
                y: window.height - inputPanel.height
            }
        }
        transitions: Transition {
            from: ""
            to: "visible"
            reversible: true
            ParallelAnimation {
                NumberAnimation {

                    properties: "y"

                    duration: 250

                    easing.type: Easing.InOutQuad

                }
            }
        }
    }

}

QML 中创建自定义对话框(Dialog)组件是一种常见的需求,尤其是在构建具有现代用户界面的应用程序时。QML 提供了灵活的机制来定义和复用 UI 组件,使得开发者可以轻松实现符合特定设计需求的对话框。 ### 自定义对话框的基本结构 一个自定义对话框通常由以下几个部分组成: - 背景遮罩层(Backdrop) - 对话框容器(Dialog Container) - 标题栏(Title) - 内容区域(Content) - 操作按钮(Actions) 以下是一个简单的自定义对话框组件的实现示例: ```qml // CustomDialog.qml import QtQuick 2.15 import QtQuick.Controls 2.15 ModalDialog { width: 300 height: 200 Column { anchors.centerIn: parent spacing: 10 Text { text: "确认操作" font.bold: true horizontalAlignment: Text.AlignHCenter } Text { text: "您确定要执行此操作吗?" horizontalAlignment: Text.AlignHCenter } Row { spacing: 10 anchors.horizontalCenter: parent.horizontalCenter Button { text: "取消" onClicked: parent.parent.parent.parent.close() } Button { text: "确认" onClicked: { console.log("Confirmed"); parent.parent.parent.parent.close(); } } } } } ``` ### 使用自定义对话框 在主界面中,可以通过 `Loader` 或直接实例化的方式来加载和显示该对话框: ```qml // main.qml import QtQuick 2.15 import QtQuick.Window 2.15 import QtQuick.Controls 2.15 Window { visible: true width: 400 height: 300 Button { text: "打开对话框" anchors.centerIn: parent onClicked: customDialog.open() } CustomDialog { id: customDialog } } ``` ### 对话框的动画与交互 为了增强用户体验,可以为对话框添加动画效果。例如,使用 `Behavior` 和 `PropertyAnimation` 来实现淡入淡出效果: ```qml ModalDialog { // ... Behavior on opacity { PropertyAnimation { duration: 300 } } } ``` 此外,还可以通过 `ModalDialog` 或 `Popup` 组件来实现遮罩层和模态行为[^1],确保用户在关闭对话框之前无法与背景界面交互。 ### 高级功能扩展 如果需要更高级的功能,例如支持拖动、动态内容加载或自定义样式,可以继承 `ModalDialog` 并扩展其功能。例如,可以通过定义信号和属性来支持动态设置标题、内容和按钮文本: ```qml ModalDialog { property alias dialogTitle: titleText.text property alias dialogContent: contentText.text // ... } ``` 然后在使用时动态设置: ```qml CustomDialog { id: customDialog dialogTitle: "警告" dialogContent: "此操作不可逆!" } ``` 通过这种方式,可以构建出高度可定制和可复用的对话框组件,满足不同应用场景的需求。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值