SailfishOS code walkthrough: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
'''English''' | [[SailfishOS code walkthrough Polish|Polski]]
[[Category:SailfishOS::Introduction_tutorial]]


_________________________________________<br /> Article is part 4 of [[wiki/Category:SailfishOS::Introduction tutorial|Introduction Tutorial]].<br />[[wiki/SailfishOS using app in emulator basics|&lt; Go To Part 3]]<br /> _________________________________________
[toc align_right=&quot;yes&amp;quot; depth=&quot;2&amp;quot;]


=Code Walkthrough=
'''English''' | [[:SailfishOS_code_walkthrough_Polish|Polski]]


The template contains the essentials of a simple Sailfish OS application. We’ll go through the code to help understand the essentials.
''''_<br />Article is part 4 of &quot;Introduction Tutorial&amp;quot;:/wiki/Category:SailfishOS::Introduction_tutorial.<br />&quot;&lt; Go To Part 3&amp;quot;:/wiki/SailfishOS_using_app_in_emulator_basics<br />''''_


When generating a new project based on the Sailfish OS application template, some of the files in the Qt Creator project are named after the name of the project. In the following discussion the project name “myfirstapp” is used as an example.
= Code Walkthrough =


==Application entry==
The template contains the essentials of a simple Sailfish OS application. We'll go through the code to help understand the essentials.


'''src/myfirstapp.cpp'''<br /> Every Sailfish application must define a simple Qt C++ application project that creates a QQuickView and instantiates a <span class="caps">QML</span> file with an ApplicationWindow as the top-level item. However, other than implementing the <span class="caps">QML</span> file itself, you don’t need to do anything else to accomplish this.
When generating a new project based on the Sailfish OS application template, some of the files in the Qt Creator project are named after the name of the project. In the following discussion the project name &quot;myfirstapp&amp;quot; is used as an example.


Assuming you named your project “myfirstapp”, the Sailfish OS application template generates the source file src/myfirstapp.cpp that does the heavy lifting for you. This file implements the entry point to your application by simply passing the argument count and argument array to the function SailfishApp::main(). This function in turn creates the required QGuiApplication and QQuickView instances and loads your main <span class="caps">QML</span> file.
== Application entry ==


Note that the name of the <span class="caps">QML</span> file is not actually passed to SailfishApp::main(). Instead, the function expects the <span class="caps">QML</span> file name to be based on the name of your target. Again, if your project name is “myfirstapp”, the Qt project file will contain the declaration <span class="caps">TARGET</span> = myfirstapp, and main() will load the <span class="caps">QML</span> file qml/myfirstapp.qml.
'''src/myfirstapp.cpp'''<br />Every Sailfish application must define a simple Qt C++ application project that creates a QQuickView and instantiates a QML file with an ApplicationWindow as the top-level item. However, other than implementing the QML file itself, you don't need to do anything else to accomplish this.


The application template creates the <span class="caps">QML</span> file for you but you should be aware of the fact that the file cannot be renamed without updating the <span class="caps">TARGET</span> definition in the .pro file.
Assuming you named your project &quot;myfirstapp&amp;quot;, the Sailfish OS application template generates the source file src/myfirstapp.cpp that does the heavy lifting for you. This file implements the entry point to your application by simply passing the argument count and argument array to the function SailfishApp::main(). This function in turn creates the required QGuiApplication and QQuickView instances and loads your main QML file.


==Top-level <span class="caps">QML</span> file==
Note that the name of the QML file is not actually passed to SailfishApp::main(). Instead, the function expects the QML file name to be based on the name of your target. Again, if your project name is &quot;myfirstapp&amp;quot;, the Qt project file will contain the declaration TARGET = myfirstapp, and main() will load the QML file qml/myfirstapp.qml.


'''qml/myfirstapp.qml'''<br /> All <span class="caps">QML</span> files used by the application are in the directory qml or its subdirectories. When the application starts, the SailfishApp::main() function first loads the <span class="caps">QML</span> file qml/myfirstapp.qml.
The application template creates the QML file for you but you should be aware of the fact that the file cannot be renamed without updating the TARGET definition in the .pro file.


