Create and Use Dynamic Library with Resources in Qt - 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.

Related

How to create a shared library of just QML files

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.

How to automatically include Qt Linguist files into Qt resources for the CMake project?

I use CMake to build Qt project with internatiolization support. Using qt5_create_translation I can create *.ts files into the source directory and *.qm files into the build directory.
I want to reduce the number of files I should to distribute. Storing translation files :/translations/*.qm into the Qt resources is straitforward solution.
But during build process names of the resources are generated automatically and there is no native way to emplace them into the *.qrc file automatically.
How to achieve desired using, say, add_custom_command or something else?
Qt's qrc file is just xml files. Thus you can an external tool to add it.
A pure cmake solution could be:
Create a dummy resource file named #TRANS_FILE#
Add this file to your qrc file
Fetch the name of the translation file into a variable. Don't know how.
Use cmake's configure_file to replace the dummy resource name
Example:
set(TRANS_FILE ${NameOfTranslationFile})
configure_file(infile.qrc outfile.qrc #ONLY)

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

Qt including resource directory structure inside executable

I'm using QWebView to run a web app. There are 650+ files. Placing the web app's directory in the source directory does not result in the executable bundling the directory.
How do I include the entire web app directory so that the executable will be able to render the files.
Note: I have currently added index.html as a resource, and can access it with qrc:// - But since I cannot add the entire directory structure to a qrc (can I?), the executable does not include the other files.
You need to put an XML node into the .qrc file for each file you want to use using the Qt resource system.
This can be done using a simple pre-build script. Take a look at qrcgen. Quoting the blog post behind this link:
The script I created, qrcgen, takes a directory and a prefix, recursively scans the directory and generates a .qrc file with the same name as the directory scanned. It has solved my problem, and I hope it can help others. It is also available via PyPI, just "easy_install qrcgen".
In order to update the .qrc file whenever your directory contens change, you need to include this step into your build process:
For C++/Qt projects, you can add this step in the build configuration in QtCreator or add in your qmake file a system(...) statement. Note that such commands aren't portable in general. (If it's not portable, you can put some operating system conditions around multiple commands.)
For PyQt/PySide projects, I don't know how to do this, but I'm sure you find a solution for this too.

Get path of file in external library

I have 2 Qt projects that I've made. One builds a library the other builds a GUI that uses that library. My project with the library includes a bunch of shaders that I've made. Within my GUI I want to reference the path to these shaders. My approach is to have a static function in my library called getLibraryPath() that returns a QString referencing the path on my file system to the library.
I've tried using QDir::currentPath() and QApplication::applicationFilePath() and other similar functions that return paths but they all return paths referencing my GUI's path, not the library's path even though the function to get the path is located inside the library.
Within my GUI I try to reference a shader by doing: Shader->addShaderFromSourceFile(QGLShader::Vertex, MyExternalLib::getLibraryPath() + "/shaders/MyVertexShader.vs");
A library is just a bunch of code. It has no notion of having its own path. The normal way of doing what you intend to is:
If the library is a separately installed product -- set a registry entry in the installer, and check it at runtime. This will be platform specific, unless the library is written in Qt -- then you can use QSettings, but make sure that you set your company name, application, etc. appropriately as it's not done by QApplication for you anymore.
If the library is installed as a part of your application, in the same folder tree: use QCoreApplication::applicationDirPath() and add a relative path between the application executable and the files you want to this path.
I was able to solve this by adding the files I want to reference to a qrc file within my library project. By doing this, I could then reference the qrc path and the files inside of them easily.

Resources