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()
    }

}
相关推荐
kaixin_learn_qt_ing11 小时前
model/view 筛选功能
qt
佳児素花痴╮14 小时前
Qt速通2
开发语言·qt
心态还需努力呀15 小时前
把 Flameshot 编译进鸿蒙 PC:一次 Qt C++ 截图工具的源码级移植实战
c++·qt·harmonyos
CodeMaker80817 小时前
规则更规范,应用却起不来:我重新理解了 Qt Agent Skills
qt
楚莫识17 小时前
Qt show()、raise()、activateWindow() 调用顺序区别
qt·pyqt
励志不掉头发的内向程序员18 小时前
【LibreCAD 2D架构】从 addHistory 到 RS_UndoCycle:LibreCAD 里的两套 History 与撤销机制
开发语言·c++·qt·学习·系统架构
kaixin_learn_qt_ing1 天前
【银河麒麟下】 安装达梦数据库 + 使用Qt访问达梦数据库
qt
mengzhi啊1 天前
缓存类CacheDataManager把临时数据存入json。做成插件·
qt·缓存·json
尘客-追梦1 天前
Day 01:插件化之前,先看清 ABI 这堵墙
开发语言·c++·qt
Quz1 天前
QML SwipeView:实现一个简单的图片浏览器
qt