1.首先按照一般步骤生成一个项目文件HELLO-QT:CTRL+N->在项目模板中选择Application->选择Qt Widgets Application,设置项目名称和路径,勾选Desktop构建套件,其他默认,然后生成一个桌面Qt图形界面项目HELLO-QT。
2.打开main.cpp文件,我将文件修改如下,同时project设置为release,保存后试运行HELLO-QT,运行结果如下图。
//#include "mainwindow.h"
//#include
//int main(int argc, char *argv[])
//{
// QApplication a(argc, argv);
// MainWindow w;
// w.show();
// return a.exec();
//}
#include //包含文件
#include
#include
#include //提供文本类转换功能
int main(int argc, char *argv[])//函数头
{
QApplication hello(argc,argv);//创建QApplication对象,两个参数接收命令行参数
//setCodecForLocale() 返回系统指定字符集
//或QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF8"));
QTextCodec::setCodecForLocale(QTextCodec::codecForLocale());
QDialog dhello;
dhello.resize(400,300);//设置窗口宽*高,单位为像素
QLabel label(&dhello);
label.move(120,120);//设置字体位置
label.setText(QStringLiteral("Hello,Qt! 你好,QT")); //设置文本
dhello.show(); //显示label,避免组件闪烁
return hello.exec(); //时间循环
}
3.命令行运行项目
为了方便运行,将HELLO-QT整个项目拷贝到QT 5.8.0安装位置(我的是D:\Qt\Qt5.8.0\5.8\mingw53_32)。
(1)点击开始->所有应用->QT 5.8.0->QT 5.8 for Desktop(MinGW5.3.0 32 bit)->cd HELLO-QT,然后y依次输入qmake -project->qmake->make->cd release->HELLO-QT.exe。
通常make这里会出现如图中白色部分的错误。
(2) 手动修改手动修改HELLO-QT.pro文件,添加greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
(3) 重复qmake->make-> cd release->HELLO-QT.exe操作,成功运行HELLO-QT.exe,出现"Hello,Qt! 你好,QT"窗口界面。