QML:如何指定相对于应用程序文件夹的图像文件路径 [英] QML: how to specify image file path relative to application folder

查看:341
本文介绍了QML:如何指定相对于应用程序文件夹的图像文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在开发我们的第一个 Qt/QML 应用程序(尝试技术).虽然技术乍一看非常有前途,但我们遇到了很多意想不到的奇怪问题,一开始就几乎放弃了.

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.

这里有一个这样的问题.

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

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

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).

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

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

提前致谢!

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

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).

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

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

推荐答案

如果你不能为你的图片使用 .qrc 文件,你可以试试这个:

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

#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();
}

在 QML 中,你会这样做:

In QML, you'd then do:

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

请注意,此代码未经测试.

Note that this code is not tested.

这篇关于QML:如何指定相对于应用程序文件夹的图像文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