Reader small image

You're reading from  Hands-On Embedded Programming with C++17

Product typeBook
Published inJan 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781788629300
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Maya Posch
Maya Posch
author image
Maya Posch

Maya Posch is a senior C++ developer with more than 15 years of experience. Discovering the joys of programming early on, and later the joys of electronics, she has always expressed a profound interest in technology, a passion that she gladly shares with others. Describing herself as a C developer who happens to like C++ and Ada, she likes to seek the limits of what can be done with the minimum of code and hardware to accomplish everything that is cool, new, and exciting. She also enjoys FPGA development, AI, and robotics research, in addition to creative writing, music, and drawing.
Read more about Maya Posch

Right arrow

Chapter 10. Developing Embedded Systems with Qt

Qt (pronounced cute) is an advanced C++-based framework that covers a wide range of APIs, allowing you to implement networking, graphical user interfaces, parsing of data formats, the playing back and recording of audio, and much more. This chapter primarily covers the graphical aspect of Qt, and how to create advanced GUIs for embedded devices to provide an attractive and functional UI to users.

The topics covered in this chapter are as follows:

  • Creating advanced GUIs with Qt for embedded systems
  • Using Qt's 3D designer to create an infotainment UI
  • Extending an existing embedded system with a GUI

The power of the right framework


A framework is essentially a collection of code aimed at easing the development of software for a specific application. It provides the developer with a range of classes—or the language equivalent—to allow you to implement the application logic without having to worry about interfacing with the underlying hardware, or using the OS's APIs.

In previous chapters, we used a number of frameworks to make our development efforts easier, from the No date Framework (Chapter 4, Resource-Restricted Embedded Systems) and CMSIS to Arduino for microcontrollers (MCUs), and from the low-level POCO framework for cross-platform development to the higher-level Qt framework.

Each of these frameworks has a specific type of system that they are intended for. For No date, CMSIS, and Arduino, the target is MCUs, ranging from 8-bit AVR MCUs to 32-bit ARM MCUs. These target the bare-metal systems, without any intermediate operating system (OS) or similar. Above those in terms of complexity...

Qt for command-line use


Even though the graphical user interface is a big selling point of the Qt framework, it is also possible to use it to develop command-line-only applications. For this, we just use the QCoreApplication class to create an input and an event loop handler, as in this example:

#include <QCoreApplication> 
#include <core.h> 
 
int main(int argc, char *argv[]) { 
   QCoreApplication app(argc, argv); 
   Core core; 
 
   connect(&core, &Core::done, &app, &app::quit, Qt::QueuedConnection); 
   core.start(); 
 
   return app.exec(); 
} 

Here, our code is implemented in a class called Core. In the main function, we create a QCoreApplication instance, which receives the command-line parameters. We then instantiate an instance of our class.

We connect a signal from our class to the QCoreApplication instance, so that if we signal that we have finished, it will trigger a slot on the latter to clean up and terminate the application.

After this, we call the...

GUI-based Qt applications


Returning to the Qt-based example project from Chapter 8, Example - Linux-Based Infotainment System, we can now compare its main function to the preceding command-line-only version to see what changes once we add a GUI to the project:

#include "mainwindow.h" 
#include <QApplication> 
 
int main(int argc, char *argv[]) { 
    QApplication a(argc, argv); 
    MainWindow w; 
    w.show(); 
     
    return a.exec(); 
} 

 

The most obvious change here is that we use QApplication instead of QCoreApplication. The other big change is that we do not use a completely custom class, but one that derives from QMainWindow:

#include <QMainWindow> 
 
#include <QAudioRecorder> 
#include <QAudioProbe> 
#include <QMediaPlayer> 
 
 
namespace Ui { 
    class MainWindow; 
} 
 
