Hands-On Embedded Programming with Qt Develop high performance applications for embedded systems with C++ and Qt 5 John Werner BIRMINGHAM - MUMBAI Hands-On Embedded Programming with Qt Copyright © 2019 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Commissioning Editor: Richa Tripathi Acquisition Editor: Shriram Shekhar Content Development Editor: Tiksha Sarang Senior Editor: Afshaan Khan Technical Editor: Gaurav Gala Copy Editor: Safis Editing Project Coordinator: Prajakta Naik Proofreader: Safis Editing Indexer: Manju Arasan Production Designer: Arvindkumar Gupta First published: July 2019 Production reference: 1120719 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78995-206-3 www.packtpub.com Packt.com Subscribe to our online digital library for full access to over 7,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website. Why subscribe? Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals Improve your learning with Skill Plans built especially for you Get a free eBook or video every month Fully searchable for easy access to vital information Copy and paste, print, and bookmark content Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks. Contributors About the author John Werner is an internationally published author, engineer, consultant, and conference speaker with more than 15 years' experience. He has earned patents for inventions ranging from Inkjet printers to automotive ignition systems. John was one of the early users of Qt on QNX and contributed to the early knowledge of how to make it work. He is a contributor to the Qt-related information on Stack Exchange. He is currently a senior software engineer, specializing in Qt development, for Caliber Imaging & Diagnostics. I would like to acknowledge the following people and companies: Qt, for providing trial licenses of their embedded products My GoG family for their support and their endless humor The team at Caliber Imaging & Diagnostics, my employer, for their support and encouragement Nick at La Salon Bianca, for making me look good The editorial staff at Packt, for helping me walk through this process of writing my first book Most of all, my wife and children for their infinite understanding, patience, and support About the reviewer Pablo Rogina was born and lives in Buenos Aires, Argentina. He earned his bachelor's degree in computer science at Universidad de Buenos Aires (UBA) and holds a postgraduate degree in information security, also from UBA. He has more than 25 years' extensive experience and exposure to several different positions and responsibilities in IT environments, ranging from programming to network administration, and including system analysis and design, end user support, and database design and management. Over the last few years, he has been focusing on embedded system development, with specific exposure to IoT and computer vision solutions. Packt is searching for authors like you If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea. Table of Contents Preface 1 Section 1: Getting Started with Embedded Qt Chapter 1: Setting Up the Environment 9 Technical requirements 9 Creating our embedded environment 10 Obtaining the hardware 11 Loading the firmware 11 Backing up the SD card 12 Identifying the SD card device 12 Grabbing the image 14 Burning a new image 14 Connections and first boot 14 Network configuration 15 Installing RSync 16 Preparing the host machine 17 Installing Qt 17 Setting up the cross compilation environment 19 Installing the cross compilation tools 19 Preparing for debugging 20 Accessing the Target's root directory on the host 20 Building Qt for the Target 21 Fixing the sources 22 Building a program 22 Configuring the Qt build 22 Decoding the configure command 23 Building the code 24 Synchronizing the new components with the target 25 Running a quick test to ensure how the program works 25 Summary 26 Questions 27 Further reading 27 Chapter 2: Writing Your First Qt Application 28 Technical requirements 29 Introducing Qt Creator 29 Starting Qt Creator 30 Qt Creator welcomes you 30 The Examples screen 31 The Tutorials screen 32 Table of Contents The Projects screen 33 The left icon bar 34 Greetings from Qt 35 Creating a project 35 Walking through the New File or Project wizard 36 Choosing a template 37 Picking a name and location 38 Selecting a kit 38 Defining the main window class 39 Selecting a source control system for your project 40 Examining the Edit screen 42 Viewing projects and open files 42 A place for editing 43 Presenting outputs 43 Designing the UI 44 Placing a text label 45 Running the code 46 Protecting your work 47 Who are you? 48 Adding a place to type your name 48 Causing a response 49 Fixing a mistake 51 Launching our application in the Qt Creator debugger 52 Examining the Debug screen 52 Setting a breakpoint 53 Tripping the breakpoint 53 Getting some help 56 Digging deeper 57 Explaining the push button code 57 Sending and receiving between objects–signals and slots 59 Cool things about signals and slots 59 Rules of signals and slots 60 Testing 60 Summary 61 Questions 62 Further reading 62 Chapter 3: Running Your First Application on the Target 63 Technical requirements 63 Configuring Qt Creator for our Target 64 Telling Qt Creator about a new device 64 Telling Qt Creator about the cross-compiler 67 Configuring the debugger for the Target 69 Letting Qt Creator know about our Raspberry Pi Qt build 70 Creating a Qt kit 72 Building for the Target 72 Adding a kit to the project 73 [ ii ] Table of Contents Building the application 74 Deploying on the Target 74 Running the application on the Target 75 Making a more flexible display using layouts 76 Debugging on the Target 79 Troubleshooting debugging 79 Summary 80 Questions 81 Section 2: Working with Embedded Qt Chapter 4: Important Qt Concepts 83 Technical requirements 83 Communicating effectively – signals and slots 84 Quick info – Signals and slots 84 Signals and slots in non-GUI code 85 Preparing the header 87 Using the QTimer 89 Setting up the timer 89 Connecting the QTimer signal 89 Finishing the code 90 Running our application 90 Signals and slots using lambdas 91 Queuing things up 93 When should I use a queued connection? 95 Implementing Signals Not Needed 96 Looking inside – Qt's introspection support 97 Properties 97 Q_PROPERTY 99 QVariant 100 Enums 102 Differing views — model/view architecture 103 A quick dive 104 Formalizing Qt's model/view 105 Customizing how model data is viewed 105 Filtering and sorting what's viewed 105 Keeping it portable – Qt's platform abstraction 106 Qt's OS and filesystem abstractions 107 Qt's container class abstractions 108 Taking a closer look at building and Main 108 Qt Creator's supported build systems 109 QMake 109 Qbs 109 CMake 109 What's all that in main(..)? 110 Summary 110 [ iii ] Table of Contents Questions 111 Chapter 5: Managing the Overall Workflow 112 Technical requirements 112 Modernizing software development 113 Falling down the development cycle 113 Continuous Integration 114 Unit testing 114 Modern development cycle 115 Moving ahead without hardware 115 Layering the problem 115 Starting our BigProject 116 BigProject customer requirements 116 Starting the Qt project 117 Initial main window design 117 Wiring up the time display 118 Testing 119 Commit it 120 Mocking the hardware 120 A new requirement 120 Defining the interface 121 Implementing the mock 122 Injecting the mock 123 Wiring up the temperature display 124 Test it! 124 Summary 125 Questions 126 Further reading 126 Chapter 6: Exploring GUI Technologies 128 Technical requirements 128 Two roads diverge 129 Qt Widgets 129 Programming widgets 130 Creating a widget with Qt Creator/Designer 130 Creating a widget by hand 135 Doing something new – QML 136 What is QML? 137 Developing a UI in QML 138 Revisiting some old friends 138 Hello from Qt! 138 Hello from QML! 139 Creating the project 139 Defining the window 140 Customizing the display 143 Using the Designer 144 The Form Editor 145 [ iv ]