Professional applications never end up being professional because of some random circumstances. They are designed like this from the very beginning. Of course, it is easier said than done, but it's still quite easy if you already know the golden rule of how to create applications that can be easily extended, maintained, scaled, and customized. The golden rule here is just one simple concept, which fortunately Qt framework already has the means to implement, and that is building applications in a modular fashion. Note that modular in this sense doesn't just mean libraries or different source code modules, but modular in the sense that each of the responsibilities and capabilities of the application is created and built independently of the others. This is, in fact, exactly the way Qt and OpenCV themselves are created. An application that is modularized can be extended very easily, even by different developers from different backgrounds...
You're reading from Computer Vision with OpenCV 3 and Qt5
In Chapter 2, Creating Our First Qt and OpenCV Project, you learned how to create a simple Qt+OpenCV application called Hello_Qt_OpenCV
. This project included almost all of the basic features provided by Qt, although we didn't go into too much detail on how our project was built into an application with a user interface and an (almost acceptable) behavior. In this section, you will learn about what went on behind the scenes when we clicked on the Run
button. This will help us with a better understanding of the structure of a Qt project and what the purpose of each file in the project folder is. Let's start by opening the project folder and going through the few files one by one. So, we have the following in the Hello_Qt_OpenCV
folder:
Hello_Qt_OpenCV.pro Hello_Qt_OpenCV.pro.user main.cpp mainwindow.cpp mainwindow.h mainwindow.ui
The first file in the Hello_Qt_OpenCV.pro
list is basically the first file that is processed by Qt when our project...
Even though we assume that the reader of this book is not a Design Pattern Denier, it's still a very good idea to remind ourselves why design patterns exist and why a successful framework such as Qt makes extensive use of different design patterns. Well, first of all, a design pattern is just one of many solutions to a software development task and it is not the only solution; and in fact, most of the times it's not even the fastest solution. However, a design pattern is definitely the most structured way of solving a software development problem, and it helps make sure you use some predefined template-like structures for everything you add to your program.
Note
Design patterns have names applied to different kinds of problems such as creating objects, how they run, how they handle data, and so on. Eric Gamma, Richard Helm, Ralph E. Johnson, and John Vlissides (referred to as the Gang of Four) describe many of the most widely used design patterns in their book titled Design...
In the next sections, you will learn how to add styling and multi-language support to our applications, but, before that, we must be familiar with the Qt Resource System. Simply put, it is the means in Qt to add resource files such as fonts, icons, images, translation files, style sheet files, and so on into our applications (and libraries).
Qt supports management using the *.qrc
files (Resource Collection Files), which are simply XML files that include information about resource files that need to be included in our applications. Let's go through a simple example and include an icon in our Hello_Qt_openCV
application to better understand how the Qt Resource System works:
- Make sure you have the
Hello_Qt_OpenCV
project opened in Qt Creator. SelectFile
and thenNew File or Project
. In the new file window, make sure you selectQt
from the second list on the left and thenQt Resource File
. Consider the following screenshot:
- Click on the
Choose...
button, and in the next screen...
Qt supports styling in using the QStyle
class and Qt Style Sheets. QStyle
is the base class for all styles in Qt, and it encapsulates the styling of the Qt user interfaces. Covering the QStyle
class is out of the scope of this book, but it should still be noted that creating a sub-class of QStyle
and implementing different styling capabilities in it is ultimately the most powerful method of changing the look and feel of a Qt application. However, Qt also offers Style Sheets to style applications. Qt Style Sheets are almost identical in syntax to HTML CSS (Cascading Style Sheets), which is an inseparable part of styling in web pages.
Note
CSS is a styling language that can be used to define the way objects on a user interface look. In general, using CSS files helps separate the styling of web pages from the underlying implementation. Qt uses a very similar method in its Style Sheets to describe the way widgets look. If you are familiar with CSS files, then Qt Style Sheets...
In this section, you will learn how to create applications that multiple languages using the Qt framework. In fact, it all comes down to a single class that is extremely easy to use. The QTranslator
class is the main Qt class responsible for handling internationalization of output (displayed) text. You simply need to make sure of the following:
- Use a default language (English, for instance) while you are building your project. This means, simply use sentences and words in the default language for everything that is displayed.
- Make sure all literal sentences in your code, or to be specific, all literal sentences that need to be translated when a different language is selected are embraced in a
tr()
function.
Using plugins in an is one of the most powerful methods of extending an application, and many of the applications that people use in their daily lives benefit from the power of plugins. A plugin is simply a library (*.dll
on Windows, *.so
on Linux, and so on) that can be loaded and used at runtime to take care of a specific task, but of course, it cannot be executed like a standalone application, and it depends on the using it. We will also use plugins throughout this book to extend our computer vision application.
In this section, we will learn how to create an example application (called Image_Filter
) that simply loads and uses plugins in a specified folder on the computer. However, before that we will learn how to create a plugin in Qt that uses both Qt and OpenCV frameworks, since our plugin will most probably need to do some computer vision magic using the OpenCV library. So, let's start.
First things first, we need to define a set of interfaces that are needed...
Everything you learned in this was meant to make you ready to start building a comprehensive computer vision application that will do the following:
- Use plugins to extend its capabilities
- Use Qt style sheets to customize its look and feel
- Support multiple languages
So, starting from now we will create the foundations of our application by taking into consideration all the facts that you learned in this chapter and the previous chapters, such as the following:
- Our application will be able to save and load all user preferences and settings. We will make it happen by using the
QSettings
class, which you already learned how to use. - It's best to have a centralized and single Qt style sheet that takes care of our application's overall look and feel, and better yet, loaded from the disk instead of embedding it into the application itself.
Throughout your career as a developer or your studies and research, you will come across the word, sustainable a lot. This chapter's goal was to introduce you to the basic concepts of creating a sustainable application in general, and a computer vision application using Qt and OpenCV in particular. You are now familiar with creating plugins, which consequently means you can create an application that can be extended by reusable libraries created by third-party developers (or yourself, of course) and without the need to rebuild the core application. In this chapter, you also learned about customizing the look and feel of a Qt application and creating multi-language Qt applications.
It was a long, but hopefully rewarding chapter. If you have followed through every example and step-by-step instruction, then by now you should be familiar with some of the most crucial techniques of cross-platform application development using the Qt framework. In this chapter, you learned about styling...