Hands-On Embedded Programming with Qt

Book description

A comprehensive guide that will get you up and running with embedded software development using Qt5

Key Features

  • Learn to create fluid, cross-platform applications for embedded devices
  • Achieve optimum performance in your applications with the QT Lite project
  • Explore the implementation of Qt with IoT using QtMqtt, QtKNX, and QtWebSockets

Book Description

Qt is an open source toolkit suitable for cross-platform and embedded application development. This book uses inductive teaching to help you learn how to create applications for embedded and Internet of Things (IoT) devices with Qt 5.

You'll start by learning to develop your very first application with Qt. Next, you'll build on the first application by understanding new concepts through hands-on projects and written text. Each project will introduce new features that will help you transform your basic first project into a connected IoT application running on embedded hardware. In addition to gaining practical experience in developing an embedded Qt project, you will also gain valuable insights into best practices for Qt development and explore advanced techniques for testing, debugging, and monitoring the performance of Qt applications. The examples and projects covered throughout the book can be run both locally and on an embedded platform.

By the end of this book, you will have the skills you need to use Qt 5 to confidently develop modern embedded applications.

What you will learn

  • Understand how to develop Qt applications using Qt Creator on Linux
  • Explore various Qt GUI technologies to build resourceful and interactive applications
  • Understand Qt's threading model to maintain a responsive UI
  • Get to grips with remote target load and debug using Qt Creator
  • Become adept at writing IoT code using Qt
  • Learn a variety of software best practices to ensure that your code is efficient

Who this book is for

