如何在QML应用中设计自己的Dialog

对话框Dialog的设计在许多的QML应用是经常用到的。许多新的开发者刚开始接触QML,有时找不到头绪。也许是由于QML的设计太过灵活,所以实现的方法有非常多。这里介绍几种简单的方式。

1)使用Ubuntu SDK提供的标准API

我们可以使用Ubuntu SDK提供的标准Dialog接口。使用的方法非常简单:

import QtQuick 2.4
import Ubuntu.Components 1.2
import Ubuntu.Components.Popups 1.0
Item {
    width: units.gu(80)
    height: units.gu(80)
    Component {
         id: dialog
         Dialog {
             id: dialogue
             title: "Save file"
             text: "Are you sure that you want to save this file?"
             Button {
                 text: "cancel"
                 onClicked: PopupUtils.close(dialogue)
             }
             Button {
                 text: "overwrite previous version"
                 color: UbuntuColors.orange
                 onClicked: PopupUtils.close(dialogue)
             }
             Button {
                 text: "save a copy"
                 color: UbuntuColors.orange
                 onClicked: PopupUtils.close(dialogue)
             }
         }
    }
    Button {
        anchors.centerIn: parent
        id: saveButton
        text: "save"
        onClicked: PopupUtils.open(dialog)
    }
}

就像文档中介绍的那样,我们需要import Ubuntu.Components.Popups 1.0模块来完成这个。这是目前最简单的方法,而且使用这样的方法,我们可以得到Ubuntu的look and feel。在调用时也可以传人我们的caller:

        Component {
            id: dialog
            Dialog {
                id: dialogue
                title: "Save file"
                text: "Are you sure that you want to save this file?"
                Button {
                    text: "cancel"
                    onClicked: PopupUtils.close(dialogue)
                }
                Button {
                    text: "overwrite previous version"
                    color: UbuntuColors.orange
                    onClicked: PopupUtils.close(dialogue)
                }
                Button {
                    text: "save a copy"
                    color: UbuntuColors.orange
                    onClicked: {
                        console.log("caller: " + caller );
                        if ( caller !== null ) {
                            caller.callback("file is saved!");
                        }

                        PopupUtils.close(dialogue);
                    }
                }
            }
        }

        Column {
            anchors.centerIn: parent
            spacing: units.gu(2)

            Button {
                id: saveButton
                text: "save"
                onClicked: PopupUtils.open(dialog)

                function callback(message) {
                    console.log("returned: " + message);
                }
            }

            Button {
                id: anotherSave
                text: "Another Save"
                onClicked: PopupUtils.open(dialog, anotherSave)

                function callback(message) {
                    console.log("returned: " + message);
                }
            }
}

这样我们可以通过caller来“callback”回调我们的方法。

2)创建我们自己的Dialog Component并动态调用它

我们可以自己创建一个自己的Dialog.qml文件来存放为我们的Dialog所需要的内容:

import QtQuick 2.0

Item {
    id: dialogComponent
    anchors.fill: parent

    // 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 }

    // 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.6
        // add a mouse area so that clicks outside
        // the dialog window will not do anything
        MouseArea {
            anchors.fill: parent
        }
    }

    // This rectangle is the actual popup
    Rectangle {
        id: dialogWindow
        width: 300
        height: 300
        radius: 10
        anchors.centerIn: parent

        Text {
            anchors.centerIn: parent
            text: "This is the popup"
        }

        // For demo I do not put any buttons, or other fancy stuff on the popup
        // clicking the whole dialogWindow will dismiss it
        MouseArea{
            anchors.fill: parent
            onClicked: {
                // destroy object is needed when you dynamically create it
                dialogComponent.destroy()
            }
        }
    }
}

我们可以通过如下的方法来动态创建这个Dialog:

            Button {
                id: mydialog
                text: "My customized dialog"
                onClicked: {
                    Qt.createComponent("Dialog.qml").createObject(mainpage, {});
                }
            }

这个动态创建的Dialog在点击自己的页面时,被自动消除了。

        MouseArea{
            anchors.fill: parent
            onClicked: {
                // destroy object is needed when you dynamically create it
                dialogComponent.destroy()
            }
        }

这里的设计因人而异。我们可以设计自己的按钮来销毁创建的Dialog,并同时传回自己的选择。这个方法的好处是设计的UI不必绑定特定的操作系统,比如Ubuntu。这样这样的代码可以在多个平台上运行。

3)创建一个不可见的页面,在需要是显现

这个方法的实现也是非常简单,也在许多的QML应用中被使用。在创建页面时,我们可以同时创建不可见的部分。它们可以是重叠在一起的。只是在某个时间,只有一个页面可以被显示。通常不可见的页面在启动时被设置为“Dialog”。只有在需要的时候才可以被设置为可见。

为了说明问题,我们同样设置了一个自己的AnotherDialog.qml文件:

import QtQuick 2.0

Item {
    id: dialogComponent
    anchors.fill: parent

    // 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 }

    // 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.6
        // add a mouse area so that clicks outside
        // the dialog window will not do anything
        MouseArea {
            anchors.fill: parent
        }
    }

    // This rectangle is the actual popup
    Rectangle {
        id: dialogWindow
        width: 300
        height: 300
        radius: 10
        anchors.centerIn: parent

        Text {
            anchors.centerIn: parent
            text: "This is the popup"
        }

        // For demo I do not put any buttons, or other fancy stuff on the popup
        // clicking the whole dialogWindow will dismiss it
        MouseArea{
            anchors.fill: parent
            onClicked: {
                // destroy object is needed when you dynamically create it
                console.log("it is clicked!");
                dialogComponent.visible = false;
            }
        }
    }
}

