How to create a shared library of just QML files - qt

I am working on project that will hold shared qml components such as a button, status bar, numeric key pad, etc. I want to be able to compile these qml files into one binary (.so/.dll) to provide for use for multiple applications. currently this is my folder structure for the shared application which is setup as a lib
SharedProject (folder structure)
- .pro file
- sharedassets.qrc
- sharedcontrols.qrc
- sharedassests (folder)
-.qml
-.png
- qmldir.txt
- sharedcontrols (folder)
- *.qml
-qmldir.txt
In my test application I am trying to get these controls to show up through a compile library or rcc file but I am unable to I have tried the following
QResoure::registerResoure() in the main.cpp of the test application
QML_IMPORT_PATH += dir of the SharedProject
QML2_IMPORT_PATH += dir of the SharedProject
(The above works but still requires me to have the qml files within some folder. I need them to be binary and then be able to utilize them in qt creator)
any help on how to properly put my qml into a complied library would be helpful. I have read the Qt modules tutorial but still feel lost on how to accomplish this. If anyone has an example that I can reference would be vary helpful.

Related

How to properly add a .qrc file to a QT 6 widget app with Cmake as building system?

I'm trying to use a .qrc file to store icon images for my QT application but the Resources.qrc file never appears in resource selection. I'm using CMake as building system and 6.3.1 QT version.
When simply adding Resource.qrc to source files with AUTORCC ON file never appears in project tree, when specifically written as project source in CMakeLists it appears in project tree, but never in resource selection when I'm trying to set ToolButton icon, no matter if I add prefix and images or not.
When AUTORCC is OFF and qt_add_resources(PROJECT_SOURCES Resources.qrc) added to CMakeLists, Resources.qrc file appears in CMake modules folder in a tree, but again, never in resource selection.
There are a lot of guides about .qrc when QMake is building system, but I hardly found anything about same thing with CMake, and none of them helped me, maybe I couldn't understand them properly. Sorry for possible mistakes, I'm new in programming and english isn't my native language. I'll be grateful for any help.

Generate doxygen documentation using Qt project file

How can I tell doxygen to (only) use the files that are in my Qt project file when generating the documentation?
Reason for asking: I'm using a folder structure where a 'common' folder holds files that are used by some (not all) Qt projects I'm working on. Like this:
projects
MyProject
common_files
SubProject_A
SubProject_B
Both SubProject folders will have a Doxyfile and both projects will use some files from the common_files folder.
I know one way to achieve my goal (getting the documentation for just the files used in my project) is to manually add all files in the INPUT setting from the doxygen configuration file. But that would mean I'll have to change the Doxyfile every time I add or delete a file to/from my project.
Doxygen should be able to filter the files needed for the documentation from the .pro file. But how?

Create and Use Dynamic Library with Resources in Qt

I have created a Dynamic Library using Qt Creator which I have included in another Qt Project but when I call functions with works with resource files for example reading the contents of a .txt in resources, it does not work.
Once I have the Build of the Dynamic Library which created the *.so, *.a and the qrc_resources.cpp files in another folder outside the library project folder, example:
Project Folder: /home/username/Qt Project/library/
Build Folder: /home/username/Qt Project/build-library/
I then copy the build files into the library project folder and from the test project (a Qt Application Project), I right click on the project -> Add Library -> External Library -> Select the .so file (from the library project folder which I just copied).
Then, after including the header files and calling a function which reads the contents of a file from the qrc resources, it does not work.
UPDATE
I used a QFile to check if the Resource File exists in which it does but I get this error: QIODevice::read (QFile, ":/file.txt"): device not open
Adding Libraries to Projects
later
I think need to create an external resource file.
So I was able to resolve the problem by using a QFile object instead of ifstream object to open and read the file.

Switching between qrc and local path for qml files

As far as i know, the qml files could be loaded from local directory path or could be bundled in qrc file and loaded with qrc:/ URI. In debugging phase changing local qml files doesn't need recompilation of qrc file and linking together with main executable, which is fast procedure for try and error fine tuning. But in deploy phase, the qml files should be bundled together as qrc file and link to main C++ Qt application. This is good practice when you want have single executable file, however compiling qrc file and linking it again is time consuming for big projects. is there any way we can switch to qrc or local directory? for example in debug and release mode?
There is many qml components inside the project and all of these are created by URI's like qrc:/componenentname.qml inside another qml files.
So is there any way to interchange these two states in debug and release modes, and keeping qml files without duplicate changes?
All URLs inside QML, if not specified in full, are relative to the current file.
E.g. if a QML file has content like this
Image {
source: "images/foo.png"
}
then the full URL of the image is constructed at runtime based on the base URL of the QML file itself.
I.e. if the QML file itself is qrc://main.qml then the resulting path is qrc://images/foo.png, if the QML file itself is file:///path/to/your/project/main.qml then the resulting image source is file:///path/to/your/project/image/foo.png.
So if you keep URLs relative in your usage inside QML, you can simply switch between resource and local files when loading the primary QML file.
QUrl mainFile = localMode ? QUrl::fromLocalFile("main.qml") : QUrl("qrc://main.qml")
QQuickView view;
view.setSource(mainFile);
Sorry for the old post,
What we did to handle this is use Qts resource search path. We used PySide2 so examples are in python
try:
QDir.addSearchPath('search_path_name', 'path/to/root/resources')
engine.load('search_path_name:/main.qml')
except FileNotFoundError:
try:
import qml # compiled resources using pyside resource compiler
QDir.addSearchPath('search_path_name', ':/')
engine.load('qrc:///main.qml')
except ModuleNotFoundError:
pass
From this point on resources are loaded using QFile("search_path_name:some_file.txt") and it will load from either search path.

Deploy QML application with localstorage plugin

I have deployed a QML application (static build on windows, following this how-to: http://qt-project.org/wiki/How-to-build-a-static-Qt-for-Windows-MinGW). However, the qml_import_trace (screenshot below) reveals that LocalStorage is loaded from the Qt/Static folder on the development computer, not from the release folder. Hence, when launched at another computer, the LocalStorage module is not found. How may the LocalStorage plugin/module be shipped with the application?
Including the following lines in the .pro files will give svg support. Am I only missing a qtplugin for sql/localstorage? In that case, what is the proper plugin name? Also, where can I find valid inputs for QTPLUGIN+= and QT+= ?
QTPLUGIN += qsvg
QT += svg sql
If I understand you correctly you want to copy the needed files to the release folder automatically.
Use the windeployqt.exe (in qt/bin folder) with --qmldir option. It will scan the given path for QML files and collect the QML components imported in those files.
A solution, although not optimal, was to manually copy the QtQuick/LocalStorage folder from the static folder into the release folder

Resources