环境搭建
- 工具
- windows
- VS2013下载路径
- VS支持QT环境插件
- gstreamer
- windows
- 编译生成VS可打开的相关文件
- 从qt安装目录进入工程目录
- mkdir build
- cd build
- qmake -tp vc ../projectName.pro
- Qt的发布
- Qt Widgets Application
- Release 方式编译生成 exe 程序(记为test.exe)
- 将test.execopy到另外一个空文件夹(记为test)中
- 从开始菜单的Qt打开命令行,cd test
- windeployqt test.exe
- Qt Quick Application的发布方式
- Release 方式编译生成 exe 程序(记为test.exe)
- 将test.execopy到另外一个空文件夹(记为test)中
- 从开始菜单的Qt打开命令行,cd test
windeployqt test.exe --qmldir <qt安装目录>\5.5\msvc2013\qml
- Qt Widgets Application
Qt5简介
- 1 新特性
- 图形引擎给予GPU,使得应用程序能充分利用硬件加速。
- Qt Quick 2允许开发者进行适用于触摸屏的UI开发,且保持了与原生C++程序近乎相同的运行速率。
- 增加了对C++ 11支持,同时还增强了对JavaScript和QML的支持,而且QML还可以与基于C++的Qt Widgets进行混合编程。
- Qt 5几乎所有的上层API都同时提供面向经典C++和面向QML的两套接口。
- Qt 5重新定义了Qt Gui模块,它为各种图形用户界面组件提供了一般的处理,包括窗口系统集成、事件处理、OpenGL和OpenGL ES集成,2D绘画、基本图像、字体和文本内容等等
- Qt 5支持的平台之上是平台抽象层 QPA,Qt 5把全部的接口都迁移到QPA之上,使得Qt更容易移植到另外的系统和设备上。
- 重新设计了图形堆栈,与Qt 4比提高了性能。QPainter比之前支持更少的后端,限制使用一个光栅后端(Raster Backend)来绘制屏幕、像素与图像,一个OpenGL后端提供GL接口以及一个提供PDF生成与打印的后端。
- 更灵活的模块结构,满足桌面和移动融合,按需添加和删除特定的模块。
2 架构
Qt 5采用模块化代码库,使得平台的移植变得更简单。Qt 5将所有的功能模块分为3个部分:Qt基本模块(Qt Essentials)
Qt 基本模块定义了适用于所有平台的基础功能,是Qt的核心。
Qt扩展模块(Qt Add-Ons)
Qt扩展模块是针对某种特殊目的的额外模块。与基本模块不同,扩展模块的版本兼容性是由模块自身决定的。
- 开发工具(Qt Tools)
资源文件
图标的设置
- windows图标的设置
- 在.qrc资源文件中添加ico图标(test.ico)
- 在Pro文件中添加RC_ICONS = test.ico
- 重新执行qmake,生成相应的Makefile,之后运行代码就可以了
- mac和Linux的图标设置参考
库文件
- 头文件 INCLUDEPATH += lib_dir
qml
C++逻辑层
Qt自定义log输出
消息的类型:enum QtMsgType
constant | value | constant |
---|---|---|
QtDebugMsg | 0 | A message generated by the qDebug() function. |
QtInfoMsg | 4 | A message generated by the qInfo() function. |
QtWarningMsg | 1 | A message generated by the qWarning() function. |
QtCriticalMsg | 2 | A message generated by the qCritical() function. |
QtFatalMsg | 3 | A message generated by the qFatal() function. |
QtSystemMsg | QtCriticalMsg |
#include <QFile>
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QTemporaryFile>
#include <QMessageLogger>
#include <QMessageLogContext>
#include <QDateTime>
#include <QDebug>
QFile * fileLog = NULL;
const QString msgHead[] = {
"D", // Debug
"E", // warning
"!", // Crital
"X", //Fatal
"I" //Info
};
static void msgHandler(QtMsgType type,const QMessageLogContext &context,const QString &msg)
{
QString curTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
QString msgText = QString("%1 [%2] [%3 %4 %5] %6\n").arg(msgHead[type]).arg(curTime).arg(context.file).arg(context.line).arg(context.function).arg(msg);
if(fileLog){
QTextStream text(fileLog);
text << msgText;
}else {
fprintf(stdout,"%s",msgText);
}
}
void msgInstaller(void)
{
qInstallMessageHandler(msgHandler);
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
fileLog = new QFile("log.txt");
if(!fileLog->open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)){
return -1;
}
msgInstaller();
qDebug() << "Yeah ";
qCritical() << "NO ";
qInfo() << "Info";
qWarning() << "Warning";
return app.exec();
}
输出结果:
D [2017-08-08 20:53:43] [..\Hybrid\main.cpp 50 int __cdecl main(int,char *[])] Yeah
! [2017-08-08 20:53:43] [..\Hybrid\main.cpp 51 int __cdecl main(int,char *[])] NO
I [2017-08-08 20:53:43] [..\Hybrid\main.cpp 52 int __cdecl main(int,char *[])] Info
E [2017-08-08 20:53:43] [..\Hybrid\main.cpp 53 int __cdecl main(int,char *[])] Warning
附录
错误汇总
- 1 无法启动该程序,因为计算中丢失Qt5Core.dll
- 出错原因:没有相关的动态库
- 解决方法:在计算中添加相关的动态库路径
- 1 cmd设置
setx PATH “%PATH%;<Qt安装路径>\5.5\msvc2013\bin;<Qt安装路径>\Tools\QtCreator\bin”