book (Practical recipes for building cross-platform GUI applications, widgets, and animat
ui-programming-cookbook-practical-recipes-for-building-cross-platform-gui-applications-w
This Book is Available on [Link]
book (Practical recipes for building cross-platform GUI applications, widgets, and animat
Qt5 C++ GUI Programming
Cookbook
ui-programming-cookbook-practical-recipes-for-building-cross-platform-gui-applications-w
Second Edition
Practical recipes for building cross-platform GUI applications,
widgets, and animations with Qt 5
Lee Zhi Eng
BIRMINGHAM - MUMBAI
This Book is Available on [Link]
book (Practical recipes for building cross-platform GUI applications, widgets, and animat
Qt5 C++ GUI Programming Cookbook
Second Edition
ui-programming-cookbook-practical-recipes-for-building-cross-platform-gui-applications-w
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: Alok Dhuri
Content Development Editor: Akshita Billava
Technical Editor: Abin Sebastian
Copy Editor: Safis Editing
Project Coordinator: Ulhas Kambali
Proofreader: Safis Editing
Indexer: Tejal Daruwale Soni
Graphics: Tom Scaria
Production Coordinator: Deepika Naik
First published: July 2016
Second edition: March 2019
Production reference: 1220319
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78980-382-2
[Link]
This Book is Available on [Link]
book (Practical recipes for building cross-platform GUI applications, widgets, and animat
ui-programming-cookbook-practical-recipes-for-building-cross-platform-gui-applications-w
[Link]
Mapt is an online digital library that gives you full access to over 5,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
Mapt is fully searchable
Copy and paste, print, and bookmark content
[Link]
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 [Link] and as a print
book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
customercare@[Link] for more details.
At [Link], 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.
This Book is Available on [Link]
book (Practical recipes for building cross-platform GUI applications, widgets, and animat
Contributors
ui-programming-cookbook-practical-recipes-for-building-cross-platform-gui-applications-w
About the author
Lee Zhi Eng is a self-taught programmer who worked as an artist and programmer at
several game studios before becoming a part-time lecturer for two years at a university,
teaching game development subjects related to Unity and Unreal Engine. He has not only
taken part in various projects related to games, interactive apps, and virtual reality, but has
also participated in multiple projects that are more oriented toward software and system
development. When he is not writing code, he enjoys traveling, photography, and
exploring new technologies.
This Book is Available on [Link]
book (Practical recipes for building cross-platform GUI applications, widgets, and animat
About the reviewer
Nibedit Dey is a technopreneur with a multidisciplinary technology background. He holds
a bachelor's degree in biomedical engineering and a master's degree in digital design and
ui-programming-cookbook-practical-recipes-for-building-cross-platform-gui-applications-w
embedded systems. Before starting his entrepreneurial journey, he worked for L&T and
Tektronix for several years in different research and development roles. He has been using
C++ to build complex software-based systems for the last eight years.
Packt is searching for authors like you
If you're interested in becoming an author for Packt, please visit [Link]
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.
This Book is Available on [Link]
book (Practical recipes for building cross-platform GUI applications, widgets, and animat
Table of Contents
ui-programming-cookbook-practical-recipes-for-building-cross-platform-gui-applications-w
Preface 1
Chapter 1: Look-and-Feel Customization with Qt Designer 7
Introduction 7
Technical requirements 7
Using style sheets with Qt Designer 8
How to do it… 8
How it works... 10
There's more… 12
Customizing basic style sheets 13
How to do it… 13
How it works... 18
Creating a login screen using style sheets 18
How to do it… 18
How it works... 27
Using resources in style sheets 27
How to do it 27
How it works... 32
Customizing properties and sub-controls 32
How to do it… 32
How it works... 36
Styling in Qt Modeling Language (QML) 37
How to do it… 38
How it works... 47
Exposing the QML object pointer to C++ 48
How to do it… 48
How it works... 51
Chapter 2: Event Handling - Signals and Slots 52
Introduction 52
Technical requirements 53
New signals and slots syntax 53
How to do it... 53
How it works... 59
UI events with signals and slots 60
How to do it... 60
How it works... 67
Asynchronous programming made easier 67
How to do it... 67
How it works... 70
This Book is Available on [Link]
book (Practical recipes for building cross-platform GUI applications, widgets, and animat
Table of Contents
Function callbacks 71
How to do it... 71
How it works... 73
Chapter 3: States and Animations with Qt and QML 74
ui-programming-cookbook-practical-recipes-for-building-cross-platform-gui-applications-w
Introduction 74
Technical requirements 74
Property animation in Qt 75
How to do it… 75
How it works... 76
There's more… 77
Using easing curves to control property animation 78
How to do it… 78
How it works... 79
There's more… 80
Creating an animation group 81
How to do it… 81
How it works... 82
There's more… 83
Creating a nested animation group 83
How to do it… 84
How it works... 86
State machines in Qt 86
How to do it… 87
How it works... 89
There's more… 90
States, transitions, and animations in QML 90
How to do it… 91
How it works... 94
There's more… 94
Animating widget properties using animators 95
How to do it… 95
How it works... 96
Sprite animation 97
How to do it… 97
How it works... 100
There's more… 101
Chapter 4: QPainter and 2D Graphics 102
Introduction 102
Technical requirements 103
Drawing basic shapes onscreen 103
How to do it… 103
How it works... 106
There's more… 107
[ ii ]
This Book is Available on [Link]
book (Practical recipes for building cross-platform GUI applications, widgets, and animat
Table of Contents
Exporting shapes to SVG files 107
How to do it… 107
How it works... 112
There's more… 113
Coordinate transformation 113
ui-programming-cookbook-practical-recipes-for-building-cross-platform-gui-applications-w
How to do it… 113
How it works... 116
There's more… 117
Displaying images onscreen 119
How to do it… 119
How it works... 121
There's more… 122
Applying image effects to graphics 124
How to do it… 124
How it works... 127
There's more… 128
Creating a basic paint program 128
How to do it… 129
How it works... 134
Rendering a 2D canvas in QML 135
How to do it… 135
Chapter 5: OpenGL Implementation 140
Introduction 140
Technical requirements 141
Setting up OpenGL in Qt 141
How to do it… 141
How it works... 142
There's more… 143
Hello World! 143
How to do it… 144
How it works... 148
Rendering 2D shapes 149
How to do it… 149
How it works... 152
Rendering 3D shapes 153
How to do it... 153
How it works... 157
Texturing in OpenGL 158
How to do it… 158
How it works... 161
Basic lighting in OpenGL 161
How to do it… 162
How it works... 165
Moving an object using keyboard controls 165
[ iii ]
This Book is Available on [Link]
book (Practical recipes for building cross-platform GUI applications, widgets, and animat
Table of Contents
How to do it… 165
How it works... 167
3D canvas in QML 167
How to do it… 167
How it works... 172
ui-programming-cookbook-practical-recipes-for-building-cross-platform-gui-applications-w
Chapter 6: Using Network and Managing Large Documents 173
Introduction 173
Technical requirements 173
Creating a TCP server 174
How to do it… 174
How it works... 180
Creating a TCP client 181
How to do it... 182
How it works... 186
Uploading and downloading files using FTP 186
How to do it... 186
How it works... 199
Chapter 7: Threading Basics - Asynchronous Programming 200
Introduction 200
Technical requirements 200
Using threads 201
How to do it… 201
How it works... 204
QObject and QThread 204
How to do it… 205
How it works... 209
Data protection and sharing data between threads 209
How to do it… 210
How it works... 213
Working with QRunnable processes 214
How to do it… 214
How it works... 216
Chapter 8: Building a Touch Screen Application with Qt5 217
Introduction 217
Technical requirements 218
Setting up Qt for mobile applications 218
How to do it… 218
How it works… 224
Designing a basic user interface with QML 225
How to do it… 225
How it works… 231
Touch events 233
How to do it… 233
[ iv ]
This Book is Available on [Link]