Question

I'm new to QT Creator. I have added a .json file and a couple .txt files to my QT C++ Project and I can see the files in my project window. When I clean, run qmake and then build all, I'm expecting these files to be copied to my output directory, but they aren't there. I checked my .pro file and I do have the entries

OTHER_FILES += \ MyJsonFile.json \ MyTxtFile.txt \ ReadMe.txt (Note: each file name is on a separate line)

How do I get these files to copy to my output directory when I do my build? I want the program to have access to them as soon as it starts. I saw in the QT documentation that it said you can to Add New > General > Text File but the docs only say you can see the file in the project window. They do not explain how to get them to copy to your build and I can't find the answer anywhere. Im using QT Creator 3.0.1 based on QT 5.2.1 on a Windows 7 box. Thanks for any guidance.

EDIT: P.S. I am hoping that I can keep the json and text files out of the app so that I can change values in the files at runtime, or between program executions and just have the project pull the new values when it starts.

Était-ce utile?

La solution

The build and install directories are separate concepts. If you merely build, you should not expect anything to get actually installed.

In Qt Creator, when you have a qmake-based project, the default build setup invokes make. To install, you need to then invoke make install. This can be added as an additional build step in the project's "Build and Run" tab.

With qmake, there is no default location for the install directory, and no objects are declared to be installed, so make install is a no-op by default.

The files to be installed need to be:

  1. Set up as a qmake object.
  2. The object has to be added to the INSTALLS variable.

For example:

# Sets the default installation prefix to the build directory.
# This is the same default that Qt's configuration process uses.
!defined(PREFIX,var): PREFIX = $$OUT_PWD

MY_RESOURCES = \
  MyJsonFile.json \
  MyTxtFile.txt \
  ReadMe.txt

OTHER_SOURCES += $$MY_RESOURCES

# Object describing the resources to install
my_resources.path = $$PREFIX/share/myproject
my_resources.files = $$MY_RESOURCES

# The automatically generated object describing the target.
target.path = $$PREFIX/bin/myproject

# Declare my_resources and target objects to be installed.
INSTALLS += my_resources target

The user or package manager script is supposed to give the desired install prefix to qmake. A complete build done in an empty build directory (a so-called shadow build) would look like:

# on Unix
qmake "PREFIX=/usr/local" /sourcedir/myproject.pro
# on Windows (the installation into Program Files won't
# be done by make, but by an installer!)
qmake /sourcedir/myproject.pro
make
make install

Autres conseils

I think you need to use a resource file. Simply create a resource file and add your files to it. When you build your app you will always have those file within the executable. To be honest I don't know how work with QtCreator because I have always used eclipse with the plugin. I can suggest reading an article : Managing Resources

EDIT After my answer the OP told me that those file will be changed at runtime.. so my answer is clearly not what he want. Someone can tell me what to do with this answer? thanks.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top