先看qml代码:

   //QQuickImageProvider方式
Button { id: idBtnGrab; width: 119; height: 119; checkable: true; x:300; y:300
background: Item{ Image{width: parent.width;height: parent.height;fillMode: Image.PreserveAspectFit; source: (idBtnGrab.checked)? "image://iconProvider/resource/grab_click.png" :"image://iconProvider/resource/grab_default.png";}}
}
//相对资源路径方式,这种方式的前提是在main中添加了 engine.addImportPath( QString("qrc:/"));即可省去qrc:/
//直接写成resource/clock_os.png
Button { id: idBtnGrab2; width: 119; height: 119; checkable: true; x:300; y:450
background: Item{ Image{width: parent.width;height: parent.height;fillMode: Image.PreserveAspectFit; source: (idBtnGrab2.checked)? "resource/clock_os.png" :"resource/clock_od.png";}}
}
//绝对资源路径方式
Button { id: idBtnGrab3; width: 119; height: 119; checkable: true; x:450; y:300
background: Item{ Image{width: parent.width;height: parent.height;fillMode: Image.PreserveAspectFit; source: (idBtnGrab3.checked)? "qrc:/resource/od_clock.png" :"qrc:/resource/os_clock.png";}}
}
//绝对路径方式,这种不可取
Button { id: idBtnGrab4; width: 119; height: 119; checkable: true; x:450; y:450
background: Item{ Image{width: parent.width;height: parent.height;fillMode: Image.PreserveAspectFit; source: (idBtnGrab4.checked)? "C:/Users/Administrator/Desktop/qmlResourceDemo/resource/eye_no.png" :"C:/Users/Administrator/Desktop/qmlResourceDemo/resource/eye_no.png";}}
}

这里着重讲解第一种方式,这种方式非常适用于大型项目模块化编程管理,各个模块分开成立项目,最后合并项目的时候,只需要将资源文件拷贝到执行目录下(这里需要配合cmd来拷贝文件),而且不会像资源文件哪样造成exe文件非常庞大。
下面贴出cmd文件

set CP_DRV=%~d0
set CP_PATH=%~dp0
echo CP_PATH
rem ===========================================================================
rem %1 is source dir, %2 is the dest dir ( no debug ), %3 is debug or release
rem ===========================================================================

if not exist "%1" goto NO_SOURCE
if not exist "%2" mkdir "%1"
set SRC_DIR=%1
set DST_DIR=%2

if "%3" equ "debug" goto DEBUG_VERSION
goto REL_VERSION

rem ===================================================================
rem copy the library of debug version
rem ===================================================================
:DEBUG_VERSION
if not exist %DST_DIR%\debug mkdir %DST_DIR%\debug
if not exist %DST_DIR%\debug\resource mkdir %DST_DIR%\debug\resource
if not exist %DST_DIR%\data_store mkdir %DST_DIR%\data_store

if not exist %DST_DIR%\debug\resource\dropdown.png copy %SRC_DIR%\resource\dropdown.png %DST_DIR%\debug\resource\ /y
if not exist %DST_DIR%\debug\resource\play.png copy %SRC_DIR%\resource\play.png %DST_DIR%\debug\resource\ /y
if not exist %DST_DIR%\debug\resource\stop.png copy %SRC_DIR%\resource\stop.png %DST_DIR%\debug\resource\ /y
if not exist %DST_DIR%\debug\resource\grab_default.png copy %SRC_DIR%\resource\grab_default.png %DST_DIR%\debug\resource\ /y
if not exist %DST_DIR%\debug\resource\grab_click.png copy %SRC_DIR%\resource\grab_click.png %DST_DIR%\debug\resource\ /y
if not exist %DST_DIR%\debug\resource\caliper.png copy %SRC_DIR%\resource\caliper.png %DST_DIR%\debug\resource\ /y
if not exist %DST_DIR%\debug\resource\mark.png copy %SRC_DIR%\resource\mark.png %DST_DIR%\debug\resource\ /y
if not exist %DST_DIR%\debug\resource\area.png copy %SRC_DIR%\resource\area.png %DST_DIR%\debug\resource\ /y
if not exist %DST_DIR%\debug\resource\comment.png copy %SRC_DIR%\resource\comment.png %DST_DIR%\debug\resource\ /y
if not exist %DST_DIR%\debug\resource\eye_no.png copy %SRC_DIR%\resource\eye_no.png %DST_DIR%\debug\resource\ /y
if not exist %DST_DIR%\debug\resource\eye_yes.png copy %SRC_DIR%\resource\eye_yes.png %DST_DIR%\debug\resource\ /y
if not exist %DST_DIR%\debug\resource\os_clock.png copy %SRC_DIR%\resource\os_clock.png %DST_DIR%\debug\resource\ /y
if not exist %DST_DIR%\debug\resource\od_clock.png copy %SRC_DIR%\resource\od_clock.png %DST_DIR%\debug\resource\ /y
if not exist %DST_DIR%\debug\resource\clock_od.png copy %SRC_DIR%\resource\clock_od.png %DST_DIR%\debug\resource\ /y
if not exist %DST_DIR%\debug\resource\clock_os.png copy %SRC_DIR%\resource\clock_os.png %DST_DIR%\debug\resource\ /y
if not exist %DST_DIR%\debug\resource\eyeball.png copy %SRC_DIR%\resource\eyeball.png %DST_DIR%\debug\resource\ /y
if not exist %DST_DIR%\debug\resource\down.png copy %SRC_DIR%\resource\down.png %DST_DIR%\debug\resource\ /y

