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();
}
此時,代碼顯示如下錯誤:
運行時錯誤提示:#include<QApplication>–No such file……
實際上,QT5中很多常用的QT頭文件都被移到core gui widgets 等模塊中去了,在QT5中,.pro文件需要增加額外的壹行(註意大小寫):
QT += core gui widgets
其中Qt += core gui widgets 表示鏈接QtCore(d).dll、QtGui(d).dll、QtWidgets(d).dll。
我們在.pro文件中增加壹行上述代碼,保存,再雙擊打開.cpp文件,此時錯誤提示線消失,運行,結果