QML对话框控件

ColorDialog

复制代码
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Layouts 1.15//布局模块
import QtQuick.Controls 2.15 //Controls模块
import QtQuick.Dialogs 1.3

ApplicationWindow {
    width: 360
    height: 520
    visible: true
    title: qsTr("Qml learn ")
    id:rootid

    Column{
        spacing: 20
        anchors.fill: parent
        Button{
            text: "select color"
            onClicked: {
                colorid.open();
            }
            anchors.horizontalCenter: parent.horizontalCenter
        }
        Rectangle{
            id:rectid
            width: 200
            height: 200
            border.width: 8
            anchors.horizontalCenter: parent.horizontalCenter
        }
        ColorDialog{
            id:colorid
            title: "select color"
            onAccepted: {
                console.log("accept = ",colorid.color);
                rectid.color = colorid.color;
            }
        }
    }


    //Qt5的windows没有focus属性,用以下实现
    Shortcut {
        sequence: "q"
        context: Qt.ApplicationShortcut
        onActivated: Qt.quit()
    }

}

FileDialog

复制代码
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Layouts 1.15//布局模块
import QtQuick.Controls 2.15 //Controls模块
import QtQuick.Dialogs 1.3

ApplicationWindow {
    width: 360
    height: 520
    visible: true
    title: qsTr("Qml learn ")
    id:rootid

    Column{
        spacing: 20
        anchors.fill: parent
        Button{
            text: "select file"
            onClicked: {
                fileid.open();
            }
            anchors.horizontalCenter: parent.horizontalCenter
        }
        Rectangle{
            id:rectid
            width: 200
            height: 200
            border.width: 8
            anchors.horizontalCenter: parent.horizontalCenter
        }
        FileDialog{
            id:fileid
            title: "select file"
            nameFilters: ["*.*","*.txt"]
            // fileMode: FileDialog.ExistingFile
            // 选中单个文件触发

            onAccepted: {
                console.log("accept = ",fileid.fileUrl );
            }
        }
    }


    //Qt5的windows没有focus属性,用以下实现
    Shortcut {
        sequence: "q"
        context: Qt.ApplicationShortcut
        onActivated: Qt.quit()
    }

}

FolderDialog

复制代码
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Layouts 1.15//布局模块
import QtQuick.Controls 2.15 //Controls模块
import QtQuick.Dialogs 1.3
import Qt.labs.platform 1.1
ApplicationWindow {
    width: 360
    height: 520
    visible: true
    title: qsTr("Qml learn ")
    id:rootid

    Column{
        spacing: 20
        anchors.fill: parent
        Button{
            text: "select file"
            onClicked: {
                folderid.open();
            }
            anchors.horizontalCenter: parent.horizontalCenter
        }
        Rectangle{
            id:rectid
            width: 200
            height: 200
            border.width: 8
            anchors.horizontalCenter: parent.horizontalCenter
        }
        FolderDialog{
            id:folderid
            title: "select file"
            // currentFolder: folderid.currentFolder

            onAccepted: {
                console.log("accept = ",folderid.folder );
            }
        }
    }


    //Qt5的windows没有focus属性,用以下实现
    Shortcut {
        sequence: "q"
        context: Qt.ApplicationShortcut
        onActivated: Qt.quit()
    }

}

FontDialog

注意:使用这个需要使用QApplication,并且需要链接动态库。

复制代码
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Layouts 1.15//布局模块
import QtQuick.Controls 2.15 //Controls模块
import QtQuick.Dialogs 1.3
import Qt.labs.platform 1.1
ApplicationWindow {
    width: 360
    height: 520
    visible: true
    title: qsTr("Qml learn ")
    id:rootid

    Column{
        spacing: 20
        anchors.fill: parent
        Button{
            text: "select file"
            onClicked: {
                folderid.open();
            }
            anchors.horizontalCenter: parent.horizontalCenter
        }
        Rectangle{
            id:rectid
            width: 200
            height: 200
            border.width: 8
            anchors.horizontalCenter: parent.horizontalCenter
        }
        FontDialog{
            id:folderid
            title: "select file"

            onAccepted: {
                console.log("accept = ",folderid.currentFont );
            }
        }
    }


    //Qt5的windows没有focus属性,用以下实现
    Shortcut {
        sequence: "q"
        context: Qt.ApplicationShortcut
        onActivated: Qt.quit()
    }

}

MessageDialog

我这里就一个按钮

