A stack of techical books

"There's No Silver Bullet"

We believe in methods that amplify learning, communication and collaborative teamwork. Methods that help us do our job better and faster.

In this section our experts share their insights and experience.

Articles

Reaktor Twitter

Qt Application Framework

Smooth application development with C++

Qt is a C++-based application framework which allows applications to be ported to different platforms with a minimum of hassle. C++ provides fine grained control over resource usage and performance, which is essential for certain applications. Well-known examples of Qt-based desktop software include Skype and Google Earth. It is also used in a number of embedded systems and is the primary application programming interface for MeeGo developers.

Qt was originally developed and owned by the Norwegian company Trolltech. The company was acquired by Nokia in 2008, and Nokia continues to develop Qt. At Nokia, Qts licensing has become more permissive and the development processes more open. Qt is currently available under both a commercial license and the open source LGPL.

Originally, Qt was used primarily in desktop environments and certain embedded systems. Since Nokia acquired it, the focus of development has moved towards mobile systems. However, Qt also continues to support several desktop environments. The inclusion of the Qt framework in Nokia devices has broadened its installed user base: Nokia reports that it has sold more than 100 million devices capable of running Qt-based software. It has also published plans for a future Qt version 5, which focuses on further improving Qt Quick, a programming technique for user interfaces. Qt Quick will be examined in more detail later on in this article.

Native code without a virtual machine

Unlike with many other modern application frameworks, the primary operating environment of Qt applications is not based on a virtual machine. Qt applications are written in the C++ programming language and compiled into binary code, which the device can execute directly. An exception to this rule is the Qt Quick, which includes a JavaScript virtual machine.

Applications written in C++ do not have automatic memory management, so developers must make sure their applications correctly reserve – and later, free – the memory they need. Memory handling is an integral part of the structure of a well-designed application, and manual memory management requires a diligent and experienced developer. Object hierarchies and ownerships must be designed according to the requirements set by memory management in order to minimize problems related to memory handling.

The Qt library aims to reduce the burden of the developer by helping with memory management: Qt objects can, for example, be ordered hierarchically, so that freeing any object will automatically free the objects below it in the hierarchy. A lot of Qt interface classes follow the implicit sharing idiom, which allows objects to be copied efficiently. The actual copy is made only after an object modifies some common data (copy-on-write semantics); this allows parameters and return values to be passed as value objects instead of object references. Programming with value objects is simpler and reduces the amount of memory management issues.

Qt simplifies event-based programming

Qt’s application development model endorses event-based programming. Events - called signals in Qt - are defined in class definitions, and events that take place during the execution of the program cause emission of these predefined signals. Objects have special event handlers, or ‘slots’, which can be connected to one or more signals. When a signal is triggered, Qt takes care of executing all event handlers connected to the particular signal. This method allows for clearer division of responsibility inside the program, separating the sender of an event from its handler.

Many Qt extensions, such as event-based programming and bindings to Qt Quick, require information about the program and its objects during execution. The C++ programming language, however, lacks support for supplying such metadata during the execution of a program. This is why Qt’s build environment includes a preprocessor, which generates metadata related to the structure of the program in the compilation phase. Because of this, Qt applications have access to better metaprogramming capabilities than C++ applications in general.

A clean and well-documented API

Qt’s application programming interface (API) is well documented, clean and consistent – especially for a library that has been developed for nearly 20 years. Qt developers have taken the quality of the library’s interface seriously from the start and have made sure that they document their principles for interface development.

The history of Qt includes also exceptions to the rule of high-quality APIs: for example designing a user interface with Qt Quick’s predecessor, QGraphicsWidget, is at times difficult and achieving the desired outcome may take a lot of time and effort. Qt’s primary weakness, however, is that, for historical reasons, it lacks support for C++ exceptions and therefore also the powerful error handling capabilities provided by them.

Qt guarantees compatibility for application binaries which use the same major version of the library. The goal is to have minimal non-backwards-compatible changes even when the major version changes, so that migrating applications to the new library version is as straightforward as possible.

A chink in Qt’s armor: poor testability

