Application Programming Using C++ And Qt

Introduction

You may have used various kinds of software, like antiviruses, Adobe Photoshop, AMD's software, virtual machines, OpenOffice, VLC, or other software, like screen/webcam recorders, 3D graphics software or downloaders like Torrentz or uploaders like FileZilla etc. They all use the same application framework.

Qt is a cross-platform GUI application framework that is used for developing application software that can be run on various software and hardware platforms with little or no changes in the underlying code.

Qt is currently being developed both by the Qt Company, a publicly listed company, and the Qt Project under open-source governance, involving individual developers and firms working to advance Qt.

Qt is available as both proprietary and open source.

Here's the list of applications that use Qt,
  • https://en.wikipedia.org/wiki/Category:Software_that_uses_Qt
  • https://wiki.manjaro.org/index.php?title=List_of_Qt_Applications
OK, enough talking. Let's start.

First, download the Qt source for Visual Studio (Microsoft Visual C++ (msvc)).
  • https://download.qt.io/archive/qt/
 Remember, you have to download the specific version of Qt for Visual Studio.

I am using Visual Studio 2013 Professional version, so I have downloaded qt-opensource-windows-x86-msvc2013_64-5.5.1.exe file.

The 5.5 version is here: https://download.qt.io/archive/qt/5.5/5.5.0/

If you are using Visual Studio version 2015, download MSVC 2015 here: 
https://download.qt.io/archive/qt/5.8/5.8.0/ 
 
Or download the latest version if you are using the latest version of Visual Studio.

Once you download it, start the setup.

It has the following screens.
 
Click "Next".
 
Skip this step.
 
Select the folder where you want to install it and click "Next". 
 
 
Select the components that you want to install, make sure the MSVC options is selected. If you need the sources, then also, select that option.
 
Click "Next".
 
 
 
Drink a cup of coffee until it finishes.

Once the installation is completed, then open the Visual Studio.

Select Tools -> Extensions and Updates option and search Qt in the search box.

Download and install Qt Visual Studio Tools.
 
 
 
Once it installed, restart the Visual Studio.

You can see one option added on menubar, QT VS TOOLS.
 
 
 
Now, select QT VS Tools -> Qt option.

It will pop up a dialog.
 
 
 
Click on the "Add" button.
 
 
 
Enter the version of Qt you have just installed and select the installation msvc folder.
 
 
 
Everything's done, now go to File -> New -> Project.

Drop the Visual C++ list, and select Qt and Qt GUI application.

Enter project name as Qt_Demo and click OK.
 
 
It will start an app wizard.

 
 
Click "Next".

 

Select the components that you are using in your Qt application.

For now, keep it as it is and click "Next".

 
Select the filename and click "Finish".

Once the solution is created, it will create many files in Solution Explorer.
 
 
 
But we don't need all those for now. So, delete the following files.
  • Qt_Demo.ui
  • Qt_Demo.h
  • Qt_Demo.qrc
  • Qt_Demo.cpp
  • Form Files folder
Now, we have only one file main.cpp

Copy and paste the following code to the editor and replace it.
  1. #include<qapplication.h>  
  2. #include<qwidget.h>  
  3.   
  4. int main(int argc, char**argv)  
  5. {  
  6.     //declare qt main application class with arguments  
  7.     QApplication qt_app(argc, argv);  
  8.   
  9.     //create a window widget object  
  10.     QWidget *window = new QWidget();  
  11.   
  12.     //declare a size object for size of window  
  13.     QSize window_size;  
  14.     window_size.setWidth(600);  
  15.     window_size.setHeight(400);  
  16.   
  17.     //declare a point object for location of window  
  18.     QPoint window_location;  
  19.     window_location.setX(100);  
  20.     window_location.setY(80);  
  21.   
  22.     //set properties to window  
  23.     window->resize(window_size);  
  24.     window->move(window_location);  
  25.     window->setWindowTitle("Qt Demo");  
  26.   
  27.     //display the window  
  28.     window->show();  
  29.       
  30.     //return the executable result  
  31.     return qt_app.exec();  
  32. }  
If you look at the code, it is the same as Java/C#.

