Book Image

Qt 5 Projects

By : Marco Piccolino
Book Image

Qt 5 Projects

By: Marco Piccolino

Overview of this book

<p>Qt is a professional cross-platform application framework used across industries like automotive, medical, infotainment, wearables, and more. In this book you’ll initially create a to-do style app by going via all stages for building a successful project. You'll learn basics of Qt's C++ and QML APIs, test-driven development with Qt Test, application architecture, and UIs with Qt Quick &amp; Quick Controls 2.</p> <p>Next, you’ll help two startups build their products. The first startup, Cute Comics, wants to help independent comic creators with a suite of apps that let them experiment with comic pages, image composition, comic dialogues, and scene descriptions.&nbsp; While developing these apps you’ll deepen your knowledge of Qt Quick's layout systems, and see Qt 3D and Widgets in action.</p> <p>The second startup, Cute Measures, wants to create apps for industrial and agricultural sectors, to make sense of sensor data via a monitoring system. The apps should run seamlessly across devices and operating systems like Android, iOS, Windows, or Mac, and be cost-effective by integrating with existing web technologies. You take the role of lead developer and prototype the monitoring system. In doing so you’ll get to know Qt's Bluetooth and HTTP APIs, as well as the Charts and Web Engine UI modules.</p> <p>These projects will help you gain a holistic view of the Qt framework.</p>
Table of Contents (16 chapters)
Title Page
PacktPub.com
Contributors
Preface
Index

Preface

"With great power, there must also come -- great responsibility!"

– A friend of Spider-Man

The projects I have for you are not just meant to show you how powerful and easy it is to build complete applications and rich user interfaces with Qt (you could find out that on your own, I assume you are smart), but first and foremost, they are meant to show you how to do that by writing maintainable code that you'll want to touch and expand upon without regret any time you feel like it. And that requires a little experience. In the end, that's what I am offering you above anything else: my experience as a software developer who uses Qt daily to fulfill my customers' and my own needs.

Every experienced storyteller will tell you that we humans pass along stories because they help us survive. That's what I am going to do in this book, too. I'll tell you the story of two start-ups that need you to help them develop their products. I am confident that you'll make it through and complete the projects because you have a clear purpose, and because this book will provide you with the roadmaps to get there.

Whether you are planning a consumer-oriented digital product, or a product ecosystem intended for businesses, you'll find out which Qt building blocks are best suited for your endeavor, and which Qt application programming interfaces (APIs) you could use to achieve your goals in a time-efficient and future-proof manner.

In every project, we will start with the user's needs by drawing knowledge from the principles of behavior-driven development, test-driven development, clean coding and clean crchitecture. You might at first find this kind of approach daunting and verbose. However  I promise, the experience will be increasingly rewarding, and the process justified. To make sure you put your best foot forward, make sure you read the following sections. Have a good journey!

Who this book is for

This book is meant for developers who want to design feature-rich, customer and business-oriented applications and dynamic graphical user interfaces, and deploy them seamlessly to embedded, mobile, server, and desktop devices. Furthermore, it is meant for developers who want to be able to grow and maintain those applications without getting frustrated or outright mad because of unnecessary code complexity.

The book is goal and project-oriented, and requires some working experience with C++ 11 and JavaScript programming, plus the willingness to explore and embrace test-driven development and current best practices in software design and implementation.

What this book covers

