[Qt-interest] How to edit a file present in a resource at run-time ?

Gordon Schumacher gordon at rebit.com
Sun Nov 22 17:41:47 CET 2009


Andre Somers wrote:
> Louis Du Verdier wrote:
>   
>> Hello,
>>
>> For a project, I have to edit a file present in a resource, but at 
>> run-time.
>> I tried this code to test :
>>
>>     QFile f(":/Test.txt");
>>     if(!f.open(QIODevice::ReadWrite | QIODevice::Text))
>>     {
>>         QMessageBox::critical(this, "Test", "Not working...");
>>         return;
>>     }
>>     f.write(QString("OK !").toAscii());
>>     QTextEdit *t = new QTextEdit(f.readAll());
>>     t->show();
>>
>> But the messageBox tell me that the file cannot be open in the 
>> ReadWrite mode.
>> I took a look in QRessource but nothing speak about how to write into 
>> a file present in the ressource.
>> So, how to write into a file present in the ressource when the 
>> application is running ?
>>
>>     
> The answer is simple: you don't.
> The resource is compiled into the executable itself. You can not change 
> the executable you are running from that same executable. I hope my 
> anti-virus software would stop any program trying to do that...
>   

There's really a better way, as long as you don't object to having an
external blob... admittedly, it's also somewhat hackish.

Look up the QResource class - it will let you load an external binary
resource file.  Make sure you pass the -binary flag to rcc.  Now the
tricky part is editing the resource...

I'd suggest that one option would be to include in your project
rcc.cpp/h from Qt's src/tools/rcc directory.  Then you could let the
user edit the data, write it to a QTemporaryFile, and call
RCCResourceLibrary::output() against it.  As I said, hackish... but it
would work.



More information about the Qt-interest-old mailing list