关于Qt Creator & Visual Studio 2017的安装不在本文介绍之中
Qt Creator4.6.2新建项目工程
1、创建工程
- 左上角 文件->新建文件或项目,如下图所示,选择Qt Widgets Application(Widget用于可视窗口显示,Console用于在命令行窗口显示,Qt Quick于后续介绍)
- 单击choose下一步,Qt会在指定的路径(F:\QTFile\PersonProgramFiles)创建untitled1这个文件,这个也是项目工程目录
- 点击下一步,默认选择Desktop Qt 5.11.1 MinGW 32bit
- 点击下一步,选择要创建项目的基本类信息,默认选择QMainWindow,关于基类三个(QMainWindow、QWidget和QDialog)详细具体区别可以参考网上资料
- 点击完成,打开左边栏目mainwindow.cpp文件,如下图所示
- 直接编译运行如下图所示,生成界面中有三个部分,依次是菜单栏–主界面–状态栏,(工具栏没有直观表现出来)
2、编写Hello World
- 在主Widget中显示 Hello World!,代码如下
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLabel>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//在主Widget中(centralWidget)新建一个Label标签用于显示字符串
QLabel *label = new QLabel(ui->centralWidget);
//为这个新建的Label标签定义一个名字,“labelShow”
label