Getting Started with Qt 5
eBook - ePub

Getting Started with Qt 5

Introduction to programming Qt 5 for cross-platform application development

Benjamin Baka

  1. 136 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Getting Started with Qt 5

Introduction to programming Qt 5 for cross-platform application development

Benjamin Baka

Book details
Book preview
Table of contents
Citations

About This Book

Begin writing graphical user interface(GUI) applications for building human machine interfaces with a clear understanding of key concepts of the Qt framework

Key Features

  • Learn how to write, assemble, and build Qt application from the command line
  • Understand key concepts like Signals and Slots in Qt
  • Best practices and effective techniques for designing graphical user interfaces using Qt 5

Book Description

Qt is a cross-platform application framework and widget toolkit that is used to create GUI applications that can run on different hardware and operating systems. The main aim of this book is to introduce Qt to the reader. Through the use of simple examples, we will walk you through building blocks without focusing too much on theory.

Qt is a popular tool that can be used for building a variety of applications, such as web browsers, media players such as VLC, and Adobe Photoshop. Following Qt installation and setup, the book dives straight into helping you create your first application.

You will be introduced to Widgets, Qt's interface building block, and the many varieties that are available for creating GUIs. Next, Qt's core concept of signals and slots are well illustrated with sufficient examples. The book further teaches you how to create custom widgets, signals and slots, and how to communicate useful information via dialog boxes. To cap everything off, you will be taken through writing applications that can connect to databases in order to persist data.

By the end of the book, you should be well equipped to start creating your own Qt applications and confident enough to pick up more advanced Qt techniques and materials to hone your skills.

What you will learn

  • Set up and configure your machine to begin developing Qt applications
  • Discover different widgets and layouts for constructing UIs
  • Understand the key concept of signals and slots
  • Understand how signals and slots help animate a GUI
  • Explore how to create customized widgets along with signals and slots
  • Understand how to subclass and create a custom windows application
  • Understand how to write applications that can talk to databases.

Who this book is for

Anyone trying to start development of graphical user interface application will find this book useful. One does not need prior exposure to other toolkits to understand this book. In order to learn from this book you should have basic knowledge of C++ and a good grasp of Object Oriented Programming. Familiarity with GNU/Linux will be very useful though it's not a mandatory skill.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Getting Started with Qt 5 an online PDF/ePUB?
Yes, you can access Getting Started with Qt 5 by Benjamin Baka in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in C++. We have over one million books available in our catalogue for you to explore.

Information

Year
2019
ISBN
9781789955125
Edition
1

Implementing Windows and Dialog

In the previous chapter, we learned how to animate our application by using signals and slots to trigger and respond to actions that occur within our application. So far, we have been concentrating on examples that are contained in only one file and do not expressly describe a full working application. To do so, we will need to change the style in which our applications are written, and also adopt a number of new conventions.
In this chapter, we shall work with Windows in Qt, so that by the end of the chapter, you should be able to do the following:
  • Understand how to subclass and create a custom window application
  • Add a menu bar to a window
  • Add a toolbar to a window
  • Use the various dialog (boxes) to communicate information to the user

Creating a custom window

