QML:如何指定相对于应用程序文件夹的图像文件路径

时间:2021-01-28 00:31:19

We are developing our first Qt/QML application (trying technology). While technology looks very promising at a glance, we have faced so many unexpected weird issues that almost give up at very beginning.

我们正在开发我们的第一个Qt / QML应用程序(尝试技术)。虽然技术看起来很有前景,但我们面临着许多意想不到的奇怪问题,这些问题几乎在一开始就放弃了。

Here one of such issue.

这里有一个这样的问题。

We want the following folders layout for our application:

我们希望应用程序具有以下文件夹布局:

--> ApplicationFolder
|--> qml        // QML files (also structured by subfolders)
|--> resources  // Application resources (images, sounds, data-files, etc.)
| |--> images   // Image resources (also structured by subfolders)
| |--> data     // Different data files
| |--> ...      // Other resources
|--> Application.exe   // Application executable
|--> *.dll             // DLLs application depends on

The problem is that in order to specify image file for Image QML item we have to use path relative to QML file?! This is absolutely insane. During development files sometimes moved between folders (you move QML file and have to fix all the path it has?!); some different QML files have to refer to same image (same image resource but different actual path in different QML files).

问题是,为了指定Image QML项目的图像文件,我们必须使用相对于QML文件的路径?!这绝对是疯了。在开发过程中,文件有时会在文件夹之间移动(你移动QML文件并且必须修复它拥有的所有路径?!);一些不同的QML文件必须引用相同的图像(相同的图像资源,但不同的QML文件中的实际路径不同)。

So the question is: how to specify image path relative to application folder? Is it possible at all?

所以问题是:如何指定相对于应用程序文件夹的图像路径?有可能吗?

Thanks in advance!

提前致谢!

PS. Using Qt's resource system (when resources are embedded into executable) is not an option in our case. We need raw resources on disk (including QML files itself, at least during development phase).

PS。在我们的案例中,使用Qt的资源系统(当资源嵌入到可执行文件中时)不是一个选项。我们需要磁盘上的原始资源(包括QML文件本身,至少在开发阶段)。

PPS. Wrote this question after spending a whole day to resolve the issue by myself via documentation/google/*; no success at all (most examples use resource embedding, others are too simple and just use relative paths).

PPS。在花了一整天的时间通过文档/ google / *解决了这个问题,写了这个问题;根本没有成功(大多数示例使用资源嵌入,其他示例太简单,只使用相对路径)。

2 个解决方案

#1


15  

If you can't use a .qrc file for your images, you could try this:

如果您不能为图像使用.qrc文件,可以尝试这样做:

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("applicationDirPath", QGuiApplication::applicationDirPath());
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

In QML, you'd then do:

在QML中,您可以:

Image {
    source: "file:///" + applicationDirPath + "/../resources/images/image.png"
}

Note that this code is not tested.

请注意,此代码未经过测试。

#2


1  

Image {
   source: "file:resources/images/image.png"
}

will work if the working directory is set to the ApplicationFolder

如果工作目录设置为ApplicationFolder将工作

note: when running from QtCreator, double check what directory the application is actually running in (i had my application run in my home directory even though the working directory was set correctly in the run configuration (after running it once in a terminal window this magically fixed itself) )

注意:从QtCreator运行时,请仔细检查应用程序实际运行的目录(我的应用程序在我的主目录中运行,即使在运行配置中正确设置了工作目录(在终端窗口中运行一次这个神奇地)固定自己))

#1


15  

If you can't use a .qrc file for your images, you could try this:

如果您不能为图像使用.qrc文件,可以尝试这样做:

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("applicationDirPath", QGuiApplication::applicationDirPath());
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

In QML, you'd then do:

在QML中,您可以:

Image {
    source: "file:///" + applicationDirPath + "/../resources/images/image.png"
}

Note that this code is not tested.

请注意,此代码未经过测试。

#2


1  

Image {
   source: "file:resources/images/image.png"
}

will work if the working directory is set to the ApplicationFolder

如果工作目录设置为ApplicationFolder将工作

note: when running from QtCreator, double check what directory the application is actually running in (i had my application run in my home directory even though the working directory was set correctly in the run configuration (after running it once in a terminal window this magically fixed itself) )

注意:从QtCreator运行时,请仔细检查应用程序实际运行的目录(我的应用程序在我的主目录中运行,即使在运行配置中正确设置了工作目录(在终端窗口中运行一次这个神奇地)固定自己))