QMake 之遍历文件夹


前言

在开发过程中,不知道你有没有遇到中途需要导入新的多个三方库的情况,反正我遇到了,在这里记录一下偷懒技巧。

知识点

  • QMake

  • Qt 项目文件 -.pro
  • 目录

  • QMake介绍

  • Qt 项目文件 -.pro介绍

  • QMake 遍历实际用处
  • QMake介绍

    这里我简要说说我自己对于 QMake 的理解吧,顺便挂一个较为详细的链接,你可以跳转到这里,阅读详情。
    我认为的 QMake,本质上与 CMake 类似,它们的目标都是在跨平台不用修改配置文件代码,在项目开发中,越来越多的使用 CMake 配置开发项目,而 QMake 也是随着 Qt 的普及而被人们接触。

    Qt 项目文件 -.pro 介绍

    QMake 语言格式所表示的 Qt 项目配置文件,说明项目的配置属性等信息,用于 Makefile 的生成或者编译

    QMake 遍历实际用处

    前置说明: 遍历当前目录源代码所有文件

    -foo
        -ExampleQMakeErgodicDir.pro
        -ExampleQMakeErgodicDir.pro.user
        -helloWorld.h
        -helloWorld.cpp
        -main.cpp
    

    Step 01: 在.pro 文件中添加一下 QMake 代码*

    # Traverse all the files in the source code directory and output them to $${SOURCES_DIR_ALL_FILES}
    SOURCES_DIR_ALL_FILES = $$files($$PWD/*.*)
    
    # Traverse the source code directory header file, and output to $${SOURCES_DIR_HEADER_FILES}
    SOURCES_DIR_HEADER_FILES = $$files($$PWD/*.h)
    
    # Traverse the source code directory cpp file, and output to $${SOURCES_DIR_CPP_FILES}
    SOURCES_DIR_CPP_FILES = $$files($$PWD/*.cpp)
    
    # Output variable $${SOURCES_DIR_ALL_FILES} All elements of files
    for(filename, SOURCES_DIR_ALL_FILES): message('[SOURCES_DIR_ALL_FILES] print filename is $${filename}')
    
    # Output variable $${SOURCES_DIR_HEADER_FILES} All elements of files
    for(filename, SOURCES_DIR_HEADER_FILES): message('[SOURCES_DIR_HEADER_FILES] print filename is $${filename}')
    
    # Output variable $${SOURCES_DIR_CPP_FILES} All elements of files
    for(filename, SOURCES_DIR_CPP_FILES): message('[SOURCES_DIR_CPP_FILES] print filename is $${filename}')
    

    输出信息

    // SOURCES_DIR_ALL_FILES
    Project MESSAGE: [SOURCES_DIR_ALL_FILES] print filename is G:/ExampleQMakeErgodicDir/ExampleQMakeErgodicDir.pro
    Project MESSAGE: [SOURCES_DIR_ALL_FILES] print filename is G:/ExampleQMakeErgodicDir/ExampleQMakeErgodicDir.pro.user
    Project MESSAGE: [SOURCES_DIR_ALL_FILES] print filename is G:/ExampleQMakeErgodicDir/helloWorld.cpp
    Project MESSAGE: [SOURCES_DIR_ALL_FILES] print filename is G:/ExampleQMakeErgodicDir/helloWorld.h
    Project MESSAGE: [SOURCES_DIR_ALL_FILES] print filename is G:/ExampleQMakeErgodicDir/main.cpp
    
    // SOURCES_DIR_HEADER_FILES
    Project MESSAGE: [SOURCES_DIR_HEADER_FILES] print filename is G:/ExampleQMakeErgodicDir/helloWorld.h
    
    // SOURCES_DIR_CPP_FILES
    Project MESSAGE: [SOURCES_DIR_CPP_FILES] print filename is G:/ExampleQMakeErgodicDir/helloWorld.cpp
    Project MESSAGE: [SOURCES_DIR_CPP_FILES] print filename is G:/ExampleQMakeErgodicDir/main.cpp
    
    // [Warning]
    // Repeated output can be solved by setting the value of the config element, but it's a bit troublesome. 
    // Here, we directly choose to ignore this situation.
    Project MESSAGE: [SOURCES_DIR_ALL_FILES] print filename is G:/ExampleQMakeErgodicDir/ExampleQMakeErgodicDir.pro
    Project MESSAGE: [SOURCES_DIR_ALL_FILES] print filename is G:/ExampleQMakeErgodicDir/ExampleQMakeErgodicDir.pro.user
    Project MESSAGE: [SOURCES_DIR_ALL_FILES] print filename is G:/ExampleQMakeErgodicDir/helloWorld.cpp
    Project MESSAGE: [SOURCES_DIR_ALL_FILES] print filename is G:/ExampleQMakeErgodicDir/helloWorld.h
    Project MESSAGE: [SOURCES_DIR_ALL_FILES] print filename is G:/ExampleQMakeErgodicDir/main.cpp
    Project MESSAGE: [SOURCES_DIR_HEADER_FILES] print filename is G:/ExampleQMakeErgodicDir/helloWorld.h
    Project MESSAGE: [SOURCES_DIR_CPP_FILES] print filename is G:/ExampleQMakeErgodicDir/helloWorld.cpp
    Project MESSAGE: [SOURCES_DIR_CPP_FILES] print filename is G:/ExampleQMakeErgodicDir/main.cpp
    

    相关