How to Write and Read To/From a Qresource File in Qt 5

How to write and read to/from a QResource file in Qt 5?

The files saved in a qresource are read-only since they are part of the executable so you can not write or modify them.

docs:

Sample Image

Currently, Qt always stores the data directly in the executable, even on Windows, macOS, and iOS, where the operating system provides native support for resources. ...

C++ / Qt - Cannot Open .txt from qrc file

You cannot open a resource file for writing, as the content is embedded in the application binary. You have to open it readonly:

QFile file(":/txt/config");
if(!file.open(QIODevice::ReadOnly)) {
qDebug() << "error: " << file.errorString();
}

Qt Reources - Add and Read JSON file

First you need to call QFile::open() before calling readAll().

Second point, you can not write to file in Qt Resources.

If you want a cross platform way to save settings and such for your software take a look at QStandardPaths::writableLocation() and QSettings.

Note that QSettings won't handle JSON out of the box, but it will handle all the read/write to file for you (and the file format and location for you if you took car of setting QCoreApplication::applicationName and QCoreApplication::organizationName).

Qt Image from resource file

All this will work if your png files are located in the same folder as .pro, .qrc, and .cpp files of your project.

Usually it is convenient to put all images to special subfolder Resources, for example. Then in .qrc the line will look like:

<file>Resources/green.png</file>

And in .cpp file:

QImage *green = new QImage(":/Resources/green.png");

Can not read file with extern type and resource system

In Cannot open resource file, a first solution can be to run qmake again. 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.

So QFile should need (probably during the step of the initialization of the global variables) an initialization to build an internal map (name -> file).

If your initialization of 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 ...;
}

How write to the file continuously in Qt?

When the line outputFile.open(QIODevice::WriteOnly); opens the file, it replaces everything that was already in the file. Try replacing the line with:

outputFile.open(QIODevice::Append);

to open it in a mode that instead appends the data to whatever exists in the file.

Note that opening the file for every single line is an inefficient solution, especially if you have many, many lines to write. Opening the file once, then writing all the words to it before closing it, would work faster.

Qt resources files with CMake and AUTORCC

I think you need to link qrc_resources generated file.

I suppose you know the next info:

http://www.cmake.org/cmake/help/latest/manual/cmake-qt.7.html

Where you can see the next line:

add_executable(myexe main.cpp resource_file.qrc)

More info:

http://www.cmake.org/cmake/help/latest/prop_tgt/AUTORCC.html



Related Topics



Leave a reply



Submit