← Back to blog

Qt Designer

·1 min read

Qt

This is an early study note from 2018. Some of it may be out of date.

Installing Qt

Using Qt in VS2015

  • Now let’s actually use Qt in VS2015. First, we need to create a Qt project.

  • In VS2015, go to File -> New -> Project. You will see the following window:

  • Under the Visual C++ templates choose Qt, then select Qt GUI Application. Leave the .NET Framework option at the top as the default.
  • Click Next and you will see the following window:

  • After clicking Next, you will see:


Pick whatever modules you need here. If you are a beginner, you can simply click Next.

  • After clicking Next again, you will see:


The defaults are fine here, so just click Finish. Change the settings if you need to.

  • Once the project is created, you will see this:

  • This is the workspace we will operate in. Now double-click QTTest.ui (under Solution Explorer -> Form Files).
  • This opens the UI design view (shown below):

  • Here you can drag widgets straight onto the form. In the Object Inspector (shown below) you can see the class of each widget and the object generated for it.

  • After creating the objects, click File -> Save (Ctrl+S) in the top-left corner.
  • Then close Qt Designer, go back to VS2015, and right-click QTTest.ui and choose Compile (Ctrl+F7), as shown below:

  • Once it compiles (it normally does), double-click QTTest.cpp. The code inside looks like this:
#include "QTTest.h"

QTTest::QTTest(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);
}
  • Now, type ui.pushButton below ui.setupUi to get the instance of the button you just created:
#include "QTTest.h"

QTTest::QTTest(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);
	ui.pushButton;
}
  • In Qt, widgets pass data to each other through signals and slots. Here is one way to do it:
#include "QTTest.h"

QTTest::QTTest(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);
	connect(ui.pushButton, &QPushButton::clicked, [this] {
		ui.label->setText(ui.label->text().replace(QRegExp("\\d+"), "25"));
	});
}
  • The result:

  • Before clicking the button:

  • After clicking the button: