在QML中使用alarm

我们可以QML应用中设置alarm来完成一些提醒的工作。为了使得它能够工作,我们必须加入calendar policy。由于calendar policy目前还是处于reserved状态,所以我们目前只能做测试使用。等将来正式发布后,就可以使用了。


import QtQuick 2.4
import Ubuntu.Components 1.2

/*!
    \brief MainView with a Label and Button elements.
*/

MainView {
    // objectName for functional testing purposes (autopilot-qt5)
    objectName: "mainView"

    // Note! applicationName needs to match the "name" field of the click manifest
    applicationName: "alarm.liu-xiao-guo"

    /*
     This property enables the application to change orientation
     when the device is rotated. The default is false.
    */
    //automaticOrientation: true

    // Removes the old toolbar and enables new features of the new header.
//    useDeprecatedToolbar: false

    width: units.gu(60)
    height: units.gu(85)

    Page {
        title: i18n.tr("alarm")

        Alarm{
            id: alarm
        }

        Column {
            spacing: units.gu(1)

            Row {
                spacing: units.gu(1)

                Label {
                    id: date
                    text: "Date:"
                    anchors.verticalCenter: parent.verticalCenter
                }
                TextField {
                    text: alarm.date.toString()
                    onAccepted: {
                        console.log("this is cool!");
                        onAccepted: alarm.date = new Date(text)
                    }
                }
            }

            Row {
                spacing: units.gu(1)
                Label {
                    id: msg
                    text: "Message:"
                    anchors.verticalCenter: parent.verticalCenter
                }
                TextField {
                    text: alarm.message
                    onAccepted: {
                        console.log("Setting the alarm message!")
                        alarm.message = text
                    }
                }
            }

            Button {
                text: "Save"
                onClicked: {
                    var now = new Date();
                    var newdate = new Date(now.getTime() + 10*1000);
                    console.log("now: " + newdate.toLocaleDateString())
                    alarm.date = newdate;

                    alarm.save();
                    if (alarm.error !== Alarm.NoError) {
                        print("Error saving alarm, code: " + alarm.error);
                    } else {
                        print("There is no error!")
                    }

                }
            }
        }
    }
}


在我们的应用中,当我们设置alarm时,我们把当前的时间加上10秒,然后设置时间:


            Button {
                text: "Save"
                onClicked: {
                    var now = new Date();
                    var newdate = new Date(now.getTime() + 10*1000);
                    console.log("now: " + newdate.toLocaleDateString())
                    alarm.date = newdate;

                    alarm.save();
                    if (alarm.error !== Alarm.NoError) {
                        print("Error saving alarm, code: " + alarm.error);
                    } else {
                        print("There is no error!")
                    }

                }

这样,当10秒钟过后,就会有我们的alarm信息。


  在QML中使用alarm_第1张图片


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






你可能感兴趣的:(在QML中使用alarm)