Win7下安装和使用Qt5详细图解

来源:互联网 发布:财务审计软件oa 编辑:程序博客网 时间:2024/05/20 17:26

1、安装Qt5

Qt5的安装比Qt4的安装简单多了,我装的是Qt5.4(qt-opensource-windows-x86-mingw491_opengl-5.4.0.exe),它集成了MinGW、Qt Creator等,不需要你再单独下载MinGW和Qt Creator。

首先,去Qt官网下载资源:qt-opensource-windows-x86-mingw491_opengl-5.4.0.exe;然后,双击安装即可。

2、配置Qt

打开Qt Creator,工具–>选项,打开“选项”对话框,如下图所示: 
这里写图片描述

若没有检测出,则添加相应的Qt版本和编译器(MinGW),再设置构建套件(Kits):设备类型、编译器(MinGW)、调试器、Qt版本,如下图所示。 
这里写图片描述

3、使用Qt

打开Qt Creator,新建项目–>其他项目–>空的qmake项目,项目命名为“QtTest”,再添加新文件main.cpp。

在main.cpp中添加如下代码:

#include<QApplication>#include<QVBoxLayout>#include<QLabel>#include<QPushButton>int main(int argc,char *argv[]){    QApplication app(argc,argv);    QWidget *window = new QWidget;    window->setWindowTitle("QtTest");    //QLabel *label= new QLabel("Hello Qt");    QLabel *label = new QLabel("<h2><i>Hello</i>""  <font color = red>Qt</font><h2>");    QPushButton *button=new QPushButton("Quit");    QObject::connect(button,SIGNAL(clicked()),&app,SLOT(quit()));    QVBoxLayout *layout=new QVBoxLayout;    layout->addWidget(label);    layout->addWidget(button);    window->setLayout(layout);    window->show();    return app.exec();}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

此时,代码显示如下错误: 
这里写图片描述

运行时错误提示:#include–No such file……

实际上,QT5中很多常用的QT头文件都被移到core gui widgets 等模块中去了,在QT5中,.pro文件需要增加额外的一行(注意大小写): 
这里写图片描述

其中Qt += core gui widgets 表示链接QtCore(d).dll、QtGui(d).dll、QtWidgets(d).dll。

我们在.pro文件中增加一行上述代码,保存,再双击打开.cpp文件,此时错误提示线消失,运行,结果如下图所示: 
这里写图片描述

注: 
1、Qt支持简单的Html样式格式。 
2、MinGW 提供了一套简单方便的Windows下的基于GCC 程序开发环境。MinGW 收集了一系列免费的Windows 使用的头文件和库文件;同时整合了GNU的工具集,特别是GNU程序开发工具,如经典gcc, g++, make等。MinGW是完全免费的自由软件,它在Windows平台上模拟了Linux下GCC的开发环境,为C++的跨平台开发提供了良好基础支持,为了在Windows下工作的程序员熟悉Linux下的C++工程组织提供了条件。

原创粉丝点击