QResource::registerResource with rccData instead rccFileName
-
Hello
in my app I use QResource::registerResource in the rccFileName variant without problems.
However, I don't understand the rccData variant.
As far as I understood I can pass a RCC binary file from memory. How should that work?
A RCC file starts with "qres" followed by 0x00 4 times and then the data.
Is anything after the first 0 not ignored? How would the function know when the resource ends?
Can someone please explain this?
Thanks...
-
Thanks for your patience.
The RCC was created with
rcc --binary icons.qrc -o icons.rcccontains SVGs and works fine with the rccFileName variant.
QFile rcc("/tmp/icons.rcc"); if(rcc.open(QIODevice::ReadOnly)) { QByteArray ba = rcc.readAll(); rcc.close(); qDebug() << ba << QResource::registerResource(ba.constData()); } else { qDebug() << "RCC open failed"; }Result is the content of the RCC file and false.
@LazyT said in QResource::registerResource with rccData instead rccFileName:
QResource::registerResource(ba.constData());
registerResource() takes a
const uchar *. You're passing aconst char *which is not automatically convertible to aconst uchar *but instead implicitly converted into a QString because you did not define QT_NO_CAST_FROM_ASCII which you should to avoid such errors. -
Simply read the content of your resource into a QByteArray and pass QByteArray::constData() to registerResource(). The internal logic knows how long the resource data is.
-
Thanks for your answer, but this doesn't work.
For example:
QResource rcc(":/rcc/icons.rcc"); QByteArray ba = rcc.uncompressedData(); qDebug() << rcc.isValid() << QResource::registerResource(ba.constData());Result: true false
@LazyT said in QResource::registerResource with rccData instead rccFileName:
but this doesn't work.
Because you have to pass the content of the resource file - read it with QFile. It's meant to deal with external resource files.
-
Please show your code. How did you create the external resource file?
-
Thanks for your patience.
The RCC was created with
rcc --binary icons.qrc -o icons.rcccontains SVGs and works fine with the rccFileName variant.
QFile rcc("/tmp/icons.rcc"); if(rcc.open(QIODevice::ReadOnly)) { QByteArray ba = rcc.readAll(); rcc.close(); qDebug() << ba << QResource::registerResource(ba.constData()); } else { qDebug() << "RCC open failed"; }Result is the content of the RCC file and false.
-
Thanks for your patience.
The RCC was created with
rcc --binary icons.qrc -o icons.rcccontains SVGs and works fine with the rccFileName variant.
QFile rcc("/tmp/icons.rcc"); if(rcc.open(QIODevice::ReadOnly)) { QByteArray ba = rcc.readAll(); rcc.close(); qDebug() << ba << QResource::registerResource(ba.constData()); } else { qDebug() << "RCC open failed"; }Result is the content of the RCC file and false.
@LazyT said in QResource::registerResource with rccData instead rccFileName:
QResource::registerResource(ba.constData());
registerResource() takes a
const uchar *. You're passing aconst char *which is not automatically convertible to aconst uchar *but instead implicitly converted into a QString because you did not define QT_NO_CAST_FROM_ASCII which you should to avoid such errors. -
@LazyT said in QResource::registerResource with rccData instead rccFileName:
QResource::registerResource(ba.constData());
registerResource() takes a
const uchar *. You're passing aconst char *which is not automatically convertible to aconst uchar *but instead implicitly converted into a QString because you did not define QT_NO_CAST_FROM_ASCII which you should to avoid such errors.@Christian-Ehrlicher Wow!!
-
Hm,
DEFINES += QT_NO_CAST_FROM_ASCIIgives tons of "is private within this context" errors.
QResource::registerResource(reinterpret_cast<const uchar*>(ba.constData()));results in true, but icons are not available because "qt.svg: Cannot open file 'xxx', because: No such file or directory".
I give up for today.
Thanks for your hints.
-
Hm,
DEFINES += QT_NO_CAST_FROM_ASCIIgives tons of "is private within this context" errors.
QResource::registerResource(reinterpret_cast<const uchar*>(ba.constData()));results in true, but icons are not available because "qt.svg: Cannot open file 'xxx', because: No such file or directory".
I give up for today.
Thanks for your hints.
@LazyT said in QResource::registerResource with rccData instead rccFileName:
gives tons of "is private within this context" errors.
Because you have a lot of implicit casts from char* to QString which are for sure not what you want.
but icons are not available because
Works fine for me:
<RCC> <qresource prefix="/"> <file>stylesheet.qss</file> <file>icons/menuIndicatorChecked.png</file> <file>icons/menuIndicatorUnchecked.png</file> <file>icons/SimulationModeOnNormal.png</file> <file>icons/dashedline.png</file> </qresource> </RCC>int main(int argc, char *argv[]) { QApplication app(argc, argv); QFile f("style.rcc"); f.open(QIODevice::ReadOnly); auto ba = f.readAll(); qDebug() << ba.size(); qDebug() << QResource::registerResource(reinterpret_cast<const uchar*>(ba.constData())); QFile fRes(":/icons/menuIndicatorChecked.png"); qDebug() << fRes.exists() << fRes.isOpen() << fRes.readAll().size(); return 0;-->
25888
true
true true 274