12

I have a project I am working on with Qt Creator and I'm trying to get it to add my resource files to my build directories (output) automatically. I have the files in the project directory, but I don't know how to edit the *.pro file to make it include additional content files. (If that is even possible?)

Does anyone know how to get content files exactly as they are into my output directories?

EDIT:

Just so everyone knows exactly what I'm trying to do... I want to automatically copy FFmpeg as an executable into my build directories. That way if the build output does not exist, it will be copied over just before the application is debugged.

I'm trying to prevent clean operations from wiping out resources and me having to copy them back over again and again. Also... I work on multiple computers and use either SVN or Dropbox, so I want to keep my paths relative. They will change when I move from one computer to another.

FINAL ANSWER:

CONFIG(release, debug|release) {
    DESTDIR = release
} else {
    DESTDIR = debug
}

#for Windows
win32 {
    #in Windows, you can use & to separate commands
    copyfiles.commands += @echo NOW COPYING ADDITIONAL FILE(S) &
    copyfiles.commands += @call copy ..\\$${TARGET}\\ffmpeg.exe $${DESTDIR}\\ffmpeg.exe
}

#for Mac
macx {
     #commands would go here if I had them
}

QMAKE_EXTRA_TARGETS += copyfiles
POST_TARGETDEPS += copyfiles
jocull
  • 20,008
  • 22
  • 105
  • 149

3 Answers3

11

If you want to automatically copy files into a directory after the build you can use a post build target.

In your pro file:

win32 {
    copyfiles.commands = @call copy <from> <to>
}
macx {
    copyfiles.commands = cp <from> <to>
}
QMAKE_EXTRA_TARGETS += copyfiles
POST_TARGETDEPS += copyfiles

Instead of copy <from> <to> you can obviously call a script/batch file that does more post build steps.

WolfgangP
  • 3,195
  • 27
  • 37
  • Can and be relative? Do you have an example of that? – jocull Oct 04 '10 at 00:29
  • Of course. The executed command is just an ordinary shell command that's executed relative to your working directory. – WolfgangP Oct 04 '10 at 12:18
  • 1
    If you don't need the resources until deployment or packaging time, you can also add extra INSTALL rules to the .pro file. – andref Oct 04 '10 at 16:20
  • I've had trouble getting this to work. The command doesn't seem to know what directory it's in, and the slashes come out the wrong way from variables like $${OUT_PWD} which makes Windows confused. – jocull Oct 04 '10 at 21:33
  • Also, these commands seem to be ignored when compiling in Qt Creator. I tried setting copyfiles.target = FORCE, but that threw an error. – jocull Oct 04 '10 at 21:58
  • 1
    Sorry, there was a typo: It's copyfiles.command**s**! Take a look at this gist: https://gist.github.com/956d08bfa5236e1a4010. I made a micro project where you can try the post build. – WolfgangP Oct 05 '10 at 08:30
  • Ahh! I finally have it working (on Windows at least). I will edit my original post with the final answer. Thank you! – jocull Oct 05 '10 at 14:06
5

Check out the Qt Resource System.

To add the resource file into your project, you have add the following in your .pro file..

RESOURCES = application.qrc

Detailed examples available in the Qt Docs..

Once added into the pro file, a separate .qrc file will be added into your Qt project with which you can double click and add your desired resources like icons, translation files etc.,

Hope it helps..

Christophe Weis
  • 2,518
  • 4
  • 28
  • 32
liaK
  • 11,422
  • 11
  • 48
  • 73
  • Aren't these resources compiled into the output, though? I have things like external processes (*.exe files) that I need to be accessible in the same directory as my compiled output. – jocull Oct 04 '10 at 21:31
  • @jocull Yes, the problem is that those files can't be accessed directly when converted to resources. I have an XML file that I want to be able to modify. How did you actually solve this in a portable way? With different copy commands per operating system? – sakisk Mar 16 '11 at 13:14
  • Yeah, that's what I ended up having to do in the *.pro. Check out the original question, as I edited it with my final answer. – jocull Mar 16 '11 at 13:25
1

It's now 2012, and neither of these methods works for me. I get this error:

mingw32-make[1]: Leaving directory `C:/src/GL-Texture-build-desktop-Qt_in_PATH_Debug'
/usr/bin/sh: copy: command not found
mingw32-make[1]: *** [copyfiles] Error 127
mingw32-make: *** [debug] Error 2
00:28:19: The process "C:\QtSDK\mingw\bin\mingw32-make.exe" exited with code 2.
Error while building/deploying project GL-Texture (target: Desktop)
When executing step 'Make'

Looking at the message, I am amazed to see that is using the sh shell, probably because of mingw. Anyhow I solved this problem by using the unix equivalent:

CONFIG(release, debug|release) {
    DESTDIR = release
} else {
    DESTDIR = debug
}

#for Windows
win32 {
    copyfiles.commands += @echo NOW COPYING ADDITIONAL FILES &
    copyfiles.commands += cp -r ../$${TARGET}/textures $${DESTDIR}/
}

QMAKE_EXTRA_TARGETS += copyfiles
POST_TARGETDEPS += copyfiles

This happens on Windows 7 / QT 4.6.3.

David
  • 1,842
  • 2
  • 21
  • 31