Resources files (.qrc) not working
Hello,
I am porting a Qt3 Custom widget from qt3 to qt4, building a plugin for it. This custom widget uses 2 images, "butterfly.png" and "qtlogo.png":
Code:
MyCanvasView
::MyCanvasView(QWidget *parent,
const char *name
) : Q3CanvasView(parent,name) {
myCanvas = new Q3Canvas(800,600);
myCanvas->setBackgroundColor ( c );//
myCanvas->setAdvancePeriod(30);
myCanvas->setDoubleBuffering(true);
this->setCanvas(myCanvas);
imageRTTI = 984376;
mainCount = 0;
butterflyimg = 0;
logoimg = 0;
tb = 0;
tp = 0;
QString butterfly_fn
= ":/images/butterfly.png";
butterflyimg[0].load( butterfly_fn );
butterflyimg[1] = butterflyimg[0].scaled(
int(butterflyimg[0].width()*0.75),
int(butterflyimg[0].height()*0.75) );
butterflyimg[2] = butterflyimg[0].scaled(
int(butterflyimg[0].width()*0.5),
int(butterflyimg[0].height()*0.5) );
butterflyimg[3] = butterflyimg[0].scaled(
int(butterflyimg[0].width()*0.25),
int(butterflyimg[0].height()*0.25) );
QString logo_fn
= ":/images/qtlogo.png";
logoimg[0].load( logo_fn );
logoimg[1] = logoimg[0].scaled( int(logoimg[0].width()*0.75),
int(logoimg[0].height()*0.75) );
logoimg[2] = logoimg[0].scaled( int(logoimg[0].width()*0.5),
int(logoimg[0].height()*0.5) );
logoimg[3] = logoimg[0].scaled( int(logoimg[0].width()*0.25),
int(logoimg[0].height()*0.25) );
}
void MyCanvasView::addButterfly()
{
[B]Q3CanvasPolygonalItem* i = new ImageItem(butterflyimg[rand()%4],myCanvas);[/B]
i->move(rand()%(myCanvas->width()-butterflyimg->width()),
rand()%(myCanvas->height()-butterflyimg->height()));
i->setZ(rand()%256+250);
i->show();
}
. The image files are here: (project folder)/images/(image file)
The code of my resources.qrc file used in the plugin is:
Code:
<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file>images/butterfly.png</file>
<file>images/qtlogo.png</file>
</qresource>
</RCC>
The make file (.pro file from the plugin project) includes the line of the resource file:
Code:
RESOURCES = resources.qrc
The plugin compiles, the c. widget appears in qtdesigner whith its methods and everything, it works.
I make a new project using this widget and I run it. The slots from the c. widget that do not need any resources work perfectly. But when I attempt to call the slots who need resources (images) to run, the images are not shown at all.
Why the images, correctly defined in the resources fil, are not being shown?
:eek:
Re: Resources files (.qrc) not working
The only difference I see from what you have and what I did in something similar is:
Code:
<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix=""> <---------- here added prefix=""
<file>png/fit.png</file>
<file>png/normal.png</file>
<file>png/smaller.png</file>
<file>png/bigger.png</file>
<file>pics2.qm</file>
</qresource>
</RCC>
and in .pro file:
Code:
RESOURCES += xxxxx.qrc <------- +=
Re: Resources files (.qrc) not working
I have just tried that, it still doesn't work. The images are not displayed.
Thanks anyway.
Re: Resources files (.qrc) not working
try adding Q_INIT_RESOURCES(your_file).qrc to your main.cpp i dont remember if thats the right code, just check it in qt assistant
Re: Resources files (.qrc) not working
Thanks for the reply, but it is not solved yet...
I am going to try to explain the problem in more detail:
I have a qt4-ported qt3 custom widget, and it uses images in 2 slots (addButterfly() adds a butterfly.png image; addLogo() adds a logo.png image). The widget compiles perfectly and the slots that don't use any images run perfectly. The slots that use images do not work at all.
I just want to be able to use those slots of the custom widget (and consequently the images inside of it ) in a new project.
So I put the line :
Code:
Q_INIT_RESOURCE(resources);
in the main.cpp file of the project which uses the ported custom widget.
and I add to the same project the file qrc_resources.cpp of the custom widget, generating a .o object.
But the images of the Custom widget are not shown.
I also tried to compile the resource file (.qrc) separately (with rcc) but no changes...:(
Re: Resources files (.qrc) not working
What does butterflyimg[0].load( butterfly_fn ) return?
1 Attachment(s)
Re: Resources files (.qrc) not working
Thanks for your reply.
I put, in the main of a new project, the code for loading the image of the custom widget and it returned true...
Perhaps it will be better to post here the files of the ported custom widget.
the zip contains all the necessary files of the ported custom widget (custom widget + plugin).
If you try to make a new project which uses this widget, you will see that slots like addLine() work, but methods like addButterfly() don't work:
sample code for the slots call in mainwinimpl.cpp:
Code:
//this works
void MainWindowImpl::on_pushButton_pressed()
{
mycanvasview->addLine();
}
//not working
void MainWindowImpl::on_pushButton_2_pressed()
{
mycanvasview->addButterfly();
}
sample code for the resources initalization from main.cpp:
Code:
int main(int argc, char ** argv)
{
Q_INIT_RESOURCE(resources);
MainWindowImpl win;
win.show();
app.connect( &app, SIGNAL( lastWindowClosed() ), &app, SLOT( quit() ) );
return app.exec();
}
Thanks everyone. Your help here is absolutely priceless.
Re: Resources files (.qrc) not working
Quote:
Originally Posted by
degs2k4
I put, in the main of a new project, the code for loading the image of the custom widget and it returned true...
Did you saw the image after that?
Re: Resources files (.qrc) not working
Quote:
Did you saw the image after that?
No, I didn't.... :confused:
I think that the resource file is correctly added: I edited this line, adding an icon from the resource file:
Code:
QIcon MyCanvasViewPlugin
::icon() const {
return QIcon(":/images/qtlogo.png");
}
and this image appeared in QtDesigner on the WQidget Box next to the custom widget.
The load image function is also working fine, but calling the slot addButterfly() makes no effect when using the Q3CanvasPolygonalItem class.
UPDATE
the slot addButterfly() uses the class Q3CanvasPolygonalItem for displaying the image. I found this on the Internet:
Quote:
The Q3CanvasPolygonalItem class provides a polygonal canvas item on a Q3Canvas. More...
#include <Q3CanvasPolygonalItem>
This class is part of the Qt 3 support library. It is provided to keep old source code working. We strongly advise against using it in new code. See Porting to Qt 4 for more information.
Note to Qt Desktop Light Edition users: This class is only available in the Qt Desktop Edition.
Maybe I should use the class QAbstractGraphicsShapeItem instead?
Any suggerences?
Re: Resources files (.qrc) not working
Quote:
Originally Posted by
degs2k4
No, I didn't....
So maybe the resources work and the problem is in the code that displays the image?
What happens if you add:
Code:
l.
setPixmap( QPixmap( ":/images/butterfly.png" ) );
l.show();
just before return app.exec()?
Quote:
Originally Posted by
degs2k4
Maybe I should use the class QAbstractGraphicsShapeItem instead?
No, you have to use Q3CanvasItem subclass, if you want to stick with Q3Canvas.
Re: Resources files (.qrc) not working
Quote:
What happens if you add:
Qt Code:
Code:
l.
setPixmap( QPixmap( ":/images/butterfly.png" ) );
l.show();
just before return app.exec()?
If I do that, I can see the image !
So the problem is in addButterfly() slot then, right?
Re: Resources files (.qrc) not working
Quote:
Originally Posted by
degs2k4
So the problem is in addButterfly() slot then, right?
Try showing Q3CanvasPixmap instead of your custom item.
What does ImageItem contructor do with the second parameter?
Re: Resources files (.qrc) not working
Quote:
Originally Posted by
jacek
Try showing Q3CanvasPixmap instead of your custom item.
What does ImageItem contructor do with the second parameter?
Does this:
Code:
ImageItem
::ImageItem( QImage img, Q3Canvas
*canvas
) : Q3CanvasRectangle( canvas ), image(img)
{
setSize( image.width(), image.height() );
imageRTTI = 984376;
#if !defined(Q_WS_QWS)
pixmap.fromImage(image, Qt::OrderedAlphaDither);
#endif
}
Re: Resources files (.qrc) not working
Quote:
Originally Posted by
degs2k4
Does this:
This looks OK (except that image and pixmap variables esentially hold the same data).
How does drawShape() implementation look like?
Re: Resources files (.qrc) not working
Quote:
Originally Posted by
jacek
This looks OK (except that image and pixmap variables esentially hold the same data).
How does drawShape() implementation look like?
OK, the problem was with drawShape. drawShape() of ImageItem looked like this:
Code:
void ImageItem
::drawShape( QPainter &p
) {
// On Qt/Embedded, we can paint a QImage as fast as a QPixmap,
// but on other platforms, we need to use a QPixmap.
#if defined(Q_WS_QWS)
p.drawImage( int(x()), int(y()), image, 0, 0, -1, -1, OrderedAlphaDither );
#else
p.drawPixmap( int(x()), int(y()), pixmap );
#endif
}
And I changed it to this:
Code:
void ImageItem
::drawShape( QPainter &p
) {
// On Qt/Embedded, we can paint a QImage as fast as a QPixmap,
// but on other platforms, we need to use a QPixmap.
#if defined(Q_WS_WIN)
p.drawImage( int(x()), int(y()), image, 0, 0, -1, -1, Qt::OrderedAlphaDither );
#else
p.drawPixmap( int(x()), int(y()), pixmap );
#endif
}
And the butterfly was at last displayed !
So the problem is with Q_WS_QWS (maybe because it is from Qtopia?): used for detecting the type of windowing system that is used with Qt. Correspondingly there exist Q_WS_X11 and Q_WS_WIN that are set when compiling with Qt/X11 or Qt/Windows (and there's one
for mac in Qt3 versions).
Of course I can't just remove the macro that easily.... Why it didn't work when using Q_WS_QWS? It is OK to use Q_WS_X11, Q_WS_WIN... instead?
(I'm using windows...)
Re: Resources files (.qrc) not working
The problem is ImageItem::ImageItem(). It should be:
Code:
pixmap
= QPixmap::fromImage(image, Qt
::OrderedAlphaDither);
Re: Resources files (.qrc) not working [SOLVED]
Quote:
Originally Posted by
jacek
The problem is ImageItem::ImageItem(). It should be:
Code:
pixmap
= QPixmap::fromImage(image, Qt
::OrderedAlphaDither);
OH!!!! I see:
The Qt3 version was returning a BOOL ! :
Quote:
bool QPixmap::convertFromImage ( const QImage & image, ColorMode mode )
and the Qt4 version is returning the Pixmap which I have to grab explicitely !
Quote:
QPixmap QPixmap::fromImage ( const QImage & image, Qt::ImageConversionFlags flags = Qt::AutoColor )
THANKS!!!!!!!!!!!!!!!!!!!!!!!!!!!:)