Qt 项目架构之四:实战项目

下面实现一个最基本的架构项目。

一、程序的代码目录管理

代码目录,就是你存放源码的目录,一般程序都划分为各种模块来实现,所以为每个模块单独创建一个目录来存放,是比较推荐的存放方式。

工程文件的写法 pro:

QT       += core gui network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    MainWidget.cpp \
    src/Control/CustomTooltip.cpp \
    src/Control/MyIconBtn.cpp \
    src/Control/MyTitleBar.cpp \
    src/HoverMenu/HoverMenu.cpp \
    src/Util/AppCommon.cpp \
    src/Util/AppConfig.cpp \
    src/Util/AppData.cpp \
    src/Util/AppEvent.cpp \
    src/Util/AppInit.cpp \
    src/Util/LogHandler.cpp \
    src/Util/SingleApplication.cpp

HEADERS += \
    MainWidget.h \
    src/Control/CustomTooltip.h \
    src/Control/MyIconBtn.h \
    src/Control/MyTitleBar.h \
    src/HoverMenu/HoverMenu.h \
    src/Util/AppCommon.h \
    src/Util/AppConfig.h \
    src/Util/AppData.h \
    src/Util/AppEvent.h \
    src/Util/AppInit.h \
    src/Util/LogHandler.h \
    src/Util/SingleApplication.h

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

RESOURCES += \
    resource/Resource.qrc

TRANSLATIONS += resource/Translate/Translate_EN.ts \
               resource/Translate/Translate_CH.ts

Qt 应用程序,通用的功能模块,一般都会有 资源文件、第三方依赖库文件等,为每个模块分别创建一个目录。

Qt_Framework_A.png


上面图片的 resource 文件夹目录为:

Qt_Framework_B.png


上面图片的 src 文件夹目录为:

Qt_Framework_C.png


QtCreator 项目视图为:

Qt_Framework_D.png


二、通用模块划分

2.1 工具类

类似全局的函数,提供代码通用工具功能,一般放在 src/Util 目录下。

  • 全局配置文件管理类 AppConfig 用来读写对应项目的配置文件。
  • 全局变量管理类 AppData 用来设置项目中用到的所有全局变量。
  • 全局事件中转处理类 AppEvent 用来中转系统中各种跨多个 UI 以及多个类的事件。
  • 全局程序初始化类 AppInit 用来做一些程序启动后的初始化处理。
  • 全局通用类 AppCommon,定义一些例如 delay 等常用函数。
  • 其它更多的一些通用工具类,例如 LogHandler 日志管理类、SingleApplication 单例和全局事件过滤类。

2.2 窗口部件类

开发 Qt 程序,免不了要自定义一部分窗口部件。

  • 一些自定义控件,例如自定义按钮、自定义编辑框等,一般放在 src/Control 目录下。
  • 一些提示框,例如警告框、确认框、提示框等,可以放在 src/TipBox 目录下。

三、源码下载

https://github.com/confidentFeng/QtAppProject


posted @ 2023-02-07 11:58  fengMisaka  阅读(888)  评论(0编辑  收藏  举报