复制代码
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Layouts 1.15//布局模块
import QtQuick.Controls 2.15 //Controls模块
import QtQuick.Dialogs 1.3
import Qt.labs.platform 1.1
ApplicationWindow {
    width: 360
    height: 520
    visible: true
    title: qsTr("Qml learn ")
    id:rootid

    Column{
        spacing: 20
        anchors.fill: parent
        Button{
            text: "select file"
            onClicked: {
                messageid.open();
            }
            anchors.horizontalCenter: parent.horizontalCenter
        }
        Rectangle{
            id:rectid
            width: 200
            height: 200
            border.width: 8
            anchors.horizontalCenter: parent.horizontalCenter
        }
        MessageDialog{
            id:messageid
            title: "dialog"
            text: "这是一个对话框"
            buttons: MessageDialog.Ok |MessageDialog.Cancel

            onAccepted: {
                console.log("accept = " );
            }
            onRejected: {
                console.log("reject")
            }
        }
    }


    //Qt5的windows没有focus属性,用以下实现
    Shortcut {
        sequence: "q"
        context: Qt.ApplicationShortcut
        onActivated: Qt.quit()
    }

}

自定义对话框

可以参考:gallery 示例

复制代码
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Layouts 1.15//布局模块
import QtQuick.Controls 2.15 //Controls模块
import QtQuick.Dialogs 1.3
import Qt.labs.platform 1.1

import QtQuick 2.12
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.12


ApplicationWindow {
    width: 360
    height: 520
    visible: true
    title: qsTr("Qml learn ")
    id:rootid

    Column{
        spacing: 20
        anchors.fill: parent
        Button{
            text: "select file"
            onClicked: {
                messageid.open();
            }
            anchors.horizontalCenter: parent.horizontalCenter
        }
        Rectangle{
            id:rectid
            width: 200
            height: 200
            border.width: 8
            anchors.horizontalCenter: parent.horizontalCenter
        }
        // MessageDialog{
        //     id:messageid
        //     title: "dialog"
        //     text: "这是一个对话框"
        //     // buttons: MessageDialog.Ok |MessageDialog.Cancel
        //     standardButtons: Dialog.Yes | Dialog.No

        //     // parent: Overlay.overlay

        //     onAccepted: {
        //         console.log("accept = " );
        //     }
        //     onRejected: {
        //         console.log("reject")
        //     }
        // }
        Dialog {
            id: messageid

            x: (parent.width - width) / 2
            y: (parent.height - height) / 2
            width: Math.min(page.width, page.height) / 3 * 2
            contentHeight: logo.height * 2
            parent: Overlay.overlay

            modal: true
            title: "Content"
            standardButtons: Dialog.Yes | Dialog.No

            Flickable {
                id: flickable
                clip: true
                anchors.fill: parent
                contentHeight: column.height

                Column {
                    id: column
                    spacing: 20
                    width: parent.width

                    Image {
                        id: logo
                        width: parent.width / 2
                        anchors.horizontalCenter: parent.horizontalCenter
                        fillMode: Image.PreserveAspectFit
                        source: "../images/qt-logo.png"
                    }

                    Label {
                        width: parent.width
                        text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc finibus "
                            + "in est quis laoreet. Interdum et malesuada fames ac ante ipsum primis "
                            + "in faucibus. Curabitur eget justo sollicitudin enim faucibus bibendum. "
                            + "Suspendisse potenti. Vestibulum cursus consequat mauris id sollicitudin. "
                            + "Duis facilisis hendrerit consectetur. Curabitur sapien tortor, efficitur "
                            + "id auctor nec, efficitur et nisl. Ut venenatis eros in nunc placerat, "
                            + "eu aliquam enim suscipit."
                        wrapMode: Label.Wrap
                    }
                }

                ScrollIndicator.vertical: ScrollIndicator {
                    parent: contentDialog.contentItem
                    anchors.top: flickable.top
                    anchors.bottom: flickable.bottom
                    anchors.right: parent.right
                    anchors.rightMargin: -contentDialog.rightPadding + 1
                }
            }
        }

    }


    //Qt5的windows没有focus属性,用以下实现
    Shortcut {
        sequence: "q"
        context: Qt.ApplicationShortcut
        onActivated: Qt.quit()
    }

}
相关推荐
郝学胜_神的一滴2 小时前
Qt 高级编程 038:打造高颜值下拉菜单按钮
c++·qt
小灰灰搞电子2 小时前
Qt 队列容器 QQueue 的详解与示例
开发语言·qt·qqueue
xcyxiner1 天前
DicomViewer15(act验证ci)
qt
韭菜炒鸡肝天2 天前
VTK开发笔记(一):VTK介绍,Qt..+VSx+VTK.编译
开发语言·笔记·qt
刀法孜然2 天前
QT5.12.8开发中使用触摸屏,配置的环境变量,imx6ullpro开发板qt开发
qt
回眸不遇2 天前
详谈 QT 布局 QLayout::SizeConstraint 和 QSizePolicy 对 QWidget 尺寸的影响
数据库·qt·系统架构
Richard.Wong2 天前
qt生成dll供C#调用
开发语言·qt
Infedium3 天前
Qt嵌入式开发核心亮点:线程安全硬件交互技术
开发语言·qt