How to replace qt.conf? - qt

I'm working on an embedded system. The directory /root/txpa/images/current/application/bin contains the file qt.conf, the content of which looks like this:
[Paths]
Prefix=/qtdir
Libraries=/qtdir/lib
/qtdir/lib/fonts is about the only thing in the path, and it contains several fonts of kind Vera. These are the only fonts in the system, and the app itself doesn't use them directly. Qt does.
I've been asked to remove qt.conf from its current directory, but if I do the app doesn't start (I suppose Qt can't find any fonts). How would i do this? I've read about Qt resource system, but I'm not sure how to go about doing this.

These are the steps I had to follow in order to resolve this problem:
First, create a qt_conf.qrc file that looks like this:
<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="/qt/etc/">
<file>qt.conf</file>
</qresource>
</RCC>
QLibraryInfo will load qt.conf from :/qt/etc/qt.conf using the resource system. That should explain the above qt_conf.qrc.
Second, copy the qt.conf file to the same directory as the qt_conf.qrc file (this can be changed, of course).
And finally, update the *.pro files, if any, and rebuild. The original qt.conf file can be removed from the directory that it was in.

Related

In Qt, use resources not located in the source directory

I need to use a set of resources from several different programs (images, fonts, txt files, etc). So I put them in a common folder. So I try to read one of these txt files using this path:
":/../../CommonClasses/PNGWriter/report_text/English"
However this does not work as the the QFile cannot be opened for reading with this path.
Hoewever if I move the report_text directory to the source directory and use this path:
":/report_text/English"
Then it all works just fine.
So my question is, is it possible to user resources not located in the source directory?
EDIT:
Here is my .qrc source file (and I replaced stuff.txt with an actual file from my resource file)
<RCC>
<qresource prefix="/">
<file>../../CommonClasses/PNGWriter/report_text/English</file>
<file>../../CommonClasses/PNGWriter/report_text/GothamBlackRegular.otf</file>
<file>../../CommonClasses/PNGWriter/report_text/GothamBold.otf</file>
<file>../../CommonClasses/PNGWriter/report_text/GothamBook.otf</file>
<file>../../CommonClasses/PNGWriter/report_text/GothamLight.otf</file>
<file>../../CommonClasses/PNGWriter/report_text/GothamMedium.otf</file>
<file>../../CommonClasses/PNGWriter/report_text/Spanish</file>
<file>../../CommonClasses/PNGWriter/report_text/viewmind.png</file>
</qresource>
</RCC>
The alias keyword is useful for giving things a different name in the resource system.
Instead of
<file>../../CommonClasses/PNGWriter/report_text/viewmind.png</file>
you'd write
<file alias="report_text/viewmind.png">../../CommonClasses/PNGWriter/report_text/viewmind.png</file>
Of course, this is bit of a pain if you're manually maintaining large qrc files; it may be useful to automate (script) production of them.
Thank to the friendly tip #timday, I've managed to see what the problem is. The ../../ that I used were the problem. The path to the file was actually:
:/CommonClasses/PNGWriter/report_text/English
Now it works just as expected!! I hope this helps anyone else with this problem!
I don't believe this is a native option, as when I try to add files above the project sub-directory I get "The file "/path/to/file/" is not in a subdirectory of the resource file. You now have the option to copy this file to a valid location.", with the copy option.
However, (if on linux) adding a symbolic link to the resource file in the project subdirectory works perfectly fine for me. So something like ln -s /path/to/target/resource /path/to/project/directory/resource_name.file, and then adding resource_name.file to your resources file should work. It does on mine (Qt 5.7).

How to change all path of resource file on Qt?

I draw GUI for the app and use many icons with resource file path
prefix: /ico
and path file in the folder of the project:
Resources/Images/*.png
So, each uses them in GUI, I must call::/ico/Resources/Images/*.png
Now, I want to call them with a short path such as ico/*.png
And GUI used many resources, I need change resource path many times.
UPDATE:
resource file:
<RCC>
<qresource prefix="/ico">
<file>Resources/Images/ic_add.png</file>
<file>Resources/Images/ic_add_click.png</file>
<file>Resources/Images/ic_add_disable.png</file>
<file>Resources/Images/ic_add_hover.png</file>
<file>Resources/Images/ic_arrow.png</file>
<file>Resources/Images/ic_arrow_collapse.png</file>
And in ui file is using this path many times and many where.
I think that I can't change step by step anywhere.
From doc, You would use alias attribute of file tag:
<file alias="cut-img.png">images/cut.png</file>
The file is then accessible as :/cut-img.png from the application. It is also possible to specify a path prefix for all files in the .qrc file using the qresource tag's prefix attribute:
<qresource prefix="/myresources">
<file alias="cut-img.png">images/cut.png</file>
</qresource>
In this case, the file is accessible as :/myresources/cut-img.png.

Where to deploy external resources/files?

within my Qt5 application a file ":items/cube.obj" is accessed (.obj is a 3D format and this comes from a piece of example code).
Where within my project/Qt installation path do I have to deploy this file "cube.obj" to let it work with this funny path name?
Thanks!
From docs:
By default, resources are accessible in the application under the same
file name as they have in the source tree, with a :/ prefix, or by a
URL with a qrc scheme.
Note: forward slash in :/.
If you don't use prefix in .qrc, it would be in items directory next to .pro:
/path/to/project/myproject.pro
/path/to/project/items/cube.obj
In this case root prefix is used.
If using non-root prefix, .qrc could be:
<qresource prefix="/items">
<file>cube.obj</file>
</qresource>
and files structure:
/path/to/project/myproject.pro
/path/to/project/cube.obj
Using alias:
<qresource prefix="/items">
<file alias="cube.obj">items/cube.obj</file>
</qresource>
and files structure:
/path/to/project/myproject.pro
/path/to/project/items/cube.obj

How can I organize files under the qml.qrc folder in Qt Creator?

If I have a bunch of resources (images, fonts, etc.) in different folders under my qml.qrc file, is there a way to organize this within Qt Creator?
For example, if I have the following in my qml.qrc file:
<RCC>
<qresource prefix="/">
<file>main.qml</file>
<file>pages/MainPage.qml</file>
<file>pages/NewContactPage.qml</file>
<file>images/plus.png</file>
<file>images/minus.png</file>
<file>images/exit.png</file>
</qresource>
</RCC>
It will show up as a long list in Qt Creator, like this:
Resources
qml.qrc
/
main.qml
pages/MainPage.qml
pages/NewContactPage.qml
images/plus.png
images/minus.png
images/exit.png
Since this list can get really long over the duration of the project, it would be nice if these were organized better and split into folders like they are in my directory. Any ideas?
Actually, I'd highly recommend that non .qml assets to be put in a different resource file altogether, because large files will gut application build times. What happens is even a tiny change to a qml source will result in recompilation of the entire resource file. If assets are in a different resource file they are not continuously recompiled.
This will also effectively achieve organization in addition to significantly improving build times.
I just discovered an awesome way to do it. What's weird is that nobody else suggested it, when it's so completely trivial. Perhaps it didn't work in old versions of Qt/Qt Creator but now it does.
Here it is:
<RCC>
<qresource prefix="/">
<file>main.qml</file>
<file>test/test.txt</file>
</qresource>
</RCC>
The test dir needs to exist and needs to contain test.txt.
No need for creating separate <qresource> tags with different prefixes. No need for alias attributes.
The files are cleanly organized in the filesystem and in the project explorer and you can access them from code with clean paths like :/test/test.txt.
(this screenshot is of a project that has some extra files as well - ignore those)
Bonus: You can rightclick on the "test" folder in the project explorer in Qt Creator and choose "Add new...", this does put the newly created file in the right place in the filesystem. Unfortunately it doesn't appear in the qrc subtree in the project explorer, only in a separate "Other files" subtree. You need to rightclick "qrc.qml" in the project explorer and choose "Add existing files" to make the file appear in the qrc subtree like it should. So it's a bit buggy/messy but when you learn how to use it, it's workable.
Bonus 2: You can import (add) an existing file/dir (which reside in any (sub-)sub-dir of the qrc file) and the right XML syntax will be generated, resulting in the right tree structure in the project explorer.
What I think doesn't work well:
Creating a file from Qt Creator from File -> New file or project (or Ctrl-N). This doesn't let you put the file in an arbitrary dir in the filesystem, only in the root project dir.
Files that you've put in subdirs aren't included in Qt Creator's project-wide search (Ctrl+Shift+F).
Edit: I just noticed the OP is doing exactly what I suggest. In that case, he probably is using an older Qt Creator version. Mine is 4.1.0.
If you want to use qrc files but don't like paths like "images/icons/images/icons/icon.png/" use alias as described here
<qresource prefix="/images">
<file alias="cut.png">images/cut.png</file>
</qresource>
With alias you can use your file by neatly writing /images/cut-img.png instead of /images/images/cut.png
From the Qt documentation: The Qt Resource System
By default, resources are accessible in the application under the same file name as they have in the source tree, with a :/ prefix, or by a URL with a qrc scheme.
It is also possible to specify a path prefix for all files in the .qrc file using the qresource tag's prefix attribute:
this example show how to do it:
<RCC>
<qresource prefix="/pages">
<file >pages/MainPage.qml</file>
</qresource>
<qresource prefix="/images">
<file >images/plus.png</file>
</qresource>
</RCC>
Another nice way to view your project files / folders as they appear on your File System is to do this:
Open your project
Click on the drop down menu which is above your project name, as demonstrated in the image below:
Done, now you can see your files and folders as they appear on your FS

How to convert .ui(user interface) files into .exe file in QT

I am a fresher in qt,i don't have much knowledge on qt, but i created some of file in qt for my application (regaurding to GUI format).I was created some .ui files in qt,but i wanted these files into .exe format.I think u had unerstand my problem,so please help me
uic (sometimes installed as uic-4) takes the .ui files and generates a C++ header file that you can inherit from. There are a few different ways you can work with the .ui files. See the manual for more information. Feel free to come back with specific questions.
Hallo Ram,
I think you are asking about the inclusion of .ui files within your .exe file.
If I am not wrong, then you need to include you .ui file within your projects specific resource file. It will be usually named .qrc in Qt projects.
The contents of .qrc file will look something like this:
<RCC>
<qresource prefix="/ui">
<file>ui/command/spiwidget.ui</file>
<file>ui/command/SPIMicroCommandWidget.ui</file>
<file>ui/command/utility/externdatawidget.ui</file>
<file>ui/sequencerwidget.ui</file>
<file>ui/command/watchdogwidget.ui</file>
<file>ui/command/utility/repdatawidget.ui</file>
<file>ui/command/core.png</file>
<file>ui/command/LastOpenedFiles.ui</file>
</qresource>
</RCC>
In the code above, you can see the inclusions for .ui and .png(image file) too.
After including it in .qrc file, you can use this resource in your .cpp code as follows:
QFile file(":ui/ui/command/LastOpenedFiles.ui");
Where :ui/ui/command is the path of to the .ui file being used.
Hope this explanation is useful to you!
Try using QtCreator (official IDE for Qt development). One way to use your *.ui file would bet to:
create *.h and *.cpp files containing a class that will load your widget structure.
add your new files to qt project file - *.pro
If your haven't used Qt Creator yet, then I suggest try it.
Create new project (ctrl+n) - Qt C++ Project / Qt Gui Application
Add new form to your project (ctrl+n) - Qt / Qt Designer Form Class
Look at files that where created by IDE. There is *.h file, *.cpp file and *.ui file.
Look into *.pro file, there are 3 sections SOURCES, HEADERS, FORMS
Here are some learning materials:
http://qt.nokia.com/services-partners/qt-in-education/qt-in-education-course-material

Resources