Skip to content
ELECTRONICS ENGINEERINGelectronics-engineering.com
Qt Visual C++

A 5-Minute Qt Development Tutorial with Visual Studio

Qt is a cross-platform open-source GUI framework for C++. Nokia provides an excellent development program called Qt Creator, but it also supports Qt development with Visual Studio.…

https://www.electronics-engineering.com/en/5-minute-tutorial-on-qt-development-with-visual-studio/

Was this page useful? Rate it:
average 4.0 from 1 rating

Qt is a cross-platform open-source GUI framework for C++. Nokia provides an excellent development program called Qt Creator, but it also supports Qt development with Visual Studio.

Introduction

Qt is a cross-platform open-source GUI framework for C++. Nokia provides an excellent development program called Qt Creator, but it also supports Qt development with Visual Studio. Although they do not promote Visual Studio support for their free edition, Nokia provides versions of Qt for Visual Studio 2008 on its website.
This tutorial describes how to set up the Qt development environment and use it with Visual Studio to create a (single) simple GUI application.

Prerequisites

  • Visual Studio 2008
  • Qt 4.6.1 (Visual Studio build) download link
  • Qt Addin for Visual Studio download

Lesson

Step 1: Setting up the environment

This part is easy. Just download the installers from the link above and run them. There is no need to build Qt from source.

Step 2: Create a Qt Application in Visual Studio

1.    In Visual Studio 2008, choose File > New > Project

2.    In the New Project dialog, select Qt Application from the Qt4 Projects category. Name the project qtdemo.

3.    In the Qt4 GUI wizard, click Finish to accept the default settings.

4.    In the Visual Studio Solution Explorer, examine the files generated by the wizard. If you named the project qtdemo, there should be
o A form file named qtdemo.ui
o A header file named qtdemo.h
o Source files named main.cpp and qtdemo.cpp

5.    Build and run the project in Visual Studio. An empty GUI window should appear.

Step 3: Customise the GUI

  1. In the Visual Studio Solution Explorer, double-click the qtdemo.ui file. The Qt Designer application will open and show the blank form.
  2. Drag a Button widget onto the form. Change its name to btnHello by editing the objectName property to btnHello
  3. Save the form and close Qt Designer.
  4. Back in Visual Studio, build and run the project. The GUI window should now contain the button.

Step 4: Add an Event Handler

In Solution Explorer, open qtdemo.h. Add new code to the qtdemo class, as shown below. Then build and run the project. Click the button, and the text on the button should change to “Hi”.

class qtdemo : public QMainWindow
{
    Q_OBJECT
 
public:
    qtdemo(QWidget *parent = 0, Qt::WFlags flags = 0);
    ~qtdemo();
 
private:
    Ui::qtdemoClass ui;
 
// begin new code
public slots:
    void on_btnHello_clicked() {
        ui.btnHello->setText("Hi!");
    }
// end new code
 
};

A few notes for those new to Qt:

Qt uses a mechanism called signals and slots to handle events. Widgets (like the button in this example) emit signals (like clicked) that can be received by slots (in this example, on_btnHello_clicked). Signals can be connected to slots by using a function called connect(), but in this example the association was made by name using Qt’s automatic connection feature.

A Word on Code Completion

Qt uses a tool called the User Interface Compiler (UIC) to convert form definition .ui files to C++ implementations. The uic is invoked automatically when you build the project in Visual Studio, and it generates files named (for example) ui_qtdemo.h. Visual Studio can use the code in these files to provide code completion for form members.


When you make a change to a .ui file in the designer, you need to update the Visual Studio IntelliSense database to get code completion for the newly added members. To do this, either rebuild the entire project, or manually request the regeneration of individual ui_*.h files by right-clicking a .ui file in the Visual Studio Solution Explorer and choosing Compile.


I have had limited success with code completion. Sometimes it works and sometimes it does not; your situation may be different!

OPEN A WINDOW FROM ANOTHER WINDOW

This example (built with Visual Studio and the Qt libraries) shows simply and quickly how, from a window designed in Qt and run in Visual C++, it is possible to open another window.

The simplicity of GUIs built with Qt makes it possible to develop software with great impact.

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
1
0
Would love your thoughts, please comment.x
()
x