Master application development by writing succinct, robust, and reusable code with Qt 5
Guillaume Lazar . Robin Penea
Book Details
Price
|
3.00 |
---|---|
Pages
| 514 p |
File Size
|
12,085 KB |
File Type
|
PDF format |
ISBN
| 978-1-78646-712-6 |
Copyright©
| 2016 Packt Publishing |
Guillaume Lazar is a software engineer living near Paris in France. He has worked for
different companies, from startups to multinationals, for the last 7 years. He took the
opportunity to observe and learn from many team organizations and technologies.
He eventually founded his own software development company, NeuronalMotion, at the
age of 27 with his friend Robin Penea in 2014. Information about it and its open source
projects can be found on the comapny's website, www.neuronalmotion.com.
NeuronalMotion represents to Guillaume "a different way of working." The current
hierarchical organization that applies to most companies seems obsolete to him. With his
own company, he wants to try something different, where each employee will be autonomous.
Although he defines himself as a Qt framework lover, he likes to mix different technologies
and platforms. He also spends time on game development, machine learning, and
electronics, because "things" become "alive."
Creating examples and writing chapters for this book was a serious challenge this year.
Some sections were fundamentally changed to match Qt evolution. I would not have been
able to achieve this task alone. I wish to thank Robin Penea, my friend and the coauthor of
this book. He gave his all to create with me a robust Qt book with modern C++. Our review
team, Rohit Kumar Singh, Ray Rischpater, Quentin Canu, Chistophe Dongieux, and
Hugo Loi, have done excellent work: they corrected us and raised some important issues. I
also want to thank Packt for the good follow up during the writing process. Finally, I thank
my family and, particularly, Sophie, my girlfriend, for her support.
Robin Penea is a software engineer living in France. He has worked for several companies
over the last 7 years, on many different technologies (ranging from server application
development to embedded programming).
He founded NeuronalMotion with Guillaume Lazar in 2014 to implement his vision of
entrepreneurship and project development. He aims to achieve a self-organized company.
More information is available at www.neuronalmotion.com.
Besides developing with Qt, he likes to tinker with the Android AOSP framework, learn
new programming language paradigms, and discover how to interweave technology and real life.
This book would not have existed without Guillaume Lazar, my friend and the coauthor of
the book. He was truly dedicated to designing and crafting the best possible book. I wish to
thank our reviewers, Rohit Kumar Singh, Ray Rischpater, Quentin Canu, Christophe
Dongieux, and Hugo Loi. Their input was invaluable and definitely raised the quality of
the book. Packt played a sensible role, listening to our requests and changes, guiding us
through the writing, and monitoring our mental states during this year with benevolence.
I also wish to thank my father, Calin, for believing in me. A last word for Flore, my
girlfriend, who kept my motivation up and supported me.
About the Reviewer
Ray Rischpater is a software engineer and author with over 20 years of industry experience
and sixteen books to his credit. Beginning with software development for mobile platforms,
he’s done work in several areas, including mobile, web services, building tools for mapping
and geospatial engineering, and applying machine learning to geospatial processing problems.
During this time, he’s participated in the development of Internet technologies and custom
applications for Newton, Magic Cap, Palm OS, Java ME, Qualcomm BREW, Apple iOS, and
Google Android, as well as several proprietary platforms. Over the last six years, his focus
has shifted from mobile applications and services to building tools, software processes, and
machine learning models to facilitate mapping the real world. Presently, he’s employed as a
software engineering manager at Uber, doing work on mapping and machine learning.
When not writing for or about software development, he enjoys making music, hiking, and
photography with his family and friends in and around the San Lorenzo Valley in central
California. When he’s able, he also provides public service through amateur radio as the
licensed Amateur Extra station KF6GPE.
He is the author of Microsoft Mapping with Carmen Au, by Apress (published in 2013)
and Application Development with Qt Creator, Second Edition, by Packt (published in 2014). He
has written several other books, including Beginning Java ME Platform (Beginning from
Novice to Professional), by Apress (published in 2008), Software Development for the
QUALCOMM BREW Platform, by Apress (published in 2003), Wireless Web Development, First
Edition, by Apress (published in 2002), and Wireless Web Development, Second Edition, by
Apress (published in 2004).
I’d like to thank my wife, Meg Given, for her patience as I worked on yet another book project.
Preface
C++ is a powerful language. Coupled with Qt, you have in your hands a cross-platform
framework that allies performance and ease of use. Qt is a vast framework that provides
tools in many areas (GUI, threads, networking, and so on). 25 years after its inception, Qt
continues to evolve and grow with each release.
This book aims to teach you how to squeeze the best out of Qt with the new C++14
additions (lambdas, smart pointers, enum classes, and so on). These two technologies
together bring you a safe and powerful development toolbox. Throughout the book, we try
to emphasize a clean architecture that lets you create and maintain your application in a
complex environment.
Each chapter is based on an example project that is the basis of all the discussion. Here are
some tasters about what we will see in this book:
Uncover qmake secrets
Take a deep dive in the model/view architecture and study how you can build a
complex application with this pattern
Study QML and Qt Quick applications in mobile
Develop Qt 3D components using QML and JavaScript
Show how to develop plugins and SDKs using Qt
Cover the multi-threading technologies provided by Qt
Build an IPC mechanism using sockets
Serialize data using XML, JSON, and binary format
We'll cover all this and much, much more.
Note that you can take a look at Chapter 14, Qt Hat Tips and Tricks, whenever you want if
you want to get some development candies and see some code snippets that might make
your development more pleasurable.
And most importantly, have fun writing Qt applications!
Table of Contents
Preface 1
Chapter 1: Get Your Qt Feet Wet 8
Creating a project 9
MainWindow structure 12
Qt Designer 15
Signals and slots 17
Custom QWidget 19
Adding a task 23
Using a QDialog 26
Distributing code responsibility 28
Emitting a custom signal using lambdas 29
Simplifying with the auto type and a range-based for loop 34
Summary 38
Chapter 2: Discovering QMake Secrets 39
Designing a cross-platform project 39
Adding the Windows implementation 42
Adding the Linux implementation 48
Adding the Mac OS implementation 53
Transforming SysInfo into a singleton 56
Exploring Qt Charts 58
CpuWidget using QCharts 60
Memory using Qcharts 64
The .pro file in depth 70
Under the hood of qmake 74
Beneath Q_OBJECT and signals/slots 77
Summary 80
Chapter 3: Dividing Your Project and Ruling Your Code 81
Designing a maintainable project 82
Defining data classes 86
Storing your data in a database 89
Protecting your code with a smart pointer 97
Implementing the model 104
Summary 117
Chapter 4: Conquering the Desktop UI 118
Creating a GUI linked to a core shared library 118
Listing your albums with AlbumListWidget 123
Creating a ThumbnailProxyModel 128
Displaying the selected album with AlbumWidget 132
Enhancing thumbnails with PictureDelegate 141
Displaying a picture with PictureWidget 145
Composing your Gallery app 147
Summary 151
Chapter 5: Dominating the Mobile UI 152
Starting with Qt Quick and QML 153
Checking your development environment 162
Creating a Qt Quick project 163
Preparing your Qt Quick gallery entry point 166
Displaying albums with ListView 169
Theming the application with a QML singleton 172
Loading a database on mobile 176
Creating a new album from a custom InputDialog 178
Loading images with an ImageProvider 182
Displaying thumbnails in a GridView 187
Swiping through full resolution pictures 191
Summary 194
Chapter 6: Even Qt Deserves a Slice of Raspberry Pi 195
Discovering Qt3D 196
Configuring Qt for your Raspberry Pi 201
Creating an entry point for your Qt3D code 206
Setting up the scene 210
Assembling your Qt3D entities 214
Preparing the board game 220
Crafting entities from the factory 222
Building a snake engine in JavaScript 225
Varying the HUD with QML states 233
Profiling your QML application 237
Summary 242
Chapter 7: Third-Party Libraries Without a Headache 243
Creating your Qt Designer plugin 244
Configuring the project for Windows 246
Configuring the project for Linux 248
Configuring the project for Mac 249
Implementing your OpenCV filters 250
Designing the UI with FilterWidget 254
Exposing your plugin to Qt Designer 259
Using your Qt Designer plugin 262
Building the image-filter application 265
Summary 273
Chapter 8: Animations - It's Alive, Alive! 274
Preparing an SDK 274
Creating your plugins 278
Loading your plugins dynamically 284
Using the plugins inside the application 288
Discovering the Animation Framework 292
Making your thumbnails jump 296
Fading the picture in 298
Flashing the thumbnail in a sequence 301
Summary 306
Chapter 9: Keeping Your Sanity with Multithreading 307
Discovering QThread 307
Flying over Qt multithreading technologies 313
Architecting the Mandelbrot project 317
Defining a Job class with QRunnable 319
Using QThreadPool in MandelbrotCalculator 325
Displaying the fractal with MandelbrotWidget 330
Summary 339
Chapter 10: Need IPC? Get Your Minions to Work 340
Architecturing an IPC project 348
Laying down the foundations with an SDK 352
Working with QDataStream and QTcpSocket 356
Interacting with sockets in the worker 363
Interacting with sockets from the application 373
Building your own QTcpServer 379
Summary 388
Chapter 11: Having Fun with Serialization 389
Architecting the drum machine project 389
Creating a drum track 392
Making your objects serializable with QVariant 399
Serializing objects in JSON format 405
Serializing objects in XML format 409
Serializing objects in binary format 417
Playing low latency sounds with QSoundEffect 418
Triggering a QButton with your keyboard 419
Bringing PlaybackWorker to life 421
Accepting mouse drag and drop events 424
Summary 427
Chapter 12: You Shall (Not) Pass with QTest 428
Discovering Qt Test 429
Executing your tests 435
Writing factorized tests with datasets 439
Benchmarking your code 444
Testing your GUI 447
Spying on your application with QSignalSpy 450
Summary 451
Chapter 13: All Packed and Ready to Deploy 452
Packaging your application 453
Packaging for Windows 454
Packaging for Linux with a distribution package 457
Packaging for Linux with AppImage 463
Packaging for Mac OS X 465
Packaging for Android 468
Packaging for iOS 474
Summary 475
Chapter 14: Qt Hat Tips and Tricks 476
Managing your workspace with sessions 477
Searching with the Locator 477
Increasing the compilation speed 479
Examining the memory with Qt Creator 479
Generating random numbers 483
Silencing unused variable warnings 483
Logging custom objects to QDebug 485
Improving log messages 486
Saving your logs to a file 488
Generating a command-line interface 489
Sending and receiving HTTP data 491
Summary 496
Index 497
What you need for this book
All the code in this book can be compiled and run from Qt Creator using Qt 5.7. You can do
it from your preferred OS: Windows, Linux, or Mac OS.
About the mobile-specific chapters, either an Android or an iOS device works, but it is not
mandatory (the simulator/emulator can be enough).
Chapter 6, Even Qt Deserves a Slice of Raspberry Pi, offers to build an application running on
a Raspberry Pi. Although it is more fun if we can do it with a real Raspberry Pi, it is not
necessary to have one to complete the chapter.
Who this book is for
This book will appeal to developers and programmers who would like to build GUI-based
application. You should be fluent with C++ and the object-oriented paradigm. Qt
knowledge is recommended, but is not necessary.