如何从非Qt函数访问Qt资源数据 [英] How to access Qt resource data from non-Qt functions

查看:181
本文介绍了如何从非Qt函数访问Qt资源数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的理解,在Qt应用程序中打包非代码资源(如数据文件)的方法是使用资源系统。但是,如果我想使用非Qt函数来访问资源。例如,我可能有一个.txt或.csv文件与一些应用程序数据,我想使用ifstream访问。似乎不工作使用:...语法代替非Qt函数和类的文件名。是否有一个单独的工作流打包非应用程序中的非Qt函数使用的数据?

As I understand it, the way to packages non-code resources such as data files in a Qt app is using the resource system. However, what if I want to access a resource using a non-Qt function. For example, I may have a .txt or .csv file with some application data that I want to accessing using ifstream. It doesn't seem to work to use the ": ..." syntax in place of a filename for non-Qt functions and classes. Is there a separate workflow for packaging data used by non-Qt functions in an app?

我使用OSX,但我认为这些问题是平台无关的。 / p>

I'm using OSX, but I would assume these issues are platform independent.

推荐答案

Qt资源系统的唯一目的是在可执行文件中捆绑数据。

The sole purpose of the Qt resource system is to bundle data within the executable itself. If you wish not to integrate the data in the executable, then you simply must not use the resource system.

在mac上,如果你想添加data.txt文件,从项目源到您的应用程序包,但不是可执行文件本身,请将以下内容添加到您的 .pro 文件:

On mac, if you wish to add "data.txt" from project source to your application bundle, but not to the executable itself, add the following to your .pro file:

mac {
    BUNDLE = $$OUT_PWD/$$TARGET$$quote(.app)/Contents
    QMAKE_POST_LINK += ditto \"$$PWD/data.txt\" \"$$BUNDLE/Resources/\";
}


$ b < :applicationDirPath()用于获取到文件的路径:

Given the above project file, use the QCoreApplication::applicationDirPath() for a path useful in getting to the file:

#include <QCoreApplication>
#include <QFile>
#include <QDebug>

int main(int argc, char *argv[])
{
   QCoreApplication a(argc, argv);
   qDebug() << QCoreApplication::applicationDirPath();
   QFile data(QCoreApplication::applicationDirPath() + "/../Resources/data.txt");
   if (data.open(QIODevice::ReadOnly | QIODevice::Text))
      qDebug() << data.readAll();
   return 0;
}



在上面的例子中, Resources 文件夹与Qt资源系统无关。它仅仅是OS X应用程序包中的命名约定。我们在这里不使用Qt资源系统。

In the above example, the Resources folder has nothing to do with the Qt resource system. It's simply a naming convention in OS X application bundles. We're not using the Qt resource system here.

如果您希望使用Qt资源系统并直接访问资源数据,而不是通过 QFile QResource 类提供对可执行文件中捆绑的资源的访问。

If you wish to use the Qt resource system and access the resource data directly and not through a QFile, the QResource class provides access to resources that are bundled in the executable.

如果您控制的代码坚持使用 ifstream 用于数据输入,那么它是人为限制的,应该是固定的。它应该使用 istream ,因为该类可以由任何东西,不一定是一个文件。如果它是你不能控制的代码,你可以在 ifstream 5 / qlocalsocket.htmlrel =nofollow> QLocalSocket

If the code under your control insists on using ifstream for data input, then it's artificially limited and should be fixed. It should use istream instead, as that class can be backed by anything, not necessarily a file. If it's code that you don't control, you could set up the ifstream on a QLocalSocket.

常量 QResource :: data() 输入流流缓冲区。

如果资源 isCompressed(),则需要首先将其解压缩到临时区域。您还可以禁用资源压缩,以避免解压缩步骤。您可以使用 upx 等完整可执行的压缩程式,而不是在程式码执行时,所有内容都已解压缩,即可使用。

If the resource isCompressed(), then you need to first decompress it to a temporary area. You can also disable resource compression to avoid the decompression step. You can use a whole-executable compressor like upx instead - by the time your code runs, everything will be already decompressed and ready to use.

这篇关于如何从非Qt函数访问Qt资源数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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