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.
- #include<qapplication.h>
- #include<qwidget.h>
-
- int main(int argc, char**argv)
- {
-
- QApplication qt_app(argc, argv);
-
-
- QWidget *window = new QWidget();
-
-
- QSize window_size;
- window_size.setWidth(600);
- window_size.setHeight(400);
-
-
- QPoint window_location;
- window_location.setX(100);
- window_location.setY(80);
-
-
- window->resize(window_size);
- window->move(window_location);
- window->setWindowTitle("Qt Demo");
-
-
- window->show();
-
-
- return qt_app.exec();
- }
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.
- #include<qapplication.h>
- #include<qwidget.h>
- #include<qlabel.h>
- #include<qpushbutton.h>
-
- class LocPoint{
- QPoint location;
- public:
- LocPoint(int x, int y){
- location.setX(x);
- location.setY(y);
- }
- QPoint getLoc() const{
- return location;
- }
- };
-
- class Size{
- QSize size;
- public:
- Size(int w, int h){
- size.setWidth(w);
- size.setHeight(h);
- }
- QSize getSize() const{
- return size;
- }
- };
-
- int main(int argc, char *argv[])
- {
-
- QApplication qt_app(argc, argv);
-
- QSize size;
-
-
- QString label_text = "Hello World";
- QString html_tags_label_text = "<h1><font color='#ed2332'>Hello World</font></h1>";
-
-
- QLabel *label_1 = new QLabel();
- label_1->setText(label_text);
- label_1->move((new LocPoint(300, 80))->getLoc());
- label_1->resize((new Size(250, 60))->getSize());
- label_1->show();
-
- QLabel *label_2 = new QLabel();
- label_2->setText(html_tags_label_text);
- label_2->move((new LocPoint(370+label_1->width(),80))->getLoc());
- label_2->resize((new Size(250, 60))->getSize());
- label_2->show();
-
-
- QPushButton *button_1 = new QPushButton();
- button_1->setText("Button 1");
- button_1->move((new LocPoint(300, 120+label_1->height()))->getLoc());
- button_1->resize((new Size(250, 60))->getSize());
- button_1->show();
-
-
- QColor back_color;
- back_color.setRgb(80, 120, 220);
-
-
- QPalette back_palet,fore_palet;
- back_palet.setColor(QPalette::ColorGroup::Active, QPalette::ColorRole::Background, back_color);
-
- QPushButton *button_2 = new QPushButton();
- button_2->setText("Button 2");
- button_2->setFlat(true);
- button_2->setPalette(back_palet);
- button_2->move((new LocPoint(370+button_1->width(), 120 + label_1->height()))->getLoc());
- button_2->resize((new Size(250, 60))->getSize());
- button_2->show();
-
-
- 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.