[Qt] vs2015 new blank Qt project


Recently studying Qt, Station B After reading a good tutorial, the teacher pulled all the code from scratch without IDE, which is very helpful for novices to learn and understand. Sometimes it is convenient to use IDE, but also missed a lot of details. This tutorial uses UBuntu+vim, which is compiled directly with qmake. It is still a little difficult for novices. I use vs2015 + Qt5 14.2 during the implementation, several small problems were solved smoothly and recorded.

Environment configuration

VS2015 + Qt vs Tools
Qt5.14.2 or higher
I won't talk about installing Qt and configuring QtVsTools. There are a lot of online tutorials. It is recommended to configure both before continuing to learn

Create project

Open VS, create a new project, select Qt - Qt Empty Application, enter the name, and click OK

The next step is OK until Finish

Add file

Operation results

The tutorial is to make a simple addition calculator. The operation results are as shown in the figure above. After entering valid numbers in the left and right input boxes, = takes effect. After clicking =, the output results will be output to the third input box. The first two input boxes can only enter numbers, not other text.

file structure

Contains three files, main cpp,Calculator.cpp,Calculator.h. Add them in the corresponding files of Vs in sequence without repeating


For all codes, it is recommended to roll them by hand. Only after you code them all again can you have the effect and remember the accurate usage.


#include <qapplication>
#include "Calculator.h"

int main(int argc, char** argv)
	QApplication app(argc, argv);
	Calculate cal;
	return app.exec();


#include "Calculator.h"

	m_edtNumL = new QLineEdit(this);
	m_edtNumL->setValidator(new QDoubleValidator(this));
	m_edtNumR = new QLineEdit(this);
	m_edtNumR->setValidator(new QDoubleValidator(this));
	m_labPlus = new QLabel("+",this);
	m_btnPlus = new QPushButton("=", this);
	m_edtResult = new QLineEdit(this);
	QHBoxLayout *layout = new QHBoxLayout(this);


	connect(m_edtNumL, SIGNAL(textChanged(QString)), this, SLOT(EnablePlusBtn(void)));
	connect(m_edtNumR, &QLineEdit::textChanged, this, &Calculate::EnablePlusBtn);
	connect(m_btnPlus, &QPushButton::clicked, this, &Calculate::OnClickPlus);

void Calculate::EnablePlusBtn(void)
	bool LOk, ROk;
	m_btnPlus->setEnabled(LOk && ROk);

void Calculate::OnClickPlus(void)
	double result;
	result = m_edtNumL->text().toDouble() + m_edtNumR->text().toDouble();
	QString str = QString::number(result);


#ifndef __CALCULATOR_H
#define __CALCULATOR_H

#include <QDialog>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QDoubleValidator>

class Calculate :public QDialog {
	void OnClickPlus(void);
	void EnablePlusBtn(void);
	QLabel * m_labPlus;
	QLineEdit* m_edtNumL;
	QLineEdit* m_edtNumR;
	QPushButton* m_btnPlus;
	QLineEdit* m_edtResult;



Compile run

If you add according to this process, you will always be prompted when entering the code. The header file is not found. The problem is that qt vs tools is not configured. Because it is a new empty template, it is not automatically configured. Clicking compile will also cause problems. Next, configure qt vs tools. Right click the project, select properties, find Qt Installation in Qt Project Setting, select the QT version you installed and configured, and enter the QT module used in the current project in Qt Modules. Here, there are three modules: core, gui and widgets. You can enter them and use them in the middle; Separate, click apply, confirm, and then compile and run


Be sure to write the code yourself

I've seen a lot of tutorials before, which directly use the vs template to directly create projects. Many places are vague. I don't know what's going on. This time, after reading the tutorial, I have a lot of problems when I write the code myself. If there are problems, I'll look up and turn over the tutorial to deepen my impression, so novices still have to write more and practice more

Chinese garbled code

I saw a PPT before the problem of garbled code, and it was very clear what was going on and how to solve it. At that time, I tried it and forgot to solve the problem. This time, I picked it up and studied again, and there was another problem of Chinese garbled code. The screenshot is garbled, and the QString::fromLocak8bit in the code can correctly display Chinese. But it's still inconvenient. If an expert knows how to solve it, please comment and let me know.


It's a bad habit that the code is not annotated, because it's done with the video. In addition, it's a little basic before, so it doesn't write comments. There are two ways to use connect: one is the standard writing of signal and slot, and the other is the function pointer.

Keywords: Qt

Added by TheHaloEffect on Mon, 10 Jan 2022 04:27:25 +0200