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:
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
"_Gfortran_Pow_C8_I4" Error When Linking .O Files from G++ and Gfortran Using G++
Strange Behavior with Constexpr Static Member Variable
C++ When Should We Prefer to Use a Two Chained Static_Cast Over Reinterpret_Cast
Forcing String to Int Function to Consume Entire String
How Do C++ Progs Get Their Return Value, When a Return Is Not Specified in the Function
C++ CSV Line with Commas and Strings Within Double Quotes
Elegant Way to Implement Extensible Factories in C++
Changing R's Seed from Rcpp to Guarantee Reproducibility
How to Override Compile Command of Visual Studio 2017 Community
How to Build a Program Using C++ Driver of Mongodb
How to Use a Boost Condition Variable to Wait for a Thread to Complete Processing
Using an Union (Encapsulated in a Struct) to Bypass Conversions for Neon Data Types
Why Do C++ Template Definitions Need to Be in the Header
If a 32-Bit Integer Overflows, How to Use a 40-Bit Structure Instead of a 64-Bit Long One