How to deploy audio/video files together with iOS/Android app? Using Qt 5.3 - qt

I have learned that I can not bundle audio/video files within resource file in order to play them in my qml.
So, I have tried to use DEPLOYMENTFOLDERS in .pro file it does not copy the files.
It seems that QtCreator does not generate necessary code contents using DEPLOYMENTFOLDERS in .pri file.
Is there an easy way to say copy these files into build output location?
Visual studio C# has this option just saying one true/false to do this.
It should not be this hard. :-)

"Is there an easy way to say copy these files into build output location?" - this is a different task. You may achieve that with QMAKE_POST_LINK and writing a Makefile rule to copy the required files to the the output directory.
To copy the required file into the iOS bundle use QMAKE_BUNDLE_DATA. See https://qt-project.org/doc/qt-5-snapshot/platform-notes-ios.html
To copy the required file into the Andoid APK you need to write INSTALLS rules like that:
android {
...
assets.path = /assets
assets.files += LIST_OF_FILES
INSTALLS += assets
}

Related

Qt 5.2 Including External File into an Android Package?

How to include an external file into 'apk' ?
Example:
There is "123.txt" in the main directory where .pro file exists. What should I add to pro file to put "123.txt" into apk.
I tried DEPLOYMENT, DEPLOYMENTFOLDERS. But they only works with Symbian and Windows CE.
There are two ways to do it, both mentioned under "Porting an Existing Qt Application" on Qt 5.1 Documentation For Android.
Bundle them into a qrc file (works cross platform)
Add them to the "assets:" directory (Android specific)
For #2:
The "assets" directory will be created when you build the project. I have found it easiest to use the "INSTALLS" qmake variable to copy the files into the directory before it is packaged into an apk. The following is from a qmake file for a project I made. Note that for INSTALLS, the path to assets reads "/assets", not "assets" as you would expect. (It actually ends up in a subdirectory of the Android build workspace.)
To access the directory from the code in android, you use "assets:". (In the example, /assets/Samples ==> assets:/Samples.)
# - setup the correct location to install to and load from
android {
# android platform
# From: http://community.kde.org/Necessitas/Assets
SAMPLES_INSTALL_PATH=/assets/Samples
} else {
# other platforms
SAMPLES_INSTALL_PATH=$$OUT_PWD/Samples
}
# - setup the 'make install' step
samples.path = $$SAMPLES_INSTALL_PATH
samples.files += $$SAMPLE_FILES
samples.depends += FORCE
INSTALLS += samples
You can use the Qt Resource system. By default, all Qt applications can access the contents of a qrc file using the ":/" prefix or the URL scheme prefix, "qrc:".
The other approach is to deploy the resources into the package's assets directory. It is the best option if you want to achieve better interoperability with the Android APIs. You can access all resources in the directory using the "assets:" prefix. Unlike qrc, this approach is not a cross-platform solution.
When you build your project, a folder named "assets" is created in the Build-Directory/android-build/. After copying your files in the assets directory, you can add these to your pro:
deployment.files += MyFile1
deployment.files += MyFile2
...
deployment.path = /assets
INSTALLS += deployment
The files in assets are readonly. So you should first copy it to some other location if you want to change them:
QFile dfile("assets:/MyFile1");
if (dfile.exists())
{
dfile.copy("./MyFile1");
QFile::setPermissions("./MyFile1",QFile::WriteOwner | QFile::ReadOwner);
}
Specific to User2400925
In QT 5.1 I had used to copy the database from Assets folder to the home folder of the user, if the file does not exist. Which can be used by the App.
You may go through this link
One more simple way to do that:
1) Add this string into your .pro
ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android-sources
2) Create android-sources folder in your proj folder. Put anything you need into android-sources/assets/. You can also put there any other files, such as AndroidManifest.xml or android-sources/res/drawable/icon.png that you want to be copied and updated into the target bundle.
One more simple way to do that:
Add this string into your .pro
ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android-sources
Create android-sources folder in your proj folder. Put anything you need into android-sources/assets/. You can also put there any other files, such as AndroidManifest.xml or android-sources/res/drawable/icon.png that you want to be copied and updated into the target bundle.

How to access qt directories in .pro file?

I want to build qt application that will gather all the necessary binaries for standalone execution on install.
I know that can be done in lines of:
QT_DIR=C:/Qt/4.8.4
dlls_to_move.files += $$DIR/bin/QtCore.dll
however that seems clumsy. Is there a way to retrieve Qt binary folder actually used, like project directory that can be retrieved with $$PWD?
From qmake Advanced Usage:
The special $$[...] operator can be used to access various configuration options that were set when Qt was built:
So I think you'd want to do this in your project file:
dlls_to_move.files += $$[QT_INSTALL_BINS]/QtCore.dll

Copy file to destination directory in Qt Creator

I want to copy a data file from a directory in my source tree to the directory of the linked app so it's available at runtime, on Windows only. There appear to be two suggested techniques: use a post target dependency to issue a DOS copy command (Including resource files in Qt Creator build directory) or use an install step (Copy a file to the build directory after compiling project with Qt), but I cannot make either work in the way I would like.
The former requires me to use qmake path variables to generate my source and destination paths, but they contain backslash path separators, which the DOS copy command cannot handle.
The install solution forces other users of my project to set up a post build step in Qt Creator before it will work (one per configuration, in fact) and I would like to avoid this, as I want to make my project work with a default Qt Creator installation.
Is there any way to do this apparently simple task that can be wholly defined in the .pro file for the project? For example, is there a way to expand qmake path variables in a platform specific way?
Though these commands run ONLY after the executable is ACTUALLY linked, this solution doesn't require an external batch file. Note: this a Windows-only solution:
From our .pri file:
win32 {
...
# Copy the appropriate dll files into the target destination directory.
QMAKE_TBB_LIBDIR = $$quote($$PWD/MySource/MyLibs/$${PLATFORM_NAME}/vc9)
QMAKE_POST_LINK = copy /y $${replace(QMAKE_TBB_LIBDIR, /, \\)}\\*.dll > $${replace($$quote(DESTDIR), /, \\)}
...
}
This places a command in the Makefile that copies all the .dll files in MyLibs/x64 or MyLibs/Win32 into the destination directory.
However, if the executable did not need to be linked, then the .dlls are NOT copied.
The post build batch file would not have this limitation.