在我们的Main.qml中:

            Button {
                id: myanotherdialog
                text: "My another dialog"
                onClicked: {
                    dialog1.visible = true;
                }
            }

  ...

        AnotherDialog {
            id: dialog1
            anchors.fill: parent
            visible: false
        }

我们通过这样的方法,可以设计出我们自己想要的任何一个Dialog。如果不采用Ubuntu的任何API,你可以让它运行到任何一个你想要的平台之中。相比较第二种方法,这个方法需要占用额外的内存,并且在应用一启动时,就会被装载到内存中。

运行我们的测试应用:

    

整个项目的源码在:git clone https://gitcafe.com/ubuntu/dialog.git

时间: 2024-08-01 17:58:47

如何在QML应用中设计自己的Dialog的相关文章

如何在QML中设计一个expandable ListView

在前面的文章"如何在QML中使用ListView并导航到其它页面中"中,我们已经介绍了各种在ListView中导航到其它页面的方法.在这篇文章中,我来介绍如何建立一个expandable的ListView.通过这样的方法,ListView可以不用导航到其它的页面中,但是它可以通过状态的控制占据整个页面,而得到显示. 首先我们可以使用Ubuntu SDK来创建一个最简单的"QML App with Simple UI (qmlproject)"项目.我们的Main.q

如何在QML应用中启动Scope

在这篇文章中,我们将介绍如何在QML应用中调用Scope,并把搜索的关键词传进去.这对有些QML应用需要用到Scope的情况非常有用.更多关于url-dispatcher的知识,请在文章"使用URL dispatcher的范例"看到. Scope ID 首先我们来讲一下什么是Scope ID.我们打开我们创建的任何一个Scope的manifest.json文件: { "architecture": "@[email protected]", &q

如何在Ubuntu QML应用中设计像微信对话那样的UI

我们知道像微信那样的带有气球的对话框对于一些聊天的应用来说非常好.在很多即时通讯的应用中可以用到.在今天的文章中,我们将介绍如何使用QML来实现这样的界面. 为了方便,我们可以采用Ubuntu SDK中的"QtQuick App with QML UI (qmake)"这个模版来实现一个模版的应用.为了能够创建一个TextBalloon的控件,我们使用了C++代码: textballoon.h #ifndef TEXTBALLOON_H #define TEXTBALLOON_H #i

如何在Winform界面中设计图文并茂的界面

在Winform里面,很多控件元素都是标准的,如图标.按钮.工具栏等等,所以一般设计标准的Winform界面比较快捷,但是往往这样的界面相对单调一些,特别在界面控件比较少的情况下,我们往往需要加入一些图片.背景什么来衬托一下,看起来图文并茂一些,本文主要介绍,如何在Winfrom里面利用各种控件的特点,设计一个相对比较美观.图文并茂的Winform界面. 1.界面效果分析 在一些场景里面,如一些进销存的系统里面,我们往往把一些相关的模块处理放在一起,如进货.退货.库存调入.调出.产品.库存.盘点

如何在QML应用中实现一个Splash画面

在QML应用中,我们经常要用到一个SplashScreen的画面来渲染我们的应用.那么我们怎么在自己的应用中做一个Splash Screen呢? 首先我们来设计一个自己的SplashScreen的QML模块: SplashScreen.qml import QtQuick 2.0 Item { id: splash anchors.fill: parent property int timeoutInterval: 2000 signal timeout Image { id: splashIm

如何在QML应用中读写文件

我们知道,在QML应用中,有时我们需要来读写一些文件,但是在我们的QML语言中并没有相应的API接口来供我们做(虽然有API接口来存储设置文件等).那么我们怎么来做这个事情呢?我们可以通过Qt C++的方法来实现这个功能. 1)创建一个简单的模版应用 我们使用Ubuntu SDK的模版来创建一个最简单的应用:      我们选择"QML App with C++ plugin"模版来做我们的应用. 2)添加文件读写的文件到项目中 我们添加如下的C++ "FileIO类到我们的

如何在QML应用中得到一个Item的所有属性,信号及方法

Item是QML语言中最基本的元素.有时为了方便,我们可以列出它里面的所有的属性,信号及方法.我们可以通过这个方法来修改我们的属性等.在QML语言中,所有的可视的控件都是继承于Item的. 下面我们来通过一个例子来展示如何这么做.我们可以设计一个简单的QML应用如下: import QtQuick 2.0 import Ubuntu.Components 1.1 /*! \brief MainView with a Label and Button elements. */ MainView {

如何在QML中使用ListView并导航到其它页面中

我们知道ListView在QML应用中扮演非常重要的角色.看看我们的很多的应用都是在使用ListView.那么当我们点击ListView中的item并导航到另外一个页面呢?其实这样的方法有很多.在这篇文章中,我们来介绍其中的几种.开发者可以参照其中的设计,或自己想出更好的设计. 1)使用PageStack来完成 在我们的RssReader中的例子中,我们使用了PageStack来完成我们的导航.我们可以把我们的每个页面都做成我们的Page.当我们的页面被点击后,我们把新的Page压入栈中.在返回

如何在QML应用中使用Javascript解析JSON

很多QML应需要访问web services.我们可以通过Javascript的方法来解析得到我们所需要的JSON数据,并把它们展示出来.在今天的例子中,我们将展示如何实现它? 我们可以创建一个最基本的"QML App with Simple UI (qmlproject)",并取名我们的应用为"baidutranslator".我们将使用的API为: http://openapi.baidu.com/public/2.0/bmt/translate?client_