Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QFile on resource file can not be written to
I'm unable to open a resource file for write. I can open the same file using its full path just fine, but not when the path is specified as a resource. AND YES, the file is specified in my qrc file and I can open it just file for read.
QString filename = QString::fromUtf8(":/dbase/myfile.xml");
if ( ! file.open(QFile::WriteOnly | QFile::Text))
Then I tried it using the full path and it worked just fine:
QString filename = QString::fromUtf8("/home/user/proj_root/dbase/myfile.xml");
if ( file.open(QFile::WriteOnly | QFile::Text))
So as a work around, I tried to use the "absolutePath" member of a resource to get the full path/filename. That didn't work either. The error message indicates that the resource path was not modified.
QString sTemp = QString::fromUtf8(":/dbase/myfile.xml");
QResource * rTemp = new QResource(sTemp);
filename = rTemp->absoluteFilePath();
if (! file.open(QFile::WriteOnly | QFile::Text))
Can somebody please help me understand what's going on? I'm using QT4.8.2 on a Linux-Fedora-16_x86_64 desktop.
MuldeR last edited by
As far as I know, files that are compiled into the executable using the Qt Resource system are read-only. You can't modify them, so any attempt to open such file for writing is going to fail. How should it be possible to modify resources compiled into the EXE file? Especially an executable can't modify itself while it is running. You'll have to re-compile the QRC file (using RCC) and then re-build the EXE file, if one of the resources has changed.
Note: You may use QResource to access the data of the resource directly - but again that memory area is read-only. So if you need to modify that data, you'll have to copy it into a your own memory buffer first. Of course this way all modifications will be lost as soon as the application terminates. Write the contents of the memory buffer into to a separate (none-resource) file, if you need to store it durably...
Peppy last edited by
I think resource files compiled into binaries are read-only. In your second example you have absolute path to file on your hard drive, not in your resources. And if I am right, there is no way how to do some work around.
Thanks for the replies people, I really appreciate it. So I need a way to find my root program location and perform my own run-time construction of the full path/filenames to my XML database files and NOT include the XML files in the resources. Right?
koahnig last edited by
[quote author="steveheflin" date="1346371275"]Thanks for the replies people, I really appreciate it. So I need a way to find my root program location and perform my own run-time construction of the full path/filenames to my XML database files and NOT include the XML files in the resources. Right? [/quote]
AFAIK the resource files must be read only as the others already stated. However, you may use it for initial reading and writing a new xml file somewhere.
The full path to the application shall be part of the runtime argument. argv should be the name of the application. Typically it contains already the full path to the application. However, there is also a method for "QApplication::applicationDirPath":http://qt-project.org/doc/qt-4.8/qcoreapplication.html#applicationDirPath . It is for sure more secure to use this.
You might want to think again. It is not a good idea to write things to the place of the executables. Most OS prevent this in the mean time. Typically today's OS have a scheme where to place the application data. You can also use "QSettings":http://qt-project.org/doc/qt-4.8/qsettings.html to store some runtime information.
twsimpson last edited by
"QDesktopServices":http://qt-project.org/doc/qt-4.8/qdesktopservices.html should help you out here, it gives you a way to retrieve the locations where certain kinds of data should be stored or retrieved from, including persistent application storage.
Hey thank to all, this is all good advice. Keeping my database files under the executable is really not a good idea.