How to use qmake with two source files which have the same name?

My Qt project have two source files with the same name but in different folder.
The pro file is:
SOURCES = A/Test.cpp
SOURCES += B/Test.cpp
It can generate Visual Studio solution file via Qt Visual Studio addon, but it won't work because the generated object file have the same name: Test.obj.
That will cause LNK2001 unresolved external symbol because one of Test.obj is overwritten.
How to write proper pro file to deal with that?
Before Qt 5
You can try adding that line to your .pro file:
CONFIG += object_with_source
But as the option name implies, the .obj files will not be created in the out-of-source / "shadow build" directory.
Qt 5 and older
That option has been replaced by object_parallel_to_source in Qt 5, which should work with the shadow building.
You should consider splitting your solution in multiple projects, but it depends if each one of those folders could represent a project by its own.
If you choose this solution, you will have to write one .pro file per project. The usual way to go is to write a 'generic' *.pri file which is included from every *.pro file:
folder1.pro
TEMPLATE=lib
TARGET=folder1
include( ../common.pri )
folder2.pro
TEMPLATE=lib
TARGET=folder2
include( ../common.pri )
common.pri (in parent directory)
SOURCES += *.cpp
HEADERS += *.h
# etc.
Obviously the contents of each pro file depends on your solution.
If you don't want to split the source files in multiple projects, the easier solution would be to rename one the conflicting files, I guess.
I recently came across this issue, too. Splitting the project into subprojects made everything much more complicated, and -at least on my first attempt- flat-out didn't work. Then I tried CONFIG += object_with_source and CONFIG += object_parallel_to_source, but both did't seem to work with my Qt version.
So this is how I solved it (for Visual Studio 2010; I don't know if works the same with other versions):
If the project were an ordinary Visual Studio project, not one generated by QMake, you could solve it as described here: Visual Studio 2010 & 2008 can't handle source files with identical names in different folders? (changing the output dir of object files to a relative dir by appending %(RelativeDir) in the project settings "C/C++" > "Output Files" > "Object File Name").
Obviously, you don't want to do this by hand everytime you create a new Visual Studio project with QMake, so why not automatize it? After all Visual Studio project files are but ordinary XML files. Looking at the diff before and after setting the options reveals it's saved in a single unique tag called ObjectFileName.
So I wrote this Python script:
import sys
filename = sys.argv[1]
f = open(filename, "r", -1, "utf-8-sig")
lines = f.readlines()
f.close()
f = open(filename, "w", -1, "utf-8-sig")
for line in lines:
line = line.replace("</ObjectFileName>", "%(RelativeDir)\</ObjectFileName>")
f.write(line)
f.close()
..and use it like this in my bat-file that I always call to create the Visual Studio project:
qmake -tp vc myproject.pro
#cd ../scripts
unflatten_vcproj_obj_output.py "../src/myproject.vcxproj"
#pause
Not a beautiful solution, but it works.
One solution is to rename the files:
A/a_Test.cpp
B/b_Test.cpp
It's a bit ugly, but its simple and the class names can stay the same.

how to create header file and source file of a .ui file in Qtdesigner?

I have an application in which i have a mainwindow.ui file and i create a new designer file dialoge.ui in same application now how i can create source file and header file for dialoge.ui .I am using QtCreater(windows).
I am a beginner in qt , i think there should be a way for the same but i am not getting.
Help me.
Thanks
You can create source & header out of .ui file externally & then import them into your application(Dont forget to reference then in the .pro file.). Create a executable(.bat or .exe) to execute following commands. Below is the shell script(I am a linux user.)
echo HEADERS
<path to your uic compiler>/uic -o form.h form.ui
echo SOURCES
<path to your uic compiler>/uic -i form.h -o form.cpp form.ui
For each file somewidget.ui, during the build process ui_somewidget.h and ui_somewidget.cpp will be created. The tool used to generate them is uic.
All you have to make sure that the .ui files are added to the .pro file of your project, along with the other source and header files, like this:
FORMS += somewidget.ui
qmake/make will automatically generate and build the .cpp and .h files for somewidget.ui.
you can do this:
Save your dialoge.ui file in a directory.
Use qmake to create the .pro file (qmake -project), qmake is smart enough to detect the .ui.
This will also generate the appropriate makefile rules to invoke uic, Qt's user interface compiler.
If you are using visual studio, you can call qmake -tp vc, this will generate a visual studio project, linked and ready to use.
The visual studio project will generate a ui_dialoge.h for you, you can copy this to another project and use it in another header file which will use this dialoge.ui
I'm not sure I've completely understood your question. You have an application developed in QtCreator in which you have 2 .ui files. And you want to generate the corresponding header/source files to these 2 files.
Using QtCreator you don't need to worry about generating header files. This is done automatically. During the build phase, the User Interface Compiler (uic) is called and translates the .xml files into c++ header files.
Additionally to the already correct answers: change from Debug to Release mode in the general settings. In my case, the header file wasn't created in Debug mode

Resources