Get path of file in external library - qt

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.

Related

How do I split out QML files embedded within a DLL?

I have QML file that has been embedded into a dll. I think it was done something like this
How can I embed a Qt resource into a .dll file?
(The second answer).
Is there anyway to split out the QML file to obtain the source code? I am not very familiar with QT framework
If it's embedded via *.qrc, then it's NOT compatible with standard windows/linux (.dll/.so) resource formats. qrc is compiled as xxx_qrc.cpp file and embedded by linker as .obj file with static initialization code. I.e. it's just part of the binary. You can access "contents" of qrc via QFile with "qrc:/." URL. But for that, you have to load DLL with resources embedded in current process, because qrc is hooked up in static initialization (aka DllMain in Windows). Something like:
QLibrary lib("./library.dll");
if (!lib.load())
throw exception(lib.errorString().toStdString());
QFile resource(":/resource.qml");
if (!resource.open(QIODevice::ReadOnly))
throw exception(resource.errorString().toStdString());
resource.copy("./exported.qml");
To explore currently loaded virtual qrc file system tree, you can use QDir(":/"). I guess it's pretty easy to figure out the rest from here.
And of course - you have to be aware what sort of DLLs you are loading into your process, as they may contain arbitrary code that will be executed as you call QLibrary::load!

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.

Path to the project current dir in qt

I want to get a path to the project directory in Qt and reference the other files of my project relative to it. I know this issue have been already discussed here
Get current working directory in a Qt application
and here
How to get current working directory path of a Qt application?
but the answer is still unknown. In case it's impossible to get such a path then what is the approach of navigation among files of the Qt project.
Based on your comment, you are trying to:
Access some images which are being used in my program. I could of course put them into build target directory, but it becomes uncomfortable to pass my code to others.
The approach to store resource files in the project source directory and rely on such structure at runtime is not a greatest idea. I can't imagine the situation when mixing the concepts of initially decoupled source and build directories could be useful, correct me if I'm wrong.
So, according to your needs:
The most simple and plain way is to use the Qt resource system. The resource files are simply embedded into the executable, so there will be no need to access the file system.
Another way is the automatic deployment of the needed files. This answer describes the way to copy your files to the target directory using qmake.

How to prevent Qt from linking

I have a bigger project including code in different languages. One part of it is written in C++/QML. The final linkage is performed via DMD. I need qmake to generate only object files without linking. Currently qmake tries to link a library, what fails with undefined reference.
Is there a way to make qmake generate only object files without linking?
Simply change the project type to the lib template, and add CONFIG += staticlib. The static library is simply a single file archive of all your objects. DMD can then link it to the final executable.

Cannot find correct file called qtnetwork4.dll

Guys I've written app in qt and when trying to run it (double click on *.exe) I'm getting error that this application cannot start because qtnetwork4.dll isn't available. I've searched my drive and I found few files with this name (and copied one by one every time trying to run my app) but none of them seems to work. How can I solve it?
place into your *.exe - folder appropriate dll-s from %QT_PATH%\bin\
There is an app called Dependency Walker that will let you know what functions in qtnetwork4.dll are being used in your program. It can be found at the following:
http://www.dependencywalker.com/
All of the DLLs your program use will be from the same installation as the qmake.exe that is used to compile the program. If you are using Qt Creator you can see where the qmake.exe your using is located by going to:
tools->options->Qt4
Get the DLLs that are located at the path displayed here to make sure they will work with your program.
On windows, DLL are looked for in folder where executable lies, then using "PATH" environenment variable.
In your case, you want to put your dll along your executable, taking them from the sdk you're using to compile
The following page provides a lot of additional information on the subject (example, plugins, strategy for building installers)
To force using QtNetwork and linking it, you have to put
QT += network
in your poject file.
If you're sure you're not using it, you can use
QT -= network

Resources