涉及到c++ 14新特性: lambda,autovariables.
A basic .pro file generally contains:
1) Qt modules used (core, gui, and so on)
2) Target name (todo, todo.exe, and so on)
3) Project template (app, lib, and so on)
4) Sources, headers, and forms
----------------------------------------------------- 例 子-----------------------------------------------------------------------
In Qt Creator, you can create a new Qt project via File | New File or Project | Application| Qt Widgets Application.
下面四个文件的基本架构都是由qt creator在创建工程时自动生成的!
1) pro文件:
For GCC and CLANG compilers, you must add CONFIG += c++14 to the .pro file to enable C++14 on a Qt project, as shown in the following code:
QT += core gui
CONFIG += c++14
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = todo
TEMPLATE = app
SOURCES += main.cpp \
MainWindow.cpp
HEADERS += MainWindow.h \
FORMS += MainWindow.ui \ //qt5里面没有.ui
其中,MainWindow.ui是xml形式的ui 文件,可以由qt creator打开。
2)main.cpp file
#include "MainWindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
As usual, the main.cpp file , by default, perform two actions:
Instantiate and show your main window
Instantiate a QApplication and execute the blocking main event loop
Qt tip:
编译快捷键:Ctrl + B (for Windows/Linux) or Command + B (for Mac)
Debug模式下运行快捷键: F5 (for Windows / Linux) or Command +R (for Mac) to run your application in debug mode。
3)mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow; //用于声明在Ui命名空间中存在一个与界面对应的MainWindows类,跟下面定义的同名类是不同的。 ?
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
//C++关键字explici,声明为explicit的构造函数不能在隐式转换中使用。
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void addTask();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
私有成员变量ui属于类 Ui::MainWindow, which is defined in the ui_MainWindow.h file generated by Qt. It's the C++ transcription of the UI design file MainWindow.ui, 如果你用qt designer添加了一些控件,例如按钮,重新编译后,会看到ui_MainWindow.h中会增加相应的button的定义,从而也可以直接在该文件中通过代码来增加控件!!
The ui member variable will allow you to interact with your UI components (QLabel, QPushButton, and so on) from C++。
4)mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h" //该文件是根据mainwindow.ui文件自动生成的
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow) //通过初始化成员列表来给成员变量ui赋值。
{
// setupUi function is to initialize all widgets used by the MainWindow.ui design file,例如 menuBar = new QMenuBar(MainWindow);
ui->setupUi(this);
//connect的使用,pushButton 是按钮类型的指针,其在ui_mainwindow.h中被定义:pushButton = new QPushButton(centralWidget);
//信号接收者: QApplication::instance(). It is the QApplication object created in main.cpp.
//槽函数:&QApplication::quit,this is a pointer to one of the receiver's member slot functions. In
//this example, we use the built-in quit() slot from Qapplication, which will exit the application. quit是 QCoreApplication类的静态函数。
connect(ui->pushButton, &QPushButton::clicked,
QApplication::instance(), &QApplication::quit);
//使用自己在类中定义的槽函数:
connect(ui->pushButton_2, &QPushButton::clicked,
this, &MainWindow::addTask);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::addTask()
{
qDebug() << "User clicked on the button!";
}
ui_mainwindow.h 中定义了命名空间Ui。可以查看代码。
--------------------------------------------------- 例 子 end-----------------------------------------------------------------------
新建一个类,来用这个类hold our data. 新建的这个类有自己的ui文件,从而可以与mainWindow区分开来。qt creator提供了一个自动工具
来新建一个C++类以及与它关联的ui文件。