简体   繁体   English

无法读取具有外部类型和资源系统的文件

[英]Can not read file with extern type and resource system

I want to read a json file via Qt resource system in a c++ class.我想通过 C++ 类中的 Qt 资源系统读取 json 文件。

Assume Translator.h :假设Translator.h

    class Translator
    {
    public:
        Translator();
    
        void read(const QString &fpath);
        QString valueAt(const QString &key) const;
    
    };
    
    extern Translator _tr;

    inline QString Tr(const QSTring &key) {
        return _tr.valueAt(key);
}

And Translator.cpp :Translator.cpp

Translator::Translator() {
    read(":/Resources/Text/Translation.json");
}

void Translator::read(const QString &fpath) {
    QFile f(fpath);
    f.open(QIODevice::ReadOnly | QIODevice::Text);
    f.readAll(); // f.errorString() -> no such file or directory
    f.close();
}

Translator _tr;

And also .qrc file: .qrc:还有.qrc文件:.qrc:

<RCC version="1.0">
    <qresource>
    <file>Resources/Text/Translation.json</file>
    </qresource>
</RCC>

Every time i run above code i get error :每次我运行上面的代码时,都会出现错误:

QIODevice::read (QFile, ":\\Resources\\Text\\Translation.json"): device not open

However when i remove extern Translator object and create that in main function or when replace the path "Resources/Text/Translation.json" with full path there is no more error但是,当我删除 extern Translator对象并在主函数中创建该对象时,或者将路径“Resources/Text/Translation.json”替换为完整路径时,不再有错误

Note:笔记:

might be good to mention that i want to read Translator.json one time in my whole application and for that i create an extern Translator值得一提的是,我想在整个应用程序中一次阅读Translator.json ,为此我创建了一个extern Translator

In Cannot open resource file , a first solution can be to run qmake again.无法打开资源文件中,第一个解决方案可以是再次运行 qmake。 It is maybe likely to change the link edition with new dependency information.可能会使用新的依赖信息更改链接版本。

Here is a possible explanation.这是一个可能的解释。 Qt cannot disambiguate ":\\Resources\\Text\\Translation.json" before it reads the content of the .qrc even if the .qrc is compiled in the executable.即使.qrc是在可执行文件中编译的,Qt 在读取.qrc的内容之前也无法消除“:\\Resources\\Text\\Translation.json”的歧义。

So QFile should need (probably during the step of the initialization of the global variables) an initialization to build an internal map (name -> file).所以QFile应该需要(可能在全局变量的初始化步骤中)一个初始化来构建一个内部映射(名称 -> 文件)。

If your initialization of Translator _tr;如果你初始化Translator _tr; occurs before this map initialization, you have the error.在此地图初始化之前发生,您有错误。 If it occurs after, things should work.如果它发生在之后,事情应该会奏效。

Another solution could be另一种解决方案可能是

class Translator
{
public:
    Translator();

    void read(const QString &fpath);
    QString valueAt(const QString &key) const;
private:
    bool m_ready;
};

Translator::Translator() : m_ready(false) {}

void Translator::read(const QString &fpath) {
    QFile f(fpath);
    f.open(QIODevice::ReadOnly | QIODevice::Text);
    f.readAll(); // f.errorString() -> no such file or directory
    f.close();
}

QString Translator::valueAt(const QString &key) const {
    if (!m_ready) {
      m_ready = true;
      read(":/Resources/Text/Translation.json");
    }
    ...
    return ...;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM