PDA

View Full Version : [Qt 4.6.3] : pixmap resource for a label



didier
15th September 2010, 14:58
Hello everyone,


I wanted to associate a pixmap to a QLabel.
For that, I trusted Qt Designer.
I declared my pixmap to a qt resource file
and then have associated with my famous QLabel.

uic generates the following line:




stateIndicator-> setPixmap (QPixmap (QString:: fromUtf8 (":/icons/amber-on.ico")));


To me, it seems correct.

The compilation of the application goes smoothly.
Note: that the .cpp generated by rcc is well taken into account by the compilation chain.
However, the pixmap does not appear in place of the label.

Qt may dislike the ICO format. Never mind, I converted to PNG.
Still nothing but a construction error since each time the pixmap, QLabel:: pixmap () -> isNull () is true.

I give up the resource file to associate the file directly. Ico to my label.
And then I'll give you a thousand, it works!

However, I missed something?? ... Or is it a bug in Qt ? In any case, I saw nothing of this kind, reported.

tbscope
15th September 2010, 15:32
Surely the following is not correct:


": / icons / amber-on.ico "
It contains spaces where spaces should not be.

Is this copy an paste?

didier
15th September 2010, 15:38
Yes, it's formatting during copy/paste.
I'm correcting the message.

tbscope
15th September 2010, 15:52
One reason why a resource can not be found is because the prefix is wrong.

Can you post the contents of your resource file please?

Edit:
Another problem might be QString::fromUtf8
What happens if you leave that conversion out?

_Stefan
15th September 2010, 15:56
Did you build your imageformats plugins? If Qt cannot find them, loading the Pixmap will fail, leaving you with a isNull pixmap.
You can find the them in the QT_DIR\plugins\imageformats folder. For ICO, there should be a qico(d)4.dll or .a/.so whatever ;p

If you run the application stand-alone, you should include the plugin and add the "addLibraryPath" command in your app, so Qt can find the plugin.

didier
15th September 2010, 23:46
One reason why a resource can not be found is because the prefix is wrong.

Can you post the contents of your resource file please?


I guess it is not useful because it's managed by Qt Designer : qt resource file, prefixes in .ui file and so on.



Edit:
Another problem might be QString::fromUtf8
What happens if you leave that conversion out?


Same thing : the code is generated by uic. I've nevertheless tryed without but without success.

didier
16th September 2010, 17:26
I've taken the decision to track step-by-step what happens in Qt run-time in this context. So, I've linked my executable against Debug Qt libraries.
And I 've discovered the following :




bool QImageReader::read(QImage *image)
{
if (!image) {
qWarning("QImageReader::read: cannot read into null pointer");
return false;
}

if (!d->handler && !d->initHandler())
return false;

(d is QImageReaderPrivate* and d->handler is QImageIOHandler*)

d->handler being NULL pointer,

hence, this method returns false and so on until returning to my own code line :



stateIndicator->setPixmap(QPixmap(QString::fromUtf8(":/icons/green-off.png")));


Seems not to be a plugin problem since support of png format is native.

So, what's the problem ? (Hehe, cause you're a noob ! ;) )

Thank for your answer.

tbscope
16th September 2010, 17:54
Can you please post the contents of your resource file?
I think the prefix is wrong.

didier
16th September 2010, 19:08
Here it is :



<RCC>
<qresource prefix="icons">
<file>green-off.png</file>
</qresource>
</RCC>

didier
17th September 2010, 00:30
I've tried with a small example and it works : a dialog box with a label and an arbitrary png image. Nevertheless, I've tested it with Qt 4.6.2 on Ubuntu plateform (My project is under Qt 4.6.3 and Windows).
I have verified again the .qrc is well computed and linked with the target. And this is the case.
I foresee tracing execution in order to compare what it is done or not for concluding.

I keep you informed.

didier
17th September 2010, 01:02
Ok ! Found !
The reason is explained in "Qt Resource system" chapter.

If you have resources in a static library, you might need to force initialization of your resources by calling Q_INIT_RESOURCE() with the base name of the .qrc file. For example:

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Q_INIT_RESOURCE(graphlib);
...
return app.exec();
}

Thank alll for your help.
I hope my trouble will help you too.