Sunday, 6 April 2014

Weather Forecast (First Ubuntu Touch app tutorial)

Welcome to the fifth part of the tutorial. This tutorial will be wrap-up of what we have done so far.

import QtQuick 2.0
import Ubuntu.Components 0.1
import "components"

    \brief MainView with a Label and Button elements.

MainView {
    id: root

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

    // Note! applicationName needs to match the "name" field of the click manifest
    applicationName: "com.ubuntu.developer.karthik.upadya1.wrapup"

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

    property string currentTime: ""
    property string currentDate: ""
    property int intervalIndex: 0
    property int languageIndex: 0

    function updateTime () {
        var date = new Date
        root.currentDate = Qt.formatDateTime(date, "dd - MM - yyyy");
        root.currentTime = Qt.formatDateTime(date, "hh : mm : ss");


    Page {
        title:"Weather Forecast")
        Timer {
            id: timer
            running: && visible == true
            repeat: true
            triggeredOnStart: true
            onTriggered: {
                interval: 1000

        Column {
            anchors {
                fill: parent

            Row {
                id: line1

                Label {
                    id: clockText
                    fontSize: "large"
                    color: "lightgreen"
                    text: + "\t" + currentTime)

            Row {
                id: line2

                OptionSelector {
                    id: intervalTime
                    width: root.width * 0.4
                    selectedIndex: root.intervalIndex
                    highlightWhenPressed: true
                    model: ["Interval"),
                  "Every 3 hour"),
                  "Two days"),
                  "Three days"),
                  "Four days"),
                  "Five days"),
                  "Six days"),
                  "One week"),
                  "Eight days"),
                  "Nine days"),
                  "Ten days"),
                  "Eleven days"),
                  "Twelve days"),
                  "Thirteen days"),
                  "Two weeks"),]
                    containerHeight: itemHeight * 3
                    onSelectedIndexChanged: {
                        intervalIndex = selectedIndex

                OptionSelector {
                    id: languageSelect
                    width: root.width * 0.5
                    selectedIndex: root.languageIndex
                    model: ["Language"),
                  "Chinese Traditional"),
                  "Chinese Simplified"),
                    containerHeight: itemHeight * 3
                    onSelectedIndexChanged: {
                        languageIndex = selectedIndex

            Row {
                id: line3

                TextField {
                    id: cityName
                    width: root.width * 0.6
                    placeholderText:"Enter city name")

                Button {
                    id: searchButton
                    onClicked: {
                        print("Add the logic for getting and displaying weather data here")

When you execute the project the output will be like this:

On clicking the button you will get some message in standard output.

Now to an important aspect of publishing your app, If you are from android background you know about package namespace i.e. reverse domain name to uniquely identify your application. Ubuntu also has same concept. It is set in applicationName property. You can see it above.

When you are registering for publishing your app in Ubuntu store it asks for this information in Package namespace. By default it will be "com.ubuntu.developer.<your-name>". If you have a domain you can use it too.
The same package namespace must be used in applicationName property along with name of your main qml file in your project.

For Example:
I named above project as wrapup. So my qml filename is wrapup.qml
My package namespace is "com.ubuntu.developer.karthik.upadya1"
So I have set applicationName property as com.ubuntu.developer.karthik.upadya1.wrapup

And there is more hing you have to check for, in your Ubuntu-SDK IDE click on "Publish" in left panel. In General tab there are 5 text fields.
You have to make sure that value of Name field and applicationName property are same. If they are not just copy paste the appliactionName value in Name field.
You can put your name in Maintainer field.
Leave the Title field as it is.
Next you can give one line description about your app in Description field.
Version field indicate the current version. Follow your own version method. But keep in mind that as you update the app you must increase the version number.

If you have any doubts or problems you can mail me at or comment below.
In next tutorial we will see how to fetch the weather data.
Happy developing :)