This book is for software and hardware professionals with experience in different domains who are seeking new career opportunities in embedded systems and IoT. Working knowledge of the C++ Linux command line will be useful to get the most out of this book.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Hands-On Embedded Programming with Qt
  3. About Packt
    1. Why subscribe?
  4. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Code in Action
      4. Conventions used
    4. Get in touch
      1. Reviews
  6. Section 1: Getting Started with Embedded Qt
  7. Setting Up the Environment
    1. Technical requirements
    2. Creating our embedded environment
      1. Obtaining the hardware
      2. Loading the firmware
        1. Backing up the SD card
          1. Identifying the SD card device
          2. Grabbing the image
        2. Burning a new image
      3. Connections and first boot
        1. Network configuration
      4. Installing RSync
    3. Preparing the host machine
      1. Installing Qt
      2. Setting up the cross compilation environment
        1. Installing the cross compilation tools
        2. Preparing for debugging
        3. Accessing the Target's root directory on the host
    4. Building Qt for the Target
      1. Fixing the sources
      2. Building a program
        1. Configuring the Qt build
        2. Decoding the configure command
      3. Building the code
      4. Synchronizing the new components with the target
    5. Running a quick test to ensure how the program works
    6. Summary
    7. Questions
    8. Further reading
  8. Writing Your First Qt Application
    1. Technical requirements
    2. Introducing Qt Creator
      1. Starting Qt Creator
      2. Qt Creator welcomes you
        1. The Examples screen
        2. The Tutorials screen
        3. The Projects screen
        4. The left icon bar
    3. Greetings from Qt
      1. Creating a project
        1. Walking through the New File or Project wizard
          1. Choosing a template
          2. Picking a name and location
          3. Selecting a kit
          4. Defining the main window class
          5. Selecting a source control system for your project
      2. Examining the Edit screen
        1. Viewing projects and open files
        2. A place for editing
        3. Presenting outputs
      3. Designing the UI
        1. Placing a text label
      4. Running the code
      5. Protecting your work
    4. Who are you?
      1. Adding a place to type your name
      2. Causing a response
    5. Fixing a mistake
      1. Launching our application in the Qt Creator debugger
        1. Examining the Debug screen
        2. Setting a breakpoint
        3. Tripping the breakpoint
      2. Getting some help
    6. Digging deeper
      1. Explaining the push button code
      2. Sending and receiving between objects–signals and slots
        1. Cool things about signals and slots
        2. Rules of signals and slots
      3. Testing
    7. Summary
    8. Questions
    9. Further reading
  9. Running Your First Application on the Target
    1. Technical requirements
    2. Configuring Qt Creator for our Target
      1. Telling Qt Creator about a new device
      2. Telling Qt Creator about the cross-compiler
      3. Configuring the debugger for the Target
      4. Letting Qt Creator know about our Raspberry Pi Qt build
      5. Creating a Qt kit
    3. Building for the Target
      1. Adding a kit to the project
      2. Building the application
    4. Deploying on the Target
    5. Running the application on the Target
      1. Making a more flexible display using layouts
    6. Debugging on the Target
      1. Troubleshooting debugging
    7. Summary
    8. Questions
  10. Section 2: Working with Embedded Qt
  11. Important Qt Concepts
    1. Technical requirements
    2. Communicating effectively – signals and slots
      1. Quick info – Signals and slots
      2. Signals and slots in non-GUI code
        1. Preparing the header
        2. Using the QTimer
        3. Setting up the timer
        4. Connecting the QTimer signal
        5. Finishing the code
        6. Running our application
      3. Signals and slots using lambdas
      4. Queuing things up
        1. When should I use a queued connection?
        2. Implementing Signals Not Needed
    3. Looking inside – Qt's introspection support
      1. Properties
        1. Q_PROPERTY
      2. QVariant
      3. Enums
    4. Differing views — model/view architecture
      1. A quick dive
      2. Formalizing Qt's model/view
      3. Customizing how model data is viewed
      4. Filtering and sorting what's viewed
    5. Keeping it portable – Qt's platform abstraction
      1. Qt's OS and filesystem abstractions
      2. Qt's container class abstractions
    6. Taking a closer look at building and Main
      1. Qt Creator's supported build systems
        1. QMake
        2. Qbs
        3. CMake
      2. What's all that in main(..)?
    7. Summary
    8. Questions
  12. Managing the Overall Workflow
    1. Technical requirements
    2. Modernizing software development
      1. Falling down the development cycle
        1. Continuous Integration
        2. Unit testing
        3. Modern development cycle
      2. Moving ahead without hardware
      3. Layering the problem
    3. Starting our BigProject
      1. BigProject customer requirements
      2. Starting the Qt project
      3. Initial main window design
      4. Wiring up the time display
      5. Testing
      6. Commit it
    4. Mocking the hardware
      1. A new requirement
      2. Defining the interface
      3. Implementing the mock
      4. Injecting the mock
      5. Wiring up the temperature display
      6. Test it!
    5. Summary
    6. Questions
    7. Further reading
  13. Exploring GUI Technologies
    1. Technical requirements
    2. Two roads diverge
      1. Qt Widgets
        1. Programming widgets
          1. Creating a widget with Qt Creator/Designer
          2. Creating a widget by hand
      2. Doing something new – QML
        1. What is QML?
        2. Developing a UI in QML
    3. Revisiting some old friends
      1. Hello from Qt!
      2. Hello from QML!
        1. Creating the project
        2. Defining the window
        3. Customizing the display
        4. Using the Designer
          1. The Form Editor
          2. The Text Editor
          3. The Library window
          4. The Navigator window
          5. The Connections window
          6. The Properties window
          7. Experimenting with the QML Designer
    4. Comparing the technologies
    5. What if you didn't have to choose?
      1. New requirements for BigProject
        1. Designing the UI
          1. Creating the historical data form
          2. Implementing the historical table
          3. Adding the QtQuick (QML) chart
          4. Adding the temperature history form to the UI
        2. Providing data for the chart
          1. Storing the last reading
          2. Updating temperatureUpdate(..)
          3. Catching newReading in the chart
        3. Adjusting the .pro file
        4. Reformatting the History page
    6. Summary
    7. Questions
    8. Further reading
  14. Adding More Features
    1. Technical requirements
    2. Keeping records
      1. Defining the database structure
      2. Interfacing to the database
        1. Simplifying handling readings
        2. Defining the TemperatureStorage class
        3. Opening (and creating) the database
        4. Closing the database
        5. Saving data
        6. Retrieving readings
          1. Using a QSqlQuery
          2. Using QSqlTableModel
        7. Integrating the storage
    3. Designing with state machines
      1. Guess what marketing wants now?
        1. Determining the states
        2. Determining the conditions that cause transitions
        3. Drawing it out
      2. Qt's early state machine support
      3. An easier way to make state machines
        1. Using the state machine
          1. Updating the UI
          2. Creating an HVAC controller
          3. Wiring in the HVAC state machine
          4. Testing our simple thermostat
        2. Handling more requirements!
          1. Adding a new state
          2. Adding an event to a state
        3. Working with embedded states
    4. Searching for a keyboard
      1. Defining the input panel
      2. Adding the input panel to the GUI
      3. Solving a crash
        1. Decoding the fix
    5. Weaving a web of sockets
      1. Implementing the embedded WebSocket server
        1. Defining the WSReporter class
        2. Implementing WSReporter
          1. The constructor and destructor
          2. Handling connects and disconnects
          3. Sending the updated temperature
        3. Wiring in WSReporter
      2. Monitoring the Host
    6. Considering IoT security
      1. Authentication and encryption
      2. Anti-playback
      3. Implications on the BigProject
    7. Summary
    8. Questions
    9. Further reading
  15. Section 3: Deep Dive into Embedded Qt
  16. Qt in the Embedded World
    1. Technical requirements
    2. Microcontrollers in embedded super PCs
      1. Qt's place in the embedded world
      2. Last word on hardware choices
    3. Qt licensing and features
      1. Open-source version
      2. Commercial version
    4. Qt's embedded options
      1. Boot to Qt (B2Qt)
      2. Qt Configuration Tool
      3. Safety-critical display
      4. Qt for Medical
      5. Qt for Automotive
      6. Qt for Automation
      7. Qt for Device Creation
      8. Remote objects
      9. Qt UI development tools
      10. Virtual keyboard
    5. Summary
    6. Questions
  17. Exploring the IoT with Qt
    1. Technical requirements
    2. Examining the IoT and Qt
      1. The IoT – a brief history
      2. Where does Qt fit into the IoT?
    3. Forecasting the weather using a Web API
      1. Searching for a weather API
      2. Implementing WeatherFetcher
        1. What we need to know
        2. Constructing WeatherFetcher
        3. Requesting the current conditions
        4. Handling the API response
        5. Forcing a weather update
        6. Setting the location and weather units
        7. Starting and stopping WeatherFetcher
      3. Making use of WeatherFetcher
        1. Creating a WeatherFetcher instance
        2. Displaying the weather
        3. Setting the location and temperature inits
      4. Testing and committing
      5. Checking your results
    4. Reading the temperature sensor in Qt
      1. Installing the Sense HAT board
        1. Why didn't you just plug in another USB power supply for the display?
      2. Building the software support
        1. RTMULib – the low-level interface
          1. Creating a cross-compilation cmake file for the Raspberry Pi
          2. Building RTMULib
          3. Installing RTMULib on the target
          4. Testing RTMULib on the target
        2. Building QSensors for the Sense HAT
        3. Testing QSensors for the Sense HAT
      3. Reading the temperature in BigProject
        1. Creating an ambient temperature sensor class
        2. Wiring in the ambient temperature sensor
        3. Testing the temperature reading on the host and the target
    5. Publishing our status using MQTT
      1. Building IoT extensions
        1. Building Qt MQTT
        2. Building Qt KNX
      2. Using QtMQTT
        1. Creating the MqttClient class
        2. Implementing the MqttClient class
        3. Implementing the constructor
        4. Implementing our hostname and port setters
        5. Connecting and disconnecting from a broker
        6. Handling connection state changes and incoming messages
        7. Publishing BigProject's status
        8. Wiring in and setting up the MqttClient class
        9. Starting and stopping the MQTT publication
          1. Creating menu entries
          2. Assigning actions to menu items
        10. Testing the MQTT publication
      3. Final words on MQTT
    6. Summary
    7. Questions
    8. Further reading
  18. Using More Qt-Related Technologies
    1. Technical requirements
    2. Saving settings
      1. Searching for settings
      2. Settings file formats
      3. Using QSettings
        1. Digging deeper into QVariant and meta types
      4. Changes to MainWindow
        1. Loading settings
        2. Saving settings
      5. Updating TemperatureSensorIF and its children
      6. Fire it up and see whether it smokes!
      7. Continuing with QSettings
    3. Communicating using D-Bus
      1. Basic D-Bus concepts
      2. Qt's D-Bus support
        1. Tools
        2. Qt classes
          1. Adding D-Bus the brute-force way
          2. Adding D-Bus the tool-using way
      3. Preparing the Target for our D-Bus to enable applications
      4. Adding D-Bus to our project
        1. Writing the C++ interface class you want to use with D-Bus
        2. Generating XML from the class declaration
        3. Making fixes to the XML
        4. Generating the Adapter and Interface classes from the XML
        5. Hooking up to D-Bus
          1. Sending change notifications
          2. Receiving settings changes
        6. Testing our D-Bus code
    4. Remote GUI
      1. Preparing the Target
      2. A simple GUI
        1. Connecting the web GUI to BigProject using D-Bus
          1. Adding the D-Bus interface code
          2. Wiring in the D-Bus interface
          3. QML code changes
    5. Printing (PDF)
      1. The basics
      2. Adding printing to BigProject
    6. Summary
    7. Questions
    8. Further reading
  19. Section 4: Advanced Techniques and Best Practices
  20. Debugging, Logging, and Monitoring Qt Applications
    1. Technical requirements
    2. Testing Qt applications
      1. Code analysis
        1. Clang analysis
          1. Manually running Clang analysis
        2. Code reviews
      2. Active testing
        1. Writing good tests
        2. Hand testing
        3. Automated testing
          1. Preparing for testing
        4. Qt testing
          1. Basic tests
          2. Covering the possibilities
          3. Running the tests and viewing the results
        5. Google Test
      3. Checking code coverage
        1. The traditional way
      4. Testing doesn't have to be hard!
    3. Debugging Qt applications
      1. Old-school debugging
      2. Working with the Qt Creator debugger
        1. Examining threads
        2. Debugging and the target firewall
    4. Monitoring Qt applications
      1. Logging the Qt way
        1. Controlling the logging format
        2. Controlling the logging destination
      2. Using Valgrind
        1. Function profiling
        2. Analyzing for memory issues
    5. Summary
    6. Questions
  21. Responsive Application Programming - Threads
    1. Technical requirements
    2. Examining Qt's different threading models
      1. Simple threads
        1. Using QThread
        2. Running a QObject in its own QThread
      2. Thread pools
      3. Qt Concurrent
        1. Running concurrent functions
        2. Mapping, filtering, and reducing data
          1. Mapping a function onto data example
          2. Filtering and reducing data
        3. Concurrent examples
      4. QML WorkerScript
    3. Solving problems with threads
      1. Responsive GUIs
      2. Image processing
      3. Near real-time control
    4. Overcoming cross-thread communication and synchronization
      1. Synchronization
        1. Mutexes and semaphores
        2. Waiting with mutexes
      2. Protecting data
      3. Communicating between threads
      4. Being careful mixing threads and Qt GUI
    5. Summary
    6. Questions
    7. Further reading
  22. Qt Best Practices
    1. Technical requirements
    2. Understanding the Why? of Qt
    3. Choosing between C++ features and the Qt Way
    4. Increasing efficiency in Qt
      1. Don't make extra copies
        1. Passing parameters
        2. Hidden copies
      2. Keep it DRY
        1. Avoiding pointless initializations
        2. Avoiding useless work
        3. Using the best solution
      3. Stay in one place
      4. Letting Qt Creator help
    5. Summary
    6. Questions
    7. Further reading
  23. Appendix A: BigProject Requirements
  24. Appendix B: Bonus Code - Simplifying Q_PROPERTY
    1. Simplifying defining Q_PROPERTY()
    2. Simplifying working with Q_PROPERTY items
  25. Assessments
    1. Section 1: Getting Started with Embedded Qt
      1. Chapter 1 – Setting Up the Environment
      2. Chapter 2 – Writing Your First Qt Application
      3. Chapter 3 – Running Your First Application on Your Target
    2. Section 2: Working with Embedded Qt
      1. Chapter 4 – Important Qt Concepts
      2. Chapter 5 – Managing the Overall Workflow
      3. Chapter 6 – Exploring GUI Technologies
      4. Chapter 7 – Adding More Features
    3. Section 3: Deep Dive into Embedded Qt
      1. Chapter 8 – Qt in the Embedded World
      2. Chapter 9 – Exploring the IoT with Qt
      3. Chapter 10 – Using More Qt-Related Technologies
    4. Section 4: Advanced Techniques and Best Practices
      1. Chapter 11 – Debugging, Logging, and Monitoring Qt Applications
      2. Chapter 12 – Responsive Application Programming – Threads
      3. Chapter 13 – Qt Best Practices
  26. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Hands-On Embedded Programming with Qt
  • Author(s): John Werner
  • Release date: July 2019
  • Publisher(s): Packt Publishing
  • ISBN: 9781789952063