The first two import statements allow the application to import the Qt Quick and Sailfish Silica modules we will later use. In addition, the final import statement makes the <span class="caps">QML</span> files under the pages directory available to myfirstapp.qml.
<code>#ifdef QT_QML_DEBUG<br />#include &lt;QtQuick&amp;gt;<br />#endif
 
#include &lt;sailfishapp.h&amp;gt;
 
int main(int argc, char '''argv[])<br />{<br /> // SailfishApp::main() will display &quot;qml/myfirstapp.qml&amp;quot;, if you need more<br /> // control over initialization, you can use:<br /> //<br /> // - SailfishApp::application(int, char'''[]) to get the QGuiApplication *<br /> // - SailfishApp::createView() to get a new QQuickView * instance<br /> // - SailfishApp::pathTo(QString) to get a QUrl to a resource file<br /> //<br /> // To display the view, call &quot;show()&quot; (will show fullscreen on device).
 
return SailfishApp::main(argc, argv);<br />}</code>
 
== Top-level QML file ==
 
'''qml/myfirstapp.qml'''<br />All QML files used by the application are in the directory qml or its subdirectories. When the application starts, the SailfishApp::main() function first loads the QML file qml/myfirstapp.qml.
 
<code>import QtQuick 2.0<br />import Sailfish.Silica 1.0<br />import &quot;pages&amp;quot;</code>
 
The first two import statements allow the application to import the Qt Quick and Sailfish Silica modules we will later use. In addition, the final import statement makes the QML files under the pages directory available to myfirstapp.qml.
 
<code>ApplicationWindow<br />{<br /> initialPage: Component { FirstPage { } }<br /> cover: Qt.resolvedUrl(&quot;cover/CoverPage.qml&amp;quot;)<br />}</code>


ApplicationWindow is the top-level type of all Sailfish Silica applications. The initialPage property specifies the first page to display when the application is opened. The cover property sets the active cover to be displayed when the application is pushed to the background.
ApplicationWindow is the top-level type of all Sailfish Silica applications. The initialPage property specifies the first page to display when the application is opened. The cover property sets the active cover to be displayed when the application is pushed to the background.


==First Page of the Application==
== First Page of the Application ==