To create a window(ed) application, we usually call the show() method on an instance of QWidget and that makes that widget, to be contained in a window of its own, along with its child widgets displayed in it.
A recap of such a simple application is as follows:
#include <QApplication>
#include <QMainWindow>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow mainWindow;
mainWindow.show();
return a.exec();
}
mainWindow here is an instance of QMainWindow, which is derived from QWidget. As such, by calling the show() method, a window will appear. If you were to replace QMainWindow with QLabel, this will still work.
But this style of writing applications is not the best. Instead, from this point onward, we shall define our own custom widget, in which we shall define child widgets and make connections between signals and sockets.
Now, let's rewrite the preceding application by sub-classing QMainWindow. We have chosen to subclass QMainWindow because we need to illustrate the menu and toolbars.
We start off by creating a new folder and defining a header file. The name of our header file here is mainwindow.h, but feel free to name it how you want and remember to add the .h suffix. This file should basically contain the following:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLabel>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow();
};
#endif
We include the Qt classes QMainWindow, and QLabel in our header file. Then, we subclass QMainWindow and call it MainWindow. The constructor of this new class is declared with the following:
public:
MainWindow();
The entire class definition is wrapped within an #ifndef ... #endif directive, which tells the preprocessor to ignore its content if it is accidentally included multiple times in a file.
It is possible to use the non-standard, but widely used, preprocessor directive, #pragma once.
Take notice of the Q_OBJECT macro. This is what makes the signals and slots mechanism possible. Remember that the C++ language does not know about the keywords used to set up signals and slots. By including this macro, it becomes part of the C++ syntax.
What we have defined so far is just the header file. The body of the main program has to live in some other .cpp file. For easy identification, we call it mainwindow.cpp. Create this file within the same folder and add the following lines of code:
#include "mainwindow.h"
MainWindow::MainWindow()
{
setWindowTitle("Main Window");
resize(400, 700);
QLabel *mainLabel = new QLabel("Main Widget");
setCentralWidget(mainLabel);
mainLabel->setAlignment(Qt::AlignCenter);
}
We include the header file that we defined earlier with the first line of code. The default constructor of our sub-classed widget, MainWindow, is defined.
Notice how we call the method that sets the title of the window. setWindowTitle() is invoked and can be accessed from within the constructor since it is an inherited method from QWindow. There is no need to use the this keyword. The size of the window is specified by calling the resize() method and passing two integer values to be used as the dimensions of the window.
An instance of a QLabel is created, mainLabel. The text within the label is aligned to the center by calling mainLabel->setAlignment(Qt::AlignCenter).
A call to setCentralWidget() is important as it situates any class that inherits from QWidget to occupy the interior of the window. Here, mainLabel is being passed to setCentralWidget, and that will make it the only widget to be displayed within the window.
Consider the structure of QMainWindow in the following diagram:
At the very top of every window is the Menu Bar. Elements such as the file, edit, and help menus go there. Below that, are the Toolbars. Contained within the Toolbars are the Dock Widgets, which are collapsible panels. Now, the main controls within the window must be put in the Central Widget location. Since a UI is made up of several widgets, it will be good to compose a widget that will contain child widgets. This parent widget is what you will stick into the Central Widget area. To do this, we call setCentralWidget() and pass in the parent widget. At the bottom of the window, is the Status Bar.
To run the application, we need to create an instance of our custom window class. Create a file called main.cpp within the same folder where the header and .cpp files are located. Add the following lines of code to main.cpp:
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow mainwindow;
mainwindow.show();
return app.exec();
}
We include the header file mainwindow.h, which contains the declaration of our custom class, MainWindow. Without this, the compiler wouldn't know where to find the definition of the MainWindow class.
An instance of MainWindow is created and the show() method is called on it. We still have to call the show() method on mainwindow. MainWindow, which is a subclass of QMainWindow, and behaves just like any widget out there. Furthermore, as we already know, to cause a widget to appear, you have to call the show() method on it.
To run the program, move into the folder via the command line and issue the following commands:
% qmake -project
Add QT += widgets to the .pro file that is generated. Now continue with the next set of commands:
% qmake
% make
Examine the .pro file for a second. At the very bottom of the file, we have the following lines:
HEADERS += mainwindow.h
SOURCES += main.cpp mainwindow.cpp
The headers are automatically collected and added to HEADERS. Similarly, the .cpp files are collected and added to SOURCES. Always remember to check this file when there are compilation errors to ensure that all required files have been added.
To run the program, issue the following command:
% ./classSimpleWindow
For those who work on th...

Table of contents

Citation styles for Getting Started with Qt 5

APA 6 Citation

Baka, B. (2019). Getting Started with Qt 5 (1st ed.). Packt Publishing. Retrieved from https://www.perlego.com/book/921339/getting-started-with-qt-5-introduction-to-programming-qt-5-for-crossplatform-application-development-pdf (Original work published 2019)

Chicago Citation

Baka, Benjamin. (2019) 2019. Getting Started with Qt 5. 1st ed. Packt Publishing. https://www.perlego.com/book/921339/getting-started-with-qt-5-introduction-to-programming-qt-5-for-crossplatform-application-development-pdf.

Harvard Citation

Baka, B. (2019) Getting Started with Qt 5. 1st edn. Packt Publishing. Available at: https://www.perlego.com/book/921339/getting-started-with-qt-5-introduction-to-programming-qt-5-for-crossplatform-application-development-pdf (Accessed: 14 October 2022).

MLA 7 Citation

Baka, Benjamin. Getting Started with Qt 5. 1st ed. Packt Publishing, 2019. Web. 14 Oct. 2022.