Unit testing Qt applications is fairly complicated. With C++, unit testing and emphasis on testability in general is not quite as common as with, for example, Java. Isolating the Qt dependencies of an application and replacing them with test implementations may be difficult and sometimes impossible.

Test-driven development has not gained much attention in Qt. Testing tools in Qt are thus not on the same level with the rest of the framework. The proportion of test code to production code in Qt versions 4.7 and 4.8 is roughly 20% to 80%. Because Qt's own development lacks a strong culture of unit testing, it is only natural that the testability of Qt applications is not as good as it could be. Qt offers developers a unit testing framework called QTestLib, which works well with simple unit tests, but is otherwise limited in functionality.

User interfaces with QML

The primary method of creating user interfaces in the latest Qt versions is Qt Quick. Its most important element is the JavaScript-based QML programming language, a dynamically typed language which is executed in a virtual machine beside the native program code. With the help of the run-time environment included in Qt Quick, QML integrates with native code, allowing the core part of the application to be written in C++. Native code is more efficient and offers additional ways to utilize the services offered by the Qt library. Some applications can be written purely in QML, but especially larger applications often also require the use of native code.

The core, written in native code, offers an interface that allows the QML user interface to communicate with it. A clear division of responsibility between the core and the user interface – and a well-designed interface between them – makes maintenance easy: minor changes to the user interface, for example, can often be made simply by modifying the QML code. Creation of a completely new user interface, such as a mobile version of a desktop application, however, often requires modifications also to the native core and its interface to QML. The changes that can be achieved only by modifying QML are limited. Good interface design and clear division of responsibility make adapting to new needs easier.

Thanks to QML’s likeness to JavaScript, programming with it is quite similar to web application development. Animations and event handlers, for example, can be done with just a few lines of code. QML’s way of connecting user interface components to each other feels natural. Other user interface technologies might benefit from adopting a similar approach.

Because the technology is still relatively new, best practices for using QML are still under development. The environment itself does not, for example, force developers to separate the structure, functionality and user interface layout of their application from each other – doing so requires the development team to have discipline and a set of agreed-upon conventions. If the application is not structured carefully, it may become tedious to maintain and develop.

Most work related to user interface development with the Qt framework has, for some time now, been focused on Qt Quick. Some applications, such as games, which have high memory and performance requirements, can use native libraries beside Qt Quick for increased performance.

Qt Creator is a competitive development environment

Besides the application framework, Qt offers development tools for the programmer. The most important of these is Qt Creator, an integrated development environment included with the framework. Qt Creator is tailored for Qt development, but can also be used for any other C++ development tasks.

A suitable IDE is an important part of a good programming experience. Qt Creator developers have spared no effort: it includes many useful features that are rare in the C++ world, such as code completion, syntax checking while writing and a versatile code navigator. These features, combined with a speed to match, make Qt Creator very competitive compared to other open-source C++ IDEs.

Qt Creator is based on the tools offered by the platform. Because these tools vary by platform, integration is not always seamless, and the user experience is not quite on a par with, for example, Visual Studio or XCode. In a Linux system, Qt Creator is a good choice for C++ development, even if Qt is not the actual target environment. When creating a Qt application, Qt Creator is a good choice regardless of the platform, because its features are tailored for Qt.

Qt makes C++ application development easier

Writing native code is always challenging, and C++ is known to be an especially difficult language to master. Qt makes C++ application development easier and also offers some excellent tools. The JavaScript-based QML, for example, is an innovative technology, and using it to create user interfaces feels natural.

Even though Qt makes it easier to get started and improves productivity, deeper knowledge of C++ remains very useful even when Qt is used.

Aki Saarinen closeup

Aki Saarinen, Software Architect

Aki is an enthusiastic developer who has worked with multiple programming languages including several years with C++.

Tuomas Järvensivu closeup

Tuomas Järvensivu, Software Architect

Tuomas is a software developer specialized in mobile software. He has strong experience in working with Qt.

Sami Rosendahl closeup

Sami Rosendahl, Chief Architect

Sami is a coding architect with almost 20 years of experience in C++.