'''qml/pages/FirstPage.qml'''<br />
'''qml/pages/FirstPage.qml'''<br /><code>Page {<br /> id: page</code>


Here, we first create a Page object which is simply a container for the contents of the page.<br /> To enable a pull-down menu, we need to create a flickable item which places its children on a surface that can be pulled and flicked. We do this using the SilicaFlickable component. Sailfish Silica provides the types SilicaFlickable, SilicaListView, and SilicaGridView which are styled versions of the Qt Quick flickable, list view, and grid view types.
Here, we first create a Page object which is simply a container for the contents of the page.<br />To enable a pull-down menu, we need to create a flickable item which places its children on a surface that can be pulled and flicked. We do this using the SilicaFlickable component. Sailfish Silica provides the types SilicaFlickable, SilicaListView, and SilicaGridView which are styled versions of the Qt Quick flickable, list view, and grid view types.


Next, we add a pulldown menu with one menu item, labelled “Show Page 2”. We then attach an onClicked action to the MenuItem which will push the second page onto the top of the pageStack (provided by ApplicationWindow). Note that PullDownMenu and PushUpMenu must always be nested inside a SilicaFlickable, SilicaListView, or SilicaGridView.
<code>SilicaFlickable {<br /> anchors.fill: parent</code>
 
Next, we add a pulldown menu with one menu item, labelled &quot;Show Page 2&amp;quot;. We then attach an onClicked action to the MenuItem which will push the second page onto the top of the pageStack (provided by ApplicationWindow). Note that PullDownMenu and PushUpMenu must always be nested inside a SilicaFlickable, SilicaListView, or SilicaGridView.
 
<code> PullDownMenu {<br /> MenuItem {<br /> text: &quot;Show Page 2&amp;quot;<br /> onClicked: pageStack.push(Qt.resolvedUrl(&quot;SecondPage.qml&amp;quot;))<br /> }<br /> }</code>


We set the height of SilicaFlickable to be same as the height of its children (i.e., the content of the page).
We set the height of SilicaFlickable to be same as the height of its children (i.e., the content of the page).
<code> contentHeight: childrenRect.height <code>


Finally, we arrange the content vertically. The Column element positions its child items so that they are vertically aligned and not overlapping. We also provide the page title/header using the PageHeader element. The page header is always placed at the top of content. We add a welcome text to our page using the Label element.
Finally, we arrange the content vertically. The Column element positions its child items so that they are vertically aligned and not overlapping. We also provide the page title/header using the PageHeader element. The page header is always placed at the top of content. We add a welcome text to our page using the Label element.
</code> Column {<br /> id: column
width: page.width<br /> spacing: Theme.paddingLarge<br /> PageHeader {<br /> title: &quot;UI Template&amp;quot;<br /> }<br /> Label {<br /> x: Theme.paddingLarge<br /> text: &quot;Hello Sailors&amp;quot;<br /> color: Theme.secondaryHighlightColor<br /> font.pixelSize: Theme.fontSizeExtraLarge<br /> }<br /> }<code>


The column spacing, label x, color, and font.pixelSize properties use values from Theme type instead of hard coding sizes or colours. This ensures the application adapts to the currently active theme and does not clash with system provided components.
The column spacing, label x, color, and font.pixelSize properties use values from Theme type instead of hard coding sizes or colours. This ensures the application adapts to the currently active theme and does not clash with system provided components.


==Second Page of the Application==
== Second Page of the Application ==
 
'''qml/pages/SecondPage.qml'''<br />The second page is pretty simple. It declares SilicaListView which has a model to define the data to be displayed and a delegate to define how each index of the data should be displayed.
 
</code>Page {<br /> id: page<br /> SilicaListView {<br /> id: listView<br /> model: 20<br /> anchors.fill: parent<br /> header: PageHeader {<br /> title: &quot;Nested Page&amp;quot;<br /> }<br /> delegate: BackgroundItem {<br /> id: delegate
 
Label {<br /> x: Theme.paddingLarge<br /> text: &quot;Item &quot; + index<br /> anchors.verticalCenter: parent.verticalCenter<br /> color: delegate.highlighted ? Theme.highlightColor : Theme.primaryColor<br /> }<br /> onClicked: console.log(&quot;Clicked &quot; + index)<br /> }<br /> VerticalScrollDecorator {}<br /> }<br />}<code>


'''qml/pages/SecondPage.qml'''<br /> The second page is pretty simple. It declares SilicaListView which has a model to define the data to be displayed and a delegate to define how each index of the data should be displayed.
== Application cover ==


==Application cover==
'''qml/cover/CoverPage.qml'''<br />Covers are the visual representations of backgrounded applications that are displayed on the running applications screen. We create a cover using a CoverBackground element with a centered label inside it.


'''qml/cover/CoverPage.qml'''<br /> Covers are the visual representations of backgrounded applications that are displayed on the running applications screen. We create a cover using a CoverBackground element with a centered label inside it.
</code>CoverBackground {<br /> Label {<br /> id: label<br /> anchors.centerIn: parent<br /> text: &quot;My Cover&amp;quot;<br /> }<code>


A cover can specify a list of actions that can be performed on the background application. The list is defined using CoverActionList element. Each CoverActionList can define up to two CoverAction items to specify the actions.
A cover can specify a list of actions that can be performed on the background application. The list is defined using CoverActionList element. Each CoverActionList can define up to two CoverAction items to specify the actions.


In the snippet above we display next and pause actions on the cover using two icons. You can also attach signal handlers to these actions to perform a specific function and whilst we won’t cover that in detail in this tutorial, you can try adding onTriggered: console.log(“Cover next”) to the first CoverAction. Be sure to stop, rebuild, and rerun your application to see the results.
</code> CoverActionList {<br /> id: coverAction
 
CoverAction {<br /> iconSource: &quot;image://theme/icon-cover-next&amp;quot;<br /> }


==About Sailfish Silica components==
CoverAction {<br /> iconSource: &quot;image://theme/icon-cover-pause&amp;quot;<br /> }<br /> }<code>


It’s important to note that Sailfish Silica <span class="caps">QML</span> components are designed to be used together. While it may not be immediately obvious, several Silica components cooperate to provide platform-specific functionality.
In the snippet above we display next and pause actions on the cover using two icons. You can also attach signal handlers to these actions to perform a specific function and whilst we won't cover that in detail in this tutorial, you can try adding onTriggered: console.log(&quot;Cover next&amp;quot;) to the first CoverAction. Be sure to stop, rebuild, and rerun your application to see the results.
 
== About Sailfish Silica components ==
 
It's important to note that Sailfish Silica QML components are designed to be used together. While it may not be immediately obvious, several Silica components cooperate to provide platform-specific functionality.


Editable text fields are one example of this. TextField is the Silica component that provides a one-line editable text field. However, there are several other types that have an effect on how editable text fields behave.
Editable text fields are one example of this. TextField is the Silica component that provides a one-line editable text field. However, there are several other types that have an effect on how editable text fields behave.


If the TextField is not within a Silica Page, the text field may be obscured by the virtual keyboard. The Page type ensures that page content is scrolled to keep editable text fields visible when the virtual keyboard is shown. Similarly, if the root element of the <span class="caps">QML</span> application is not a Silica ApplicationWindow, the background of the virtual keyboard is not correctly styled making it difficult to read the keys.
If the TextField is not within a Silica Page, the text field may be obscured by the virtual keyboard. The Page type ensures that page content is scrolled to keep editable text fields visible when the virtual keyboard is shown. Similarly, if the root element of the QML application is not a Silica ApplicationWindow, the background of the virtual keyboard is not correctly styled making it difficult to read the keys.


If you encounter situations where your application behaves differently to other Sailfish OS applications, your first course of action should be to verify that you’re not unintentionally using standard Qt Quick components when a more applicable Silica component is available. This is most likely to be an issue if you’re porting an existing <span class="caps">QML</span> application to Sailfish OS.
If you encounter situations where your application behaves differently to other Sailfish OS applications, your first course of action should be to verify that you're not unintentionally using standard Qt Quick components when a more applicable Silica component is available. This is most likely to be an issue if you're porting an existing QML application to Sailfish OS.


==Other files and directories in this project==
== Other files and directories in this project ==


You will notice that in addition to the .qml and .cpp files, there are a couple of other files that we have not touched so far. Let’s quickly go through them before you start exploring the <span class="caps">SDK</span>.
You will notice that in addition to the .qml and .cpp files, there are a couple of other files that we have not touched so far. Let's quickly go through them before you start exploring the SDK.


myfirstapp.desktop is a standard Linux desktop configuration file that describes how a particular program is to be launched, how it appears in menus, etc. This file specifies for example the name and icon of your applcation as they appear in the launcher. More information on the desktop entry specification can be found at standards.freedesktop.org.
myfirstapp.desktop is a standard Linux desktop configuration file that describes how a particular program is to be launched, how it appears in menus, etc. This file specifies for example the name and icon of your applcation as they appear in the launcher. More information on the desktop entry specification can be found at standards.freedesktop.org.


myfirstapp.png is the launcher icon for the application. The Icon declaration in the .desktop file (e.g. the line Icon=/usr/share/icons/hicolor/86×86/apps/myfirstapp.png) refers to this image file. The application template takes care of deploying the icon to the correct location, and the Icon declaration in the .desktop file should always refer to the file by its absolute path. Similarly to the main <span class="caps">QML</span> file, the name of the icon is based on the <span class="caps">TARGET</span> declaration in the project file. Hence, the icon file name should not be changed unless the <span class="caps">TARGET</span> declaration is also updated.
myfirstapp.png is the launcher icon for the application. The Icon declaration in the .desktop file &amp;#40;e.g. the line Icon=/usr/share/icons/hicolor/86x86/apps/myfirstapp.png&amp;amp;#41; refers to this image file. The application template takes care of deploying the icon to the correct location, and the Icon declaration in the .desktop file should always refer to the file by its absolute path. Similarly to the main QML file, the name of the icon is based on the TARGET declaration in the project file. Hence, the icon file name should not be changed unless the TARGET declaration is also updated.
 
myfirstapp.pro is a project file that ties the source code and the resulting application binary together. The file describes how the application is built by creating the Makefile into the appropriate build directory, which is then executed to actually build the application. The <span class="caps">CONFIG</span> += sailfishapp declaration in the project file causes your project to be linked against the libsailfishapp library that provides the implementation of the SailfishApp::main() function discussed at the beginning of this tutorial. The <span class="caps">CONFIG</span> declaration also ensures the application binary and its data files are deployed to the proper locations both on the emulator and on devices. For more information on the format of the project file, see the qmake Manual.
 
The qml directory contains files that should be deployed as a part of your application. In addition to <span class="caps">QML</span> files, audio, image, and JavaScript files that are used by the application should be placed in this directory or in a subdirectory in it. Basically, anything you put in the qml directory gets deployed when your application is installed in the emulator or on a device.


==Conclusion==
myfirstapp.pro is a project file that ties the source code and the resulting application binary together. The file describes how the application is built by creating the Makefile into the appropriate build directory, which is then executed to actually build the application. The CONFIG += sailfishapp declaration in the project file causes your project to be linked against the libsailfishapp library that provides the implementation of the SailfishApp::main() function discussed at the beginning of this tutorial. The CONFIG declaration also ensures the application binary and its data files are deployed to the proper locations both on the emulator and on devices. For more information on the format of the project file, see the qmake Manual.


Creating intuitive Sailfish Silica UI applications is straightforward. The fact that they are based on <span class="caps">QML</span>, a declarative UI language, makes the process fast and easy. It brings in a rich set of user interface elements and an abundance of possibilities. Now you can explore and learn more about how to use them in your application. Check out the media gallery example code available in the <span class="caps">SDK</span> for more ideas.
The qml directory contains files that should be deployed as a part of your application. In addition to QML files, audio, image, and JavaScript files that are used by the application should be placed in this directory or in a subdirectory in it. Basically, anything you put in the qml directory gets deployed when your application is installed in the emulator or on a device.


_________________________________________<br /> Article is part 4 of [[wiki/Category:SailfishOS::Introduction tutorial|Introduction Tutorial]].<br />[[wiki/SailfishOS using app in emulator basics|&lt; Go To Part 3]]<br /> _________________________________________
== Conclusion ==


===Categories:===
Creating intuitive Sailfish Silica UI applications is straightforward. The fact that they are based on QML, a declarative UI language, makes the process fast and easy. It brings in a rich set of user interface elements and an abundance of possibilities. Now you can explore and learn more about how to use them in your application. Check out the media gallery example code available in the SDK for more ideas.


* [[:Category:SailfishOS|SailfishOS]]
''''_<br />Article is part 4 of &quot;Introduction Tutorial&amp;quot;:/wiki/Category:SailfishOS::Introduction_tutorial.<br />&quot;&lt; Go To Part 3&amp;quot;:/wiki/SailfishOS_using_app_in_emulator_basics
** [[:Category:SailfishOS::Introduction tutorial|Introduction_tutorial]]

Revision as of 10:17, 24 February 2015


[toc align_right="yes&quot; depth="2&quot;]

English | Polski

'_
Article is part 4 of "Introduction Tutorial&quot;:/wiki/Category:SailfishOS::Introduction_tutorial.
"< Go To Part 3&quot;:/wiki/SailfishOS_using_app_in_emulator_basics
'
_

Code Walkthrough

The template contains the essentials of a simple Sailfish OS application. We'll go through the code to help understand the essentials.

When generating a new project based on the Sailfish OS application template, some of the files in the Qt Creator project are named after the name of the project. In the following discussion the project name "myfirstapp&quot; is used as an example.

Application entry

src/myfirstapp.cpp
Every Sailfish application must define a simple Qt C++ application project that creates a QQuickView and instantiates a QML file with an ApplicationWindow as the top-level item. However, other than implementing the QML file itself, you don't need to do anything else to accomplish this.

Assuming you named your project "myfirstapp&quot;, the Sailfish OS application template generates the source file src/myfirstapp.cpp that does the heavy lifting for you. This file implements the entry point to your application by simply passing the argument count and argument array to the function SailfishApp::main(). This function in turn creates the required QGuiApplication and QQuickView instances and loads your main QML file.

Note that the name of the QML file is not actually passed to SailfishApp::main(). Instead, the function expects the QML file name to be based on the name of your target. Again, if your project name is "myfirstapp&quot;, the Qt project file will contain the declaration TARGET = myfirstapp, and main() will load the QML file qml/myfirstapp.qml.

The application template creates the QML file for you but you should be aware of the fact that the file cannot be renamed without updating the TARGET definition in the .pro file.

#ifdef QT_QML_DEBUG<br />#include &lt;QtQuick&amp;gt;<br />#endif

#include &lt;sailfishapp.h&amp;gt;

int main(int argc, char '''argv[])<br />{<br /> // SailfishApp::main() will display &quot;qml/myfirstapp.qml&amp;quot;, if you need more<br /> // control over initialization, you can use:<br /> //<br /> // - SailfishApp::application(int, char'''[]) to get the QGuiApplication *<br /> // - SailfishApp::createView() to get a new QQuickView * instance<br /> // - SailfishApp::pathTo(QString) to get a QUrl to a resource file<br /> //<br /> // To display the view, call &quot;show()&quot; (will show fullscreen on device).

return SailfishApp::main(argc, argv);<br />}

Top-level QML file

qml/myfirstapp.qml
All QML files used by the application are in the directory qml or its subdirectories. When the application starts, the SailfishApp::main() function first loads the QML file qml/myfirstapp.qml.

import QtQuick 2.0<br />import Sailfish.Silica 1.0<br />import &quot;pages&amp;quot;

The first two import statements allow the application to import the Qt Quick and Sailfish Silica modules we will later use. In addition, the final import statement makes the QML files under the pages directory available to myfirstapp.qml.

ApplicationWindow<br />{<br /> initialPage: Component { FirstPage { } }<br /> cover: Qt.resolvedUrl(&quot;cover/CoverPage.qml&amp;quot;)<br />}

ApplicationWindow is the top-level type of all Sailfish Silica applications. The initialPage property specifies the first page to display when the application is opened. The cover property sets the active cover to be displayed when the application is pushed to the background.

First Page of the Application

qml/pages/FirstPage.qml

Page {<br /> id: page

Here, we first create a Page object which is simply a container for the contents of the page.
To enable a pull-down menu, we need to create a flickable item which places its children on a surface that can be pulled and flicked. We do this using the SilicaFlickable component. Sailfish Silica provides the types SilicaFlickable, SilicaListView, and SilicaGridView which are styled versions of the Qt Quick flickable, list view, and grid view types.

SilicaFlickable {<br /> anchors.fill: parent

Next, we add a pulldown menu with one menu item, labelled "Show Page 2&quot;. We then attach an onClicked action to the MenuItem which will push the second page onto the top of the pageStack (provided by ApplicationWindow). Note that PullDownMenu and PushUpMenu must always be nested inside a SilicaFlickable, SilicaListView, or SilicaGridView.

 PullDownMenu {<br /> MenuItem {<br /> text: &quot;Show Page 2&amp;quot;<br /> onClicked: pageStack.push(Qt.resolvedUrl(&quot;SecondPage.qml&amp;quot;))<br /> }<br /> }

We set the height of SilicaFlickable to be same as the height of its children (i.e., the content of the page).

 contentHeight: childrenRect.height <code>

Finally, we arrange the content vertically. The Column element positions its child items so that they are vertically aligned and not overlapping. We also provide the page title/header using the PageHeader element. The page header is always placed at the top of content. We add a welcome text to our page using the Label element.

Column {
id: column width: page.width
spacing: Theme.paddingLarge
PageHeader {
title: "UI Template&quot;
}
Label {
x: Theme.paddingLarge
text: "Hello Sailors&quot;
color: Theme.secondaryHighlightColor
font.pixelSize: Theme.fontSizeExtraLarge
}
}

The column spacing, label x, color, and font.pixelSize properties use values from Theme type instead of hard coding sizes or colours. This ensures the application adapts to the currently active theme and does not clash with system provided components.

== Second Page of the Application ==

'''qml/pages/SecondPage.qml'''<br />The second page is pretty simple. It declares SilicaListView which has a model to define the data to be displayed and a delegate to define how each index of the data should be displayed.

Page {
id: page
SilicaListView {
id: listView
model: 20
anchors.fill: parent
header: PageHeader {
title: "Nested Page&quot;
}
delegate: BackgroundItem {
id: delegate Label {
x: Theme.paddingLarge
text: "Item " + index
anchors.verticalCenter: parent.verticalCenter
color: delegate.highlighted ? Theme.highlightColor : Theme.primaryColor
}
onClicked: console.log("Clicked " + index)
}
VerticalScrollDecorator {}
}
}

== Application cover ==

'''qml/cover/CoverPage.qml'''<br />Covers are the visual representations of backgrounded applications that are displayed on the running applications screen. We create a cover using a CoverBackground element with a centered label inside it.

CoverBackground {
Label {
id: label
anchors.centerIn: parent
text: "My Cover&quot;
}

A cover can specify a list of actions that can be performed on the background application. The list is defined using CoverActionList element. Each CoverActionList can define up to two CoverAction items to specify the actions.

CoverActionList {
id: coverAction

CoverAction {
iconSource: "image://theme/icon-cover-next&quot;
}

CoverAction {
iconSource: "image://theme/icon-cover-pause&quot;
}
}

In the snippet above we display next and pause actions on the cover using two icons. You can also attach signal handlers to these actions to perform a specific function and whilst we won't cover that in detail in this tutorial, you can try adding onTriggered: console.log("Cover next&quot;) to the first CoverAction. Be sure to stop, rebuild, and rerun your application to see the results.

About Sailfish Silica components

It's important to note that Sailfish Silica QML components are designed to be used together. While it may not be immediately obvious, several Silica components cooperate to provide platform-specific functionality.

Editable text fields are one example of this. TextField is the Silica component that provides a one-line editable text field. However, there are several other types that have an effect on how editable text fields behave.

If the TextField is not within a Silica Page, the text field may be obscured by the virtual keyboard. The Page type ensures that page content is scrolled to keep editable text fields visible when the virtual keyboard is shown. Similarly, if the root element of the QML application is not a Silica ApplicationWindow, the background of the virtual keyboard is not correctly styled making it difficult to read the keys.

If you encounter situations where your application behaves differently to other Sailfish OS applications, your first course of action should be to verify that you're not unintentionally using standard Qt Quick components when a more applicable Silica component is available. This is most likely to be an issue if you're porting an existing QML application to Sailfish OS.

Other files and directories in this project

You will notice that in addition to the .qml and .cpp files, there are a couple of other files that we have not touched so far. Let's quickly go through them before you start exploring the SDK.

myfirstapp.desktop is a standard Linux desktop configuration file that describes how a particular program is to be launched, how it appears in menus, etc. This file specifies for example the name and icon of your applcation as they appear in the launcher. More information on the desktop entry specification can be found at standards.freedesktop.org.

myfirstapp.png is the launcher icon for the application. The Icon declaration in the .desktop file &#40;e.g. the line Icon=/usr/share/icons/hicolor/86x86/apps/myfirstapp.png&amp;#41; refers to this image file. The application template takes care of deploying the icon to the correct location, and the Icon declaration in the .desktop file should always refer to the file by its absolute path. Similarly to the main QML file, the name of the icon is based on the TARGET declaration in the project file. Hence, the icon file name should not be changed unless the TARGET declaration is also updated.

myfirstapp.pro is a project file that ties the source code and the resulting application binary together. The file describes how the application is built by creating the Makefile into the appropriate build directory, which is then executed to actually build the application. The CONFIG += sailfishapp declaration in the project file causes your project to be linked against the libsailfishapp library that provides the implementation of the SailfishApp::main() function discussed at the beginning of this tutorial. The CONFIG declaration also ensures the application binary and its data files are deployed to the proper locations both on the emulator and on devices. For more information on the format of the project file, see the qmake Manual.

The qml directory contains files that should be deployed as a part of your application. In addition to QML files, audio, image, and JavaScript files that are used by the application should be placed in this directory or in a subdirectory in it. Basically, anything you put in the qml directory gets deployed when your application is installed in the emulator or on a device.

Conclusion

Creating intuitive Sailfish Silica UI applications is straightforward. The fact that they are based on QML, a declarative UI language, makes the process fast and easy. It brings in a rich set of user interface elements and an abundance of possibilities. Now you can explore and learn more about how to use them in your application. Check out the media gallery example code available in the SDK for more ideas.

'_
Article is part 4 of "Introduction Tutorial&quot;:/wiki/Category:SailfishOS::Introduction_tutorial.
"< Go To Part 3&quot;:/wiki/SailfishOS_using_app_in_emulator_basics