Instead, it is written in C++, you have to use a pointer to allocate a memory to them.

The above program creates a simple window by creating an object (line 129) with title "Qt Demo" (line 144), window size 600*400 (line 132-134,142), window location (line 137-139, 143) and finally showing that window.

A QWidget is a class that can be anything such as QMainWindow or others controls which inherit it.

First we need to create an application. This can be created using QApplication class by passing default command line arguments parameters of the main function to its constructor.

And then returning the result when the application is executed(exec()).

Now debug the project. It must show a window.

The application depends on three dynamic link libraries that Qt provides,
  • Qt5Cored.dll
  • Qt5Guid.dll
  • Qt5Widgetsd.dll
If you dont want your application to be dependant on dll's then you need to build your application using static Qt libraries. For that download the source code of Qt, compile it using MinGW compiler and use those libraries in your application.

In Qt, every control can be shown with a default widget. Let's create a simple Labels and Buttons demo.
  1. #include<qapplication.h>  
  2. #include<qwidget.h>  
  3. #include<qlabel.h>  
  4. #include<qpushbutton.h>  
  5.   
  6. class LocPoint{  
  7.     QPoint location;  
  8. public:  
  9.     LocPoint(int x, int y){  
  10.         location.setX(x);  
  11.         location.setY(y);  
  12.     }  
  13.     QPoint getLoc() const{  
  14.         return location;  
  15.     }  
  16. };  
  17.   
  18. class Size{  
  19.     QSize size;  
  20. public:  
  21.     Size(int w, int h){  
  22.         size.setWidth(w);  
  23.         size.setHeight(h);  
  24.     }  
  25.     QSize getSize() const{  
  26.         return size;  
  27.     }  
  28. };  
  29.   
  30. int main(int argc, char *argv[])  
  31. {  
  32.     //declare qt main application class with arguments  
  33.     QApplication qt_app(argc, argv);  
  34.   
  35.     QSize size;  
  36.       
  37.   
  38.     QString label_text = "Hello World";  
  39.     QString html_tags_label_text = "<h1><font color='#ed2332'>Hello World</font></h1>";  
  40.   
  41.     //create a label object and show  
  42.     QLabel *label_1 = new QLabel();  
  43.     label_1->setText(label_text);  
  44.     label_1->move((new LocPoint(300, 80))->getLoc());  
  45.     label_1->resize((new Size(250, 60))->getSize());  
  46.     label_1->show();  
  47.   
  48.     QLabel *label_2 = new QLabel();  
  49.     label_2->setText(html_tags_label_text);  
  50.     label_2->move((new LocPoint(370+label_1->width(),80))->getLoc());  
  51.     label_2->resize((new Size(250, 60))->getSize());  
  52.     label_2->show();  
  53.   
  54.     //create a button object and show  
  55.     QPushButton *button_1 = new QPushButton();  
  56.     button_1->setText("Button 1");  
  57.     button_1->move((new LocPoint(300, 120+label_1->height()))->getLoc());  
  58.     button_1->resize((new Size(250, 60))->getSize());  
  59.     button_1->show();  
  60.   
  61.     //set colors  
  62.     QColor back_color;  
  63.     back_color.setRgb(80, 120, 220);  
  64.   
  65.     //define color paletts for foreground and background  
  66.     QPalette back_palet,fore_palet;  
  67.     back_palet.setColor(QPalette::ColorGroup::Active, QPalette::ColorRole::Background, back_color);  
  68.   
  69.     QPushButton *button_2 = new QPushButton();  
  70.     button_2->setText("Button 2");  
  71.     button_2->setFlat(true);  
  72.     button_2->setPalette(back_palet);  
  73.     button_2->move((new LocPoint(370+button_1->width(), 120 + label_1->height()))->getLoc());  
  74.     button_2->resize((new Size(250, 60))->getSize());  
  75.     button_2->show();  
  76.   
  77.     //return the executable result  
  78.     return qt_app.exec();  

First you need to include the header file of that control.

Then declare the control class object and then set their properties.

The above program will display four windows as a single component as shown in the below image.
 


Similar Articles