SailfishOS Logic
English | Polski
Logic
Except showing nice pages containig widgets full of data usually application has to do some tasks to manipulate the data(in fact it is very common to treat providing data as a task itself). Obviously complexity of tasks vary between diffrent applications and use cases, but even the simplest ones maybe very important. For example the ‘add two given numbers’ opration in a calculator app is a very simple logic but without it the calculator app wouldn’t be very useful.
Available technology
Currently there are three languages supported for implementing business logic. However SailfishOS offically support two of them(which are btw. standard solution for Qt/QML apps):
- C++
- Javascript
Except C++ and Javascript there is also partial support for Python scripts. However it is not available to put apps based on python into jolla harbour [harbour.jolla.com] yet, but you can still install them on devices manually. More info here [wiki.merproject.org].
How can I put logic into my apps?
You can do it either implicitly – write logic as a functions in qml file or explicitly – call logic from external file. The implicit approach is only available with Javascript and it’s recommended only with simple routines. The external solution can be done with all of the supported technology and it’s considered as better approach. It lets you decouple code responsible for logic and code responsible for behaviour or appearence which is significat advantage while working on bigger projects.
C++
…
Javascript
Presented sinippets come from tutorial project ‘calqlator’ which is appeneded to SailfishOS IDE.
Importing external logic – \calqlatr\qml\calqlatr.qml
Implicit behaviour code – \calqlatr\qml\content\Display.qml.
Calling external functions – \calqlatr\qml\content\Button.qml.
External module with logic – \calqlatr\qml\content\calculator.js.
…
Python
Currently the best approach to develop with python on SailfishOS is to use PyOtherSide qml plugin, as it was mentioned before it should be supported in jolla harbour soon. Here [pyotherside.readthedocs.org] you can find documentation of PyOtherSide. It contains some examples and describes general idea.
Before deploying apps with PyOtherSide on targets with SailfishOS you need to follow guides from this article [lists.sailfishos.org]. Generally you need to instal python3 and PyOtherSide plugin on target machine(either device or emulator). Please read it before trying to build example code presented below.
External module with logic – ProjectName\qml\pages\duration.py
Calling external functions – \calqlatr\qml\content\Page.qml.
Entry point for qml pages – \calqlatr\qml\ProjectName.qml.
…