class MainWindow : public QMainWindow { 
    Q_OBJECT 
     
public: 
    explicit MainWindow(QWidget *parent = nullptr); 
    ~MainWindow(); 
     
public slots: 
    void playBluetooth...

Embedded Qt


A major target of the Qt framework next to desktop systems are embedded systems, specifically Embedded Linux, where there are a few different ways to use Q. The main point of embedded Qt is to optimize the software stock by allowing you to boot straight into a Qt-optimized environment, and by allowing for a variety of ways to render to the display.

Qt for Embedded Linux supports the following platform plugins for rendering:

Custom GUIs with stylesheets


The standard widget-based GUIs that desktop systems tend to use do not lend themselves that readily to customization. As a result, you are generally faced with having to either override the painting function in a QWidget instance and handle every single pixel of the widget drawing, or to use stylesheet-based customization.

Qt stylesheets allow you to tweak the look and feel of individual widgets, even dynamically. They are essentially written using Cascading Style Sheet (CSS) syntax as used with HTML pages. They allow you to change elements of a widget, such as the borders, rounding corners, or the thickness and color of the elements.

QML


Qt Modeling Language (QML) is a user interface markup language. It is strongly based on JavaScript and even uses inline JavaScript. It can be used to create dynamic and completely custom user interfaces, and is usually used together with the Qt Quick module.

Later in this chapter, we will take an in-depth look at how a dynamic GUI is created.

3D designer


With Qt 5, the Qt 3D module was introduced, which streamlined access to the OpenGL rendering API. This new module was used as the foundation for the Qt 3D Designer editor and the accompanying runtime. It can be used to create highly dynamic GUIs, featuring a combination of 2D and 3D elements.

It is quite similar to hand-crafted QML-based GUIs, but provides a more streamlined workflow, ease of adding animations, and previewing the project. It's similar to the Qt Designer Studio, which focuses more on 2D GUIs, but this one is not available for free, instead requiring you to purchase a license.

An example of adding a GUI to the infotainment system


In this example, we will be using C++, Qt, and QML to create a graphical user interface that is capable of showing the current track that is playing, performing an audio visualization, indicating the playback progress, and allowing you to toggle different input modes using onscreen buttons.

This example is based on the Audio Visualizer example from the Qt documentation. This can be found in the Qt installation folder (if examples got installed), as well as on the Qt site: https://doc.qt.io/qt-5/qt3d-audio-visualizer-qml-example.html.

The main difference between this code and the official example is that the QMediaPlayer media player was moved into the C++ code, along with a number of other functions. Instead, a number of signals and slots between the QML UI and C++ backend are used in the new QmlInterface class for button presses, updating the UI, and interaction with the media player.

A GUI such as this could be wired into the existing...

Summary


In this chapter, we looked at the myriad ways in which the Qt framework can be used to develop for embedded systems. We briefly looked at how it compares with other frameworks and how Qt is optimized for these embedded platforms, before working through an example of a QML-based GUI that could be added to the infotainment system we previously created.

You should now be able to create basic Qt applications, both purely command line-based and with a GUI. You should also have a clear idea of which options Qt offers to develop GUIs with.

In the next chapter, we will be taking a look at the next evolution of embedded platforms, using field-programmable gate arrays (FPGAs) to add custom, hardware-based functionality to speed up embedded platforms.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Hands-On Embedded Programming with C++17
Published in: Jan 2019Publisher: PacktISBN-13: 9781788629300
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Maya Posch

Maya Posch is a senior C++ developer with more than 15 years of experience. Discovering the joys of programming early on, and later the joys of electronics, she has always expressed a profound interest in technology, a passion that she gladly shares with others. Describing herself as a C developer who happens to like C++ and Ada, she likes to seek the limits of what can be done with the minimum of code and hardware to accomplish everything that is cool, new, and exciting. She also enjoys FPGA development, AI, and robotics research, in addition to creative writing, music, and drawing.
Read more about Maya Posch

Plugin

Description

EGLFS

Provides an interface to OpenGL ES or similar 3D rendering API. Usually, the default configuration for Embedded Linux. More details about EGL can be found at the following address: https://www.khronos.org/egl.

LinuxFB

Writes directly to the framebuffer via Linux's fbdev subsystem. Only software-rendered content is supported. As a result, on some setups the display performance is likely to be limited.

DirectFB

Directly writes to the graphic card's framebuffer using the DirectFB library.

Wayland

Uses the Wayland windowing system. This allows for...