QT调试阶段如何更好地调用xml/ini配置文件,第三方的动态库

本文需求:ini/xml等文件放只在工程目录下版本管理,编辑,不人工拷贝到执行目录,出乱。build和release目录可以随意在clean阶段删除.
解决类似奇怪解决方案:利用宏分别给调试和运行阶段配置两个路径。
解决类似奇怪需求:qt 获取工程路径qt 获取工程路径qt 获取工程路径qt 获取当前程序运行路径
解决类似问题 :QT项目开发过程中运行路径问题

解决办法:在Qt Creator的build阶段增加脚本,将配置文件拷贝到exe执行文件所在目录。其他不需要独立开来的资源文件交给Qrc管理。
额外需求,将exe执行文件发布到项目上一层目录Deployed下,该发布目录与其他项目共享。

前置条件:

  1. 自行安装MinGW(这里使用git自带的git-bash), 因为QT自带MinGW不能运行bash文件(也可以单独使用bat脚本文件完成任务,后期再加)

工程目录:

+ MyProject
  + build-MyProject-Desktop_Qt_6_3_1_MinGW_64_bit-Debug/

    + debug/

      - MyProject.ini

      - MyProject.xml

      - MyProject.exe

  + build-MyProject-Desktop_Qt_6_3_1_MinGW_64_bit-Release/

    + release/

      - MyProject.ini

      - MyProject.xml

      - MyProject.exe

  + Deployed/
    - MyProject.ini

    - MyProject.xml

    - MyProject-release.exe
    - MyProject-debug.exe
  + MyProject/
    - MyProject.pro
    - MyProject.bat
    - MyProject.sh
    - MyProject.ini

    - MyProject.xml

  • Configure Project 使用默认配置
  •  Release 模式下的 Build的步骤,添加一个Custom Process Step,并配置如下,Release则带参数 release,Debug模式则带参数debug。
  • MyProject.bat
    给MyProject.sh 传递参数,项目名称 和 编译模式(debug/release)
@echo off
REM echo PATH = %PATH%

:: The directory of this script is the expanded absolute path of the "$qt_prefix/bin" directory.
set script_dir_path=%~dp0

:: This is the name of this bat-file as %[PROJECT_NAME].
set project_name=%~n0

:: This is the MODE of project-building. try to use the first param, otherwise use "debug".
set build_mode=%1
if not exist "%build_mode%" set build_mode=debug

:: Try to use original bash, otherwise to make it relocatable, use D:/Git/bin/bash found in PATH.
set git_bash_path=D:/Git/git-bash.exe
if not exist "%git_bash_path%" set git_bash_path=bash.exe

:: This is infomations for debug.
echo git_bash_path = %git_bash_path%
echo script_dir_path = %script_dir_path%
echo project_name = %project_name%
echo build_mode = %build_mode%

"%git_bash_path%" %script_dir_path%%project_name%.sh %project_name% %build_mode% 

REM # How to configure to run this bat-file in QTCreator
REM - Build Steps
REM     - Custom Process Step: [PROJECT_NAME].bat release
REM         + Command          : [PROJECT_NAME].bat
REM         + Arguments        : release
REM         + Working directory: %{buildDir}
  • MyProject.sh
    主要使用bash的*号的,不用找到build目录的完整名称,缺点是只支持一个平台的、一个QT版本的、一个编译器。
#!/bin/sh
# while use bat-file to run this sh-file, the Working directory must be %{buildDir} -- The same as bat-file.

_project_name=$1
_build_mode=$2
project_name=${_project_name:=MyProjectName}
build_mode=${_build_mode:=debug}
pn=${project_name}
bm=${build_mode}

## This is infomations for debug.
pwd
echo "project_name = ${pn}"
echo "build_mode   = ${bm}"

# Copy bin-file To Deployed as [project_name]-[build_mode].exe
[ ! -d  ../Deployed ] && mkdir -p ../Deployed
[ -f ./$bm/$pn.exe ] && cp -v ./$bm/$pn.exe ../Deployed/$pn-$bm.exe

ProjectHome="../$pn"
WorkingHome="."
for file in `find $ProjectHome -name "*.ini"`; do cp -v $file $WorkingHome;cp -v $file $DeployedDir;done
for file in `find $ProjectHome -name "*.xml"`; do cp -v $file $WorkingHome;cp -v $file $DeployedDir;done
for file in `find $ProjectHome -name "*.db"` ; do cp -v $file $WorkingHome;cp -v $file $DeployedDir;done
read -t 5
## # How to configure to run this sh-file by bat-file in QTCreator:
## - Build Steps
##     - Custom Process Step: [project_name].bat [build_mode]
##         + Command          : [project_name].bat
##         + Arguments        : [build_mode]
##         + Working directory: %{buildDir}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

亮化Chernic

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值