Chapter 1, Writing Acceptance Tests and Building a Visual Prototype, describes a simple personal app project (What's in my fridge?) both in terms of the problems it tries to solve and the solutions that an application helps come up with. We write acceptance tests with Qt Test to ensure that the main usage scenarios are met. We introduce the Qt Quick Designer and Qt Quick Controls as an effective means of prototyping and implementing user interfaces.

Chapter 2, Defining a Solid and Testable App Core, shows how the powerful Qt object model (especially signals, slots, and properties) makes it easy to design the application's architecture by means of well-defined layers, and also makes it easy to write terse and reusable code. In this chapter, we implement the scenarios and business objects that we came up with in the previous chapter, and learn about the relative merits of Qt's QML and C++ APIs.

Chapter 3, Wiring User Interaction and Delivering the Final App, shows how to add the UI on top of the business logic implemented in the previous chapter. We show how easy it is to change UI technology depending on the target platform without altering the underlying logic. We also introduce platform-specific guidelines to deploy the app on common desktop and mobile systems.

Chapter 4, Learning About Laying Out Components by Making a Page Layout Tool, uncovers the flexibility of the Qt Quick framework by building an app that makes extensive use of different and dynamic item positioning methods. We explore the Qt Quick module, discover the QML Camera API, and learn how to load images from the filesystem into the UI.

Chapter 5, Creating a Scene Composer to Explore 3D Capabilities, dives into Qt 3D, one of the latest Qt UI APIs. We build a UI that mixes 2D and 3D scenes, and set up a 3D scene whose parameters can be manipulated via the 2D UI controls. We see how to add and remove 3D models from the scene, and save a picture from the current scene with just a few lines of code.

Chapter 6, Building an Entity-Aware Text Editor for Writing Dialogues, is centered on building a productivity-focused app thanks to Qt Widgets, a set of mature, desktop-oriented UI components that cover a wide range of needs. We write a specialized text editor with custom highlighting, whose contents can be modified by both typing and widget controls. We then export the formatted text to PDF. In this chapter we also introduce Qt's model/view paradigm.

Chapter 7, Sending Sensor Readings to a Device with a Non-UI App, explores how to create a command-line based application that gathers generic sensor data and makes it available to other devices via a device-to-device Bluetooth connection.

Chapter 8, Building a Mobile Dashboard to Display Real-Time Sensor Data, unravels how to develop an application that receives the sensor readings transmitted in the previous chapters, and displays them in nice-looking charts by implementing a QML UI with Qt Charts.

Chapter 9, Running a Web Service and an HTML5 Dashboard, shows how to create an application that generates fake sensor data over time and exposes it via a REST web service. We develop an extended version of the app from the previous chapter, by using Qt WebEngine to display the content via web sockets. We leverage one of the existing JavaScript chart libraries to display the data, to make sure that the same HTML5 UI could be in future served to a standard web browser.

Appendix, Additional and Upcoming Qt Features, briefly introduces additional important Qt features that were not mentioned in the projects, as well as recent and upcoming features that were introduced after Qt 5.9 Long Term Support.

To get the most out of this book

In this book, I assume you already have a basic understanding of common data structures and algorithms. You should also be familiar with the principles of object-oriented programming (OOP). A working knowledge of C++11 is required for all projects. JavaScript 5 is required for the projects involving the QML language.

This book does not explicitly cover all the steps required for having a Qt distribution and the Qt Creator IDE up and running. You will find many resources covering this kind of information in the official Qt documentation, in other Packt titles, and in many tutorials available online. I will just show you where to download Qt at the right time.

The book is based on Qt 5.9 Long Term Support as this is, at the time of writing, the version that is supported for the longest time, as its name suggests. However, any later minor version (5.x) will be compatible with the code shown here.

If you ever need help or get stuck, which sooner or later will happen despite all efforts I took to provide you with everything you need, remember that one of Qt's best features is its community. Here are the resources I think you should familiarize yourself with before starting with the book:

When it comes to additional sources of information, here are a few recommendations:

  • Qt's official documentation (http://doc.qt.io), a very comprehensive source of information about Qt APIs and general concepts. At times it might be hard to find what you are looking for — if you are in doubt, ask on one of the channels from the previous list. The documentation is also integrated into Qt Creator, and is also available for documentation browsers such as Dash and Zeal.
  • Qt's examples, available with your distribution and accessible from Qt Creator.
  • The training videos (https://www.qt.io/qt-training-materials/) and self-study pack (https://www.qt.io/qt-training-materials/) at qt.io.
  • The Qt Company's blog (http://blog.qt.io/), the main source for announcements of Qt releases and upcoming webinars, also offering also technology-related posts on current or upcoming features, and HowTos.
  • KDAB's blogs (https://www.kdab.com/category/blogs/), a wealth of articles about various topics, including data structures, OpenGL, Qt 3D, Qt for Android, and more. KDAB is one of Qt's main contributors and a consultancy firm.
  • ICS's blog (https://www.ics.com/blog) and webinars, covering various topics, including Qt basics. ICS is Qt consultancy firm particularly active in North America.
  • The QML Book (https://qmlbook.github.io/), an extensive free resource covering QML and Qt Quick.

Download the example code files

You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at www.packtpub.com
  2. Select the SUPPORT tab
  3. Click on Code Downloads & Errata
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Qt-5-Projects. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here:

https://www.packtpub.com/sites/default/files/downloads/Qt5Projects_ColorImages.pdf.

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Each project structure is described in a .pro file."

A block of code is set as follows:

# part1-whats_in_my_fridge.pro
TEMPLATE = subdirs

SUBDIRS += usecases

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

TEMPLATE = subdirs

SUBDIRS += \
#    usecases \
    gui

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "occupy all the available space by going to the Layout tab on the right (Properties pane) and selecting Anchors on all four sides."

Note

Warnings or important notes appear like this.

Note

Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packtpub.com.