<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- gettingstartedqml.qdoc --> <title>Getting Started Programming with Qt Quick | Qt 5.12</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td ><a href="index.html">Qt 5.12</a></td><td >Getting Started Programming with Qt Quick</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right">Qt 5.12.6 Reference Documentation</td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#creating-the-alarms-project">Creating the Alarms Project</a></li> <li class="level1"><a href="#qt-creator">Qt Creator</a></li> <li class="level1"><a href="#additional-source-files">Additional source files</a></li> <li class="level2"><a href="#qtquickcontrols2-op-op-conf"><code>qtquickcontrols2.conf</code></a></li> <li class="level2"><a href="#main-op-op-qml"><code>main.qml</code></a></li> <li class="level2"><a href="#alarmdialog-op-op-qml"><code>AlarmDialog.qml</code></a></li> <li class="level2"><a href="#alarmdelegate-op-op-qml"><code>AlarmDelegate.qml</code></a></li> <li class="level2"><a href="#alarmmodel-op-op-qml"><code>AlarmModel.qml</code></a></li> <li class="level2"><a href="#tumblerdelegate-qml">TumblerDelegate.qml</a></li> <li class="level1"><a href="#entering-new-alarms">Entering new alarms</a></li> <li class="level1"><a href="#editing-alarms">Editing alarms</a></li> <li class="level1"><a href="#deleting-alarms">Deleting alarms</a></li> <li class="level2"><a href="#summary">Summary</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">Getting Started Programming with Qt Quick</h1> <span class="subtitle"></span> <!-- $$$tutorials/alarms-brief --> <p>A tutorial for Qt Quick based on an alarms application.</p> <!-- @@@tutorials/alarms --> <!-- $$$tutorials/alarms-description --> <div class="descr"> <a name="details"></a> <p>This tutorial shows how to develop a simple alarm application as an introduction to Qt Quick and Qt Quick Controls2.</p> <p>This application is similar to the alarm application usually found on an Android phone. Its features let you enter, edit, or delete alarms. An alarm can trigger on a given date, and you can set it to repeat on a series of subsequent days.</p> <p>The main screen shows the list of saved alarms:</p> <p class="centerAlign"><img src="images/mainscreen.png" alt=""Alarms application"" /></p><p>The detail screen lets you edit or delete existing alarms:</p> <p class="centerAlign"><img src="images/detailscreen.png" alt=""Detail screen"" /></p><p>The dialog screen is used for adding new alarms. It pops up when you click on the "+" RoundButton on the bottom of the main screen:</p> <p class="centerAlign"><img src="images/addalarms.png" alt=""Add alarms"" /></p><p>The source files are located in the qtdoc repository. You can either fetch the Qt 5 sources from the Qt Project, or install them as part of Qt 5. The application is also available in the example list of Qt Creator's Welcome mode.</p> <a name="creating-the-alarms-project"></a> <h2 id="creating-the-alarms-project">Creating the Alarms Project</h2> <p>This section shows how to create the project in Qt Creator. It discusses the files generated automatically by Qt Creator, and the two files the programmer has to create in Qt Creator or some other editor. The latter two files are included with the source code for this tutorial.</p> <a name="qt-creator"></a> <h2 id="qt-creator">Qt Creator</h2> <p>Setting up a new project in Qt Creator is aided by a wizard that guides you step-by-step through the project creation process. The wizard prompts you to enter the settings needed for that particular type of project and creates the project for you.</p> <p>To create the Alarms project, select <b>File</b> > <b>New File or Project</b> > <b>Application</b> > <b>Qt Quick Application - Empty</b> > <b>Choose</b>. Type "alarms" in the <b>Name</b> field, and follow the instructions of the wizard.</p> <p class="centerAlign"><img src="images/alarms2.png" alt=""Qt Creator New File or Project dialog"" /></p><p class="centerAlign"><img src="images/alarms3.png" alt=""Project Location"" /></p><p>The Qt Quick application wizard creates a project that contains the following source files:</p> <div class="table"><table class="generic"> <thead><tr class="qt-style"><th >Source file</th><th >Purpose</th></tr></thead> <tr valign="top" class="odd"><td >alarms.pro</td><td >The project file</td></tr> <tr valign="top" class="even"><td >main.cpp</td><td >The main C++ code file for the application.</td></tr> <tr valign="top" class="odd"><td >qml.qrc</td><td >The resource file, which contains the names of the source files, except main.cpp and the project file.</td></tr> </table></div> <p>The wizard generates the code in the main.cpp file below. This code block enables High DPI scaling and declares <code>app</code> and <code>engine</code>. The engine then loads our main QML file.</p> <pre class="cpp"> <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span>argv<span class="operator">[</span><span class="operator">]</span>) { <span class="type">QCoreApplication</span><span class="operator">::</span>setAttribute(<span class="type">Qt</span><span class="operator">::</span>AA_EnableHighDpiScaling); <span class="type">QGuiApplication</span> app(argc<span class="operator">,</span> argv); <span class="type">QQmlApplicationEngine</span> engine; engine<span class="operator">.</span>load(<span class="type">QUrl</span>(<span class="type">QStringLiteral</span>(<span class="string">"qrc:/main.qml"</span>))); <span class="keyword">if</span> (engine<span class="operator">.</span>rootObjects()<span class="operator">.</span>isEmpty()) <span class="keyword">return</span> <span class="operator">-</span><span class="number">1</span>; <span class="keyword">return</span> app<span class="operator">.</span>exec(); } </pre> <a name="additional-source-files"></a> <h2 id="additional-source-files">Additional source files</h2> <div class="table"><table class="generic"> <thead><tr class="qt-style"><th >Source file</th><th >Purpose</th></tr></thead> <tr valign="top" class="odd"><td ><code>qtquickcontrols2.conf</code></td><td >Selects the <code>Material</code> style with the <code>Dark</code> theme.</td></tr> <tr valign="top" class="even"><td ><code>main.qml</code></td><td >The QML code that links AlarmDialog.qml, AlarmModel.qml, AlarmDelegate.qml and TumblerDelegate.qml</td></tr> <tr valign="top" class="odd"><td ><code>AlarmDialog.qml</code></td><td >Defines the dialog for adding new alarms.</td></tr> <tr valign="top" class="even"><td ><code>AlarmDelegate.qml</code></td><td >Defines the layout of the main screen of the app.</td></tr> <tr valign="top" class="odd"><td ><code>AlarmModel.qml</code></td><td >Defines the ListModel used for storing the alarms' data.</td></tr> <tr valign="top" class="even"><td ><code>TumblerDelegate.qml</code></td><td >Defines the graphical layout of the Tumblers</td></tr> </table></div> <a name="qtquickcontrols2-op-op-conf"></a> <h3 id="qtquickcontrols2-op-op-conf"><code>qtquickcontrols2.conf</code></h3> <p>The following snippet shows how to set the <code>Dark</code> theme in the <code>Material</code> style:</p> <pre class="cpp"> [Controls] Style=Material [Material] Theme=Dark Accent=Red </pre> <a name="main-op-op-qml"></a> <h3 id="main-op-op-qml"><code>main.qml</code></h3> <p><code>mainWindow</code>, an ApplicationWindow QML type, is the root item in this app.</p> <pre class="cpp"> ApplicationWindow { id: window width: 400 height: 500 visible: true </pre> <p>The ListView <code>alarmListView</code> combines the data from <code>alarmModel</code> with the layout defined in <code>alarmDelegate</code>.</p> <pre class="cpp"> ListView { id: alarmListView anchors.fill: parent model: AlarmModel {} delegate: AlarmDelegate {} } </pre> <p>New alarms can be added by clicking RoundButton <code>addAlarmButton</code>. Clicking it opens a dialog screen <code>alarmDialog</code>.</p> <pre class="cpp"> RoundButton { id: addAlarmButton text: "+" anchors.bottom: alarmListView.bottom anchors.bottomMargin: 8 anchors.horizontalCenter: parent.horizontalCenter onClicked: alarmDialog.open() } AlarmDialog { id: alarmDialog x: Math.round((parent.width - width) / 2) y: Math.round((parent.height - height) / 2) alarmModel: alarmListView.model } </pre> <a name="alarmdialog-op-op-qml"></a> <h3 id="alarmdialog-op-op-qml"><code>AlarmDialog.qml</code></h3> <p>This dialog screen has a RowLayout with a Tumbler each for hours and minutes, and another RowLayout with a Tumbler each for day, month, and year.</p> <pre class="cpp"> contentItem: RowLayout { RowLayout { id: rowTumbler Tumbler { id: hoursTumbler model: 24 delegate: TumblerDelegate { text: formatNumber(modelData) } } Tumbler { id: minutesTumbler model: 60 delegate: TumblerDelegate { text: formatNumber(modelData) } } } RowLayout { id: datePicker Layout.leftMargin: 20 property alias dayTumbler: dayTumbler property alias monthTumbler: monthTumbler property alias yearTumbler: yearTumbler readonly property var days: [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] Tumbler { id: dayTumbler function updateModel() { // Populate the model with days of the month. For example: [0, ..., 30] var previousIndex = dayTumbler.currentIndex var array = [] var newDays = datePicker.days[monthTumbler.currentIndex] for (var i = 1; i <= newDays; ++i) array.push(i) dayTumbler.model = array dayTumbler.currentIndex = Math.min(newDays - 1, previousIndex) } Component.onCompleted: updateModel() delegate: TumblerDelegate { text: formatNumber(modelData) } } Tumbler { id: monthTumbler onCurrentIndexChanged: dayTumbler.updateModel() model: 12 delegate: TumblerDelegate { text: window.locale.standaloneMonthName(modelData, Locale.ShortFormat) } } Tumbler { id: yearTumbler // This array is populated with the next three years. For example: [2018, 2019, 2020] readonly property var years: (function() { var currentYear = new Date().getFullYear() return [0, 1, 2].map(function(value) { return value + currentYear; }) })() model: years delegate: TumblerDelegate { text: formatNumber(modelData) } } } } } </pre> <p>If you click on <b>OK</b> in the dialog, the entered data will be added to <code>alarmModel</code>:</p> <pre class="cpp"> onAccepted: { alarmModel.append({ "hour": hoursTumbler.currentIndex, "minute": minutesTumbler.currentIndex, "day": dayTumbler.currentIndex + 1, "month": monthTumbler.currentIndex + 1, "year": yearTumbler.years[yearTumbler.currentIndex], "activated": true, "label": "", "repeat": false, "daysToRepeat": [ { "dayOfWeek": 0, "repeat": false }, { "dayOfWeek": 1, "repeat": false }, { "dayOfWeek": 2, "repeat": false }, { "dayOfWeek": 3, "repeat": false }, { "dayOfWeek": 4, "repeat": false }, { "dayOfWeek": 5, "repeat": false }, { "dayOfWeek": 6, "repeat": false } ], }) } onRejected: alarmDialog.close() </pre> <a name="alarmdelegate-op-op-qml"></a> <h3 id="alarmdelegate-op-op-qml"><code>AlarmDelegate.qml</code></h3> <p>Each alarm in the main screen is an ItemDelegate. The ItemDelegate <code>root</code> contains all fields on the main screen and the detail screen. The detail screen's fields are only visible after an alarm has been clicked on, i.e. when <code>root.checked</code> is <code>true</code>.</p> <pre class="cpp"> ItemDelegate { id: root width: parent.width checkable: true onClicked: ListView.view.currentIndex = index contentItem: ColumnLayout { spacing: 0 RowLayout { ColumnLayout { id: dateColumn readonly property date alarmDate: new Date( model.year, model.month - 1, model.day, model.hour, model.minute) Label { id: timeLabel font.pixelSize: Qt.application.font.pixelSize * 2 text: dateColumn.alarmDate.toLocaleTimeString(window.locale, Locale.ShortFormat) } RowLayout { Label { id: dateLabel text: dateColumn.alarmDate.toLocaleDateString(window.locale, Locale.ShortFormat) } Label { id: alarmAbout text: "βΈ± " + model.label visible: model.label.length > 0 && !root.checked } } } Item { Layout.fillWidth: true } Switch { checked: model.activated Layout.alignment: Qt.AlignTop onClicked: model.activated = checked } } CheckBox { id: alarmRepeat text: qsTr("Repeat") checked: model.repeat visible: root.checked onToggled: model.repeat = checked } Flow { visible: root.checked && model.repeat Layout.fillWidth: true Repeater { id: dayRepeater model: daysToRepeat delegate: RoundButton { text: Qt.locale().dayName(model.dayOfWeek, Locale.NarrowFormat) flat: true checked: model.repeat checkable: true Material.background: checked ? Material.accent : "transparent" onToggled: model.repeat = checked } } } TextField { id: alarmDescriptionTextField placeholderText: qsTr("Enter description here") cursorVisible: true visible: root.checked text: model.label onTextEdited: model.label = text } Button { id: deleteAlarmButton text: qsTr("Delete") width: 40 height: 40 visible: root.checked onClicked: root.ListView.view.model.remove(root.ListView.view.currentIndex, 1) } } } </pre> <a name="alarmmodel-op-op-qml"></a> <h3 id="alarmmodel-op-op-qml"><code>AlarmModel.qml</code></h3> <p>This QML file contains the definition of <code>alarmModel</code>, the ListModel that manages the alarm data.</p> <p>It creates five ListElements with example alarms.</p> <pre class="cpp"> import QtQuick 2.11 // Populate the model with some sample data. ListModel { id: alarmModel ListElement { hour: 6 minute: 0 day: 2 month: 8 year: 2018 activated: true label: "Wake up" repeat: true daysToRepeat: [ ListElement { dayOfWeek: 0; repeat: false }, ListElement { dayOfWeek: 1; repeat: false }, ListElement { dayOfWeek: 2; repeat: false }, ListElement { dayOfWeek: 3; repeat: false }, ListElement { dayOfWeek: 4; repeat: false }, ListElement { dayOfWeek: 5; repeat: false }, ListElement { dayOfWeek: 6; repeat: false } ] } ListElement { hour: 6 minute: 0 day: 3 month: 8 year: 2018 activated: true label: "Wake up" repeat: true daysToRepeat: [ ListElement { dayOfWeek: 0; repeat: true }, ListElement { dayOfWeek: 1; repeat: true }, ListElement { dayOfWeek: 2; repeat: true }, ListElement { dayOfWeek: 3; repeat: true }, ListElement { dayOfWeek: 4; repeat: true }, ListElement { dayOfWeek: 5; repeat: false }, ListElement { dayOfWeek: 6; repeat: false } ] } ListElement { hour: 7 minute: 0 day: 3 month: 8 year: 2018 activated: false label: "Exercise" repeat: true daysToRepeat: [ ListElement { dayOfWeek: 0; repeat: true }, ListElement { dayOfWeek: 1; repeat: true }, ListElement { dayOfWeek: 2; repeat: true }, ListElement { dayOfWeek: 3; repeat: true }, ListElement { dayOfWeek: 4; repeat: true }, ListElement { dayOfWeek: 5; repeat: true }, ListElement { dayOfWeek: 6; repeat: true } ] } ListElement { hour: 5 minute: 15 day: 1 month: 9 year: 2018 activated: true label: "" repeat: false daysToRepeat: [ ListElement { dayOfWeek: 0; repeat: false }, ListElement { dayOfWeek: 1; repeat: false }, ListElement { dayOfWeek: 2; repeat: false }, ListElement { dayOfWeek: 3; repeat: false }, ListElement { dayOfWeek: 4; repeat: false }, ListElement { dayOfWeek: 5; repeat: false }, ListElement { dayOfWeek: 6; repeat: false } ] } ListElement { hour: 5 minute: 45 day: 3 month: 9 year: 2018 activated: false label: "" repeat: false daysToRepeat: [ ListElement { dayOfWeek: 0; repeat: false }, ListElement { dayOfWeek: 1; repeat: false }, ListElement { dayOfWeek: 2; repeat: false }, ListElement { dayOfWeek: 3; repeat: false }, ListElement { dayOfWeek: 4; repeat: false }, ListElement { dayOfWeek: 5; repeat: false }, ListElement { dayOfWeek: 6; repeat: false } ] } } </pre> <a name="tumblerdelegate-qml"></a> <h3 id="tumblerdelegate-qml">TumblerDelegate.qml</h3> <p>TumblerDelegate defines the graphical properties of the Tumblers.</p> <pre class="cpp"> import QtQuick 2.11 import QtQuick.Controls 2.4 import QtQuick.Controls.Material 2.4 Text { text: modelData color: Tumbler.tumbler.Material.foreground font: Tumbler.tumbler.font opacity: 1.0 - Math.abs(Tumbler.displacement) / (Tumbler.tumbler.visibleItemCount / 2) horizontalAlignment: Text.AlignHCenter verticalAlignment: Text.AlignVCenter } </pre> <a name="entering-new-alarms"></a> <h2 id="entering-new-alarms">Entering new alarms</h2> <p>At the bottom of the startup screen, you can see a Button for adding alarms. Click it to open the <b>Add new alarm</b> dialog.</p> <pre class="cpp"> RoundButton { id: addAlarmButton text: "+" anchors.bottom: alarmListView.bottom anchors.bottomMargin: 8 anchors.horizontalCenter: parent.horizontalCenter onClicked: alarmDialog.open() } </pre> <p>The dialog for new alarms:</p> <p class="centerAlign"><img src="images/addalarms.png" alt=""Add alarms"" /></p><p>All fields are entered using Tumbler QML types. If you press <code>OK</code>, the values selected in the Tumblers are written to <code>alarmModel</code>.</p> <pre class="cpp"> contentItem: RowLayout { RowLayout { id: rowTumbler Tumbler { id: hoursTumbler model: 24 delegate: TumblerDelegate { text: formatNumber(modelData) } } Tumbler { id: minutesTumbler model: 60 delegate: TumblerDelegate { text: formatNumber(modelData) } } } RowLayout { id: datePicker Layout.leftMargin: 20 property alias dayTumbler: dayTumbler property alias monthTumbler: monthTumbler property alias yearTumbler: yearTumbler readonly property var days: [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] Tumbler { id: dayTumbler function updateModel() { // Populate the model with days of the month. For example: [0, ..., 30] var previousIndex = dayTumbler.currentIndex var array = [] var newDays = datePicker.days[monthTumbler.currentIndex] for (var i = 1; i <= newDays; ++i) array.push(i) dayTumbler.model = array dayTumbler.currentIndex = Math.min(newDays - 1, previousIndex) } Component.onCompleted: updateModel() delegate: TumblerDelegate { text: formatNumber(modelData) } } Tumbler { id: monthTumbler onCurrentIndexChanged: dayTumbler.updateModel() model: 12 delegate: TumblerDelegate { text: window.locale.standaloneMonthName(modelData, Locale.ShortFormat) } } Tumbler { id: yearTumbler // This array is populated with the next three years. For example: [2018, 2019, 2020] readonly property var years: (function() { var currentYear = new Date().getFullYear() return [0, 1, 2].map(function(value) { return value + currentYear; }) })() model: years delegate: TumblerDelegate { text: formatNumber(modelData) } } } } } </pre> <a name="editing-alarms"></a> <h2 id="editing-alarms">Editing alarms</h2> <p>If you click on a particular alarm, you can edit it in the detail screen.</p> <p class="centerAlign"><img src="images/detailscreen.png" alt="" /></p><p>Clicking on an alarm sets <code>root.checked</code> to <code>true</code>, which makes visible the fields of the detail screen.</p> <pre class="cpp"> visible: root<span class="operator">.</span>checked </pre> <p>If you want the alarm to trigger also on other days, check <code>alarmRepeat</code>. The Repeater will display a checkable RoundButton for each day of the week.</p> <pre class="cpp"> Flow { visible: root.checked && model.repeat Layout.fillWidth: true Repeater { id: dayRepeater model: daysToRepeat delegate: RoundButton { text: Qt.locale().dayName(model.dayOfWeek, Locale.NarrowFormat) flat: true checked: model.repeat checkable: true Material.background: checked ? Material.accent : "transparent" onToggled: model.repeat = checked } } } </pre> <p>If you modify the description of the alarm, it will be reflected in the main screen afterwards.</p> <pre class="cpp"> TextField { id: alarmDescriptionTextField placeholderText: qsTr("Enter description here") cursorVisible: true visible: root.checked text: model.label onTextEdited: model.label = text } </pre> <a name="deleting-alarms"></a> <h2 id="deleting-alarms">Deleting alarms</h2> <p>The detail screen (see above) has a Button for deleting alarms. When <code>onClicked</code> is emitted, the current ListElement is deleted from <code>alarmModel</code>.</p> <pre class="cpp"> Button { id: deleteAlarmButton text: qsTr("Delete") width: 40 height: 40 visible: root.checked onClicked: root.ListView.view.model.remove(root.ListView.view.currentIndex, 1) } } } </pre> <a name="summary"></a> <h3 id="summary">Summary</h3> <p>The app has no code for adding sound or vibration to the alarm, nor does it store the alarms in any format or database. Maybe it could be an interesting coding project to add those features. Adding sound to this program can be realized with Qt Multimedia QML Types. Storing the data could be done quickly and easily in JSON format.</p> <p>Files:</p> <ul> <li><a href="qtdoc-tutorials-alarms-alarmdelegate-qml.html">tutorials/alarms/AlarmDelegate.qml</a></li> <li><a href="qtdoc-tutorials-alarms-alarmdialog-qml.html">tutorials/alarms/AlarmDialog.qml</a></li> <li><a href="qtdoc-tutorials-alarms-alarmmodel-qml.html">tutorials/alarms/AlarmModel.qml</a></li> <li><a href="qtdoc-tutorials-alarms-tumblerdelegate-qml.html">tutorials/alarms/TumblerDelegate.qml</a></li> <li><a href="qtdoc-tutorials-alarms-alarms-pro.html">tutorials/alarms/alarms.pro</a></li> <li><a href="qtdoc-tutorials-alarms-main-cpp.html">tutorials/alarms/main.cpp</a></li> <li><a href="qtdoc-tutorials-alarms-main-qml.html">tutorials/alarms/main.qml</a></li> <li><a href="qtdoc-tutorials-alarms-qml-qrc.html">tutorials/alarms/qml.qrc</a></li> </ul> </div> <p><b>See also </b>Qt Multimedia QML Types and JSON Support in Qt.</p> <!-- @@@tutorials/alarms --> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2019 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.<br/> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br/> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. </p> </div> </body> </html>