goto EXIT

rem ==================================================================
rem copy the library of release version
rem ==================================================================
:REL_VERSION
if not exist %DST_DIR%\release mkdir %DST_DIR%\release
if not exist %DST_DIR%\release\resource mkdir %DST_DIR%\release\resource
if not exist %DST_DIR%\data_store mkdir %DST_DIR%\data_store


if not exist %DST_DIR%\release\resource\dropdown.png copy %SRC_DIR%\resource\dropdown.png %DST_DIR%\release\resource\ /y
if not exist %DST_DIR%\release\resource\play.png copy %SRC_DIR%\resource\play.png %DST_DIR%\release\resource\ /y
if not exist %DST_DIR%\release\resource\stop.png copy %SRC_DIR%\resource\stop.png %DST_DIR%\release\resource\ /y
if not exist %DST_DIR%\release\resource\grab_default.png copy %SRC_DIR%\resource\grab_default.png %DST_DIR%\release\resource\ /y
if not exist %DST_DIR%\release\resource\grab_click.png copy %SRC_DIR%\resource\grab_click.png %DST_DIR%\release\resource\ /y
if not exist %DST_DIR%\release\resource\caliper.png copy %SRC_DIR%\resource\caliper.png %DST_DIR%\release\resource\ /y
if not exist %DST_DIR%\release\resource\mark.png copy %SRC_DIR%\resource\mark.png %DST_DIR%\release\resource\ /y
if not exist %DST_DIR%\release\resource\area.png copy %SRC_DIR%\resource\area.png %DST_DIR%\release\resource\ /y
if not exist %DST_DIR%\release\resource\eye_no.png copy %SRC_DIR%\resource\eye_no.png %DST_DIR%\release\resource\ /y
if not exist %DST_DIR%\release\resource\eye_yes.png copy %SRC_DIR%\resource\eye_yes.png %DST_DIR%\release\resource\ /y
if not exist %DST_DIR%\release\resource\comment.png copy %SRC_DIR%\resource\comment.png %DST_DIR%\release\resource\ /y
if not exist %DST_DIR%\release\resource\os_clock.png copy %SRC_DIR%\resource\os_clock.png %DST_DIR%\release\resource\ /y
if not exist %DST_DIR%\release\resource\od_clock.png copy %SRC_DIR%\resource\od_clock.png %DST_DIR%\release\resource\ /y
if not exist %DST_DIR%\release\resource\clock_od.png copy %SRC_DIR%\resource\clock_od.png %DST_DIR%\release\resource\ /y
if not exist %DST_DIR%\release\resource\clock_os.png copy %SRC_DIR%\resource\clock_os.png %DST_DIR%\release\resource\ /y
if not exist %DST_DIR%\release\resource\eyeball.png copy %SRC_DIR%\resource\eyeball.png %DST_DIR%\release\resource\ /y
if not exist %DST_DIR%\release\resource\down.png copy %SRC_DIR%\resource\down.png %DST_DIR%\release\resource\ /y

goto EXIT


rem ==================================================================
rem exit
rem ==================================================================
:EXIT

同时我们可以在pro文件中去调用cmd文件来拷贝文件,
这样点击构建就会执行cmd命令去拷贝资源文件到执行目录

win32 {
CONFIG( debug, debug|release) {
QMAKE_POST_LINK += $$win32Path("$$PWD/copyfiles.cmd") $$win32Path("$$PWD") $$win32Path("$$OUT_PWD") debug
} else {
QMAKE_POST_LINK += $$win32Path("$$PWD/copyfiles.cmd") $$win32Path("$$PWD") $$win32Path("$$OUT_PWD") release
}
}