Qt qmake:从变量

时间:2017-04-10 10:54:03

标签: qt qmake

作为Qt项目部署过程的一部分,我需要将几个文件复制到部署文件夹。我的问题是我无法让-= operator工作。我还想根据文件名过滤掉特定文件。

为了重现我的问题,我创建了一个简单的Qt项目,其.pro文件如下所示(参见"感兴趣的部分"):

TEMPLATE = app
QT = core
CONFIG += console
CONFIG -= app_bundle
CONFIG += c++11

SOURCES += main.cpp \
myfile1.cpp \
myfile1d.cpp

SOURCES += myfile1.cpp
SOURCES += myfile1d.cpp

#portion of interest - start
FOLDER_PATH = $$_PRO_FILE_PWD_/../files/

#add all files present in 'files' folder
FILES_TO_COPY = $$FOLDER_PATH/*

#exclude a specific file
win32:CONFIG(release, debug|release): {
FILES_TO_COPY -= $$FOLDER_PATH/filecd.txt
}

filesToCopy.files = $$FILES_TO_COPY
filesToCopy.path = $$_PRO_FILE_PWD_/../deploy

INSTALLS += filesToCopy
#portion of interest - finish

我添加了一个" make install"步骤到Qt Creator项目设置中的构建过程。此步骤应查看.pro文件中的INSTALLS变量,并从'文件中复制指定的文件。部署'夹。

现在,文件夹'文件'包含以下文件:

  • fileb.txt
  • filec.txt
  • filecd.txt
  • filed.txt
  • filedd.txt

这是我想做的事情:

1)最初,我想要除了' filecd.txt'之外的所有文件。要从'文件中复制'部署'。我正在尝试使用' - ='排除' filecd.txt'但是我仍然会复制所有文件。

2)我还想根据他们的名字过滤掉特定的文件。例如,如果我们处于发布模式,则不应复制文件的调试版本。这应该排除filecd.txt和filedd.txt,它们被模拟为filec.txt和filed.txt的调试版本。

感谢您的帮助。

{版本信息:Qt Creator 4.2.1,基于Qt 5.8.0(MSVC 2015,32位)

套件信息:桌面Qt 5.8.0 MSVC2015 32位)}

更新: 对于其他尝试做类似事情的人(以及有经验的人进行审查),这就是我最终做的事情:

#add all files present in 'files' folder
FILES_TO_COPY = $$files($$FOLDER_PATH/*)

#From these files: fileb.txt filec.txt filecd.txt filed.txt filedd.txt
#we only want to copy the following files to the deployment folder
#fileb.txt filec.txt filed.txt

#i.e., we wish to exclude these files from deployment folder: 
#filecd.txt filedd.txt
win32:CONFIG(release, debug|release): {
    #FILES_TO_COPY -= $$FOLDER_PATH/filecd.txt

    for(iter, FILES_TO_COPY): {
        #name of one file
        FNAME = $$iter #/path/to/filecd.txt        

        SEARCH_TOKEN = 'd.txt'
        SECTIONS = $$split(FNAME, $$SEARCH_TOKEN)#filec d.txt

        FNAME1 = $$first(SECTIONS) #filec        

        #if 'd.txt' is not part of the filename then split returns
        #the filename untouched (i.e. FNAME1 would be equal to FNAME,
        #and should be found in FILES_TO_COPY)
        #NOTE: If we are unlucky enough to have another file called                   
        #'filec' (without any extension) then the search for filecd.txt  
        #will be abandoned.        
        !contains(FILES_TO_COPY, $$FNAME1) : {
            FILE_EXT = '.txt'
            FNAME1D = $$join(FNAME1, , , $$FILE_EXT) #filec.txt                
            contains(FILES_TO_COPY, $$FNAME1D): {
                REDUNDANT_FILES += $$FNAME                    
            }
        }

    }
}

FILES_TO_COPY -= $$REDUNDANT_FILES
filesToCopy.files = $$FILES_TO_COPY
filesToCopy.path = $$_PRO_FILE_PWD_/../deploy
INSTALLS += filesToCopy

2 个答案:

答案 0 :(得分:1)

qmake在解析文件后执行glob扩展,因此当解析FILES_TO_COPY -= ...行时,变量仍然具有未扩展的glob值。

相反,使用$$files内置测试功能来组合您可以从中删除的文件列表:

FILES_TO_COPY = $$files($$FOLDER_PATH/*)

win32:CONFIG(release, debug|release): {
  FILES_TO_COPY -= $$FOLDER_PATH/filecd.txt
}

答案 1 :(得分:0)

您可以使用正则表达式删除变量,使其更整洁:

FILES_TO_COPY ~= .*filecd.txt