Results 1 to 17 of 17

Thread: Resources files (.qrc) not working

  1. #1
    Join Date
    Jan 2008
    Posts
    26
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Question 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":

    Qt Code:
    1. MyCanvasView::MyCanvasView(QWidget *parent, const char *name )
    2. : Q3CanvasView(parent,name) {
    3.  
    4. myCanvas = new Q3Canvas(800,600);
    5. QColor c(0,0,255);//
    6. myCanvas->setBackgroundColor ( c );//
    7. myCanvas->setAdvancePeriod(30);
    8. myCanvas->setDoubleBuffering(true);
    9. this->setCanvas(myCanvas);
    10. imageRTTI = 984376;
    11. mainCount = 0;
    12. butterflyimg = 0;
    13. logoimg = 0;
    14. tb = 0;
    15. tp = 0;
    16. QString butterfly_fn = ":/images/butterfly.png";
    17. butterflyimg = new QImage[4];
    18. butterflyimg[0].load( butterfly_fn );
    19. butterflyimg[1] = butterflyimg[0].scaled(
    20. int(butterflyimg[0].width()*0.75),
    21. int(butterflyimg[0].height()*0.75) );
    22. butterflyimg[2] = butterflyimg[0].scaled(
    23. int(butterflyimg[0].width()*0.5),
    24. int(butterflyimg[0].height()*0.5) );
    25. butterflyimg[3] = butterflyimg[0].scaled(
    26. int(butterflyimg[0].width()*0.25),
    27. int(butterflyimg[0].height()*0.25) );
    28. QString logo_fn = ":/images/qtlogo.png";
    29. logoimg = new QImage[4];
    30. logoimg[0].load( logo_fn );
    31. logoimg[1] = logoimg[0].scaled( int(logoimg[0].width()*0.75),
    32. int(logoimg[0].height()*0.75) );
    33. logoimg[2] = logoimg[0].scaled( int(logoimg[0].width()*0.5),
    34. int(logoimg[0].height()*0.5) );
    35. logoimg[3] = logoimg[0].scaled( int(logoimg[0].width()*0.25),
    36. int(logoimg[0].height()*0.25) );
    37.  
    38. }
    39.  
    40. void MyCanvasView::addButterfly()
    41. {
    42. [B]Q3CanvasPolygonalItem* i = new ImageItem(butterflyimg[rand()%4],myCanvas);[/B]
    43. i->move(rand()%(myCanvas->width()-butterflyimg->width()),
    44. rand()%(myCanvas->height()-butterflyimg->height()));
    45. i->setZ(rand()%256+250);
    46. i->show();
    47. }
    To copy to clipboard, switch view to plain text mode 



    . The image files are here: (project folder)/images/(image file)

    The code of my resources.qrc file used in the plugin is:

    Qt Code:
    1. <!DOCTYPE RCC><RCC version="1.0">
    2. <qresource>
    3. <file>images/butterfly.png</file>
    4. <file>images/qtlogo.png</file>
    5. </qresource>
    6. </RCC>
    To copy to clipboard, switch view to plain text mode 

    The make file (.pro file from the plugin project) includes the line of the resource file:

    Qt Code:
    1. RESOURCES = resources.qrc
    To copy to clipboard, switch view to plain text mode 

    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?

    Last edited by degs2k4; 15th February 2008 at 06:20. Reason: remove unnecesary things

  2. #2
    Join Date
    Mar 2006
    Posts
    58
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Resources files (.qrc) not working

    The only difference I see from what you have and what I did in something similar is:
    Qt Code:
    1. <!DOCTYPE RCC><RCC version="1.0">
    2. <qresource prefix=""> <---------- here added prefix=""
    3. <file>png/fit.png</file>
    4. <file>png/normal.png</file>
    5. <file>png/smaller.png</file>
    6. <file>png/bigger.png</file>
    7. <file>pics2.qm</file>
    8. </qresource>
    9. </RCC>
    To copy to clipboard, switch view to plain text mode 
    and in .pro file:
    Qt Code:
    1. RESOURCES += xxxxx.qrc <------- +=
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2008
    Posts
    26
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Resources files (.qrc) not working

    I have just tried that, it still doesn't work. The images are not displayed.

    Thanks anyway.

  4. #4
    Join Date
    Jan 2008
    Posts
    33
    Thanks
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default 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

  5. #5
    Join Date
    Jan 2008
    Posts
    26
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy 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 :

    Qt Code:
    1. Q_INIT_RESOURCE(resources);
    To copy to clipboard, switch view to plain text mode 
    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...
    Last edited by degs2k4; 18th February 2008 at 23:26. Reason: reformatted to look better

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Resources files (.qrc) not working

    What does butterflyimg[0].load( butterfly_fn ) return?

  7. The following user says thank you to jacek for this useful post:

    degs2k4 (21st February 2008)

  8. #7
    Join Date
    Jan 2008
    Posts
    26
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy 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:

    Qt Code:
    1. //this works
    2. void MainWindowImpl::on_pushButton_pressed()
    3. {
    4. mycanvasview->addLine();
    5. }
    6.  
    7.  
    8. //not working
    9. void MainWindowImpl::on_pushButton_2_pressed()
    10. {
    11.  
    12. mycanvasview->addButterfly();
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 


    sample code for the resources initalization from main.cpp:

    Qt Code:
    1. int main(int argc, char ** argv)
    2. {
    3. Q_INIT_RESOURCE(resources);
    4. QApplication app( argc, argv );
    5. MainWindowImpl win;
    6. win.show();
    7. app.connect( &app, SIGNAL( lastWindowClosed() ), &app, SLOT( quit() ) );
    8. return app.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 

    Thanks everyone. Your help here is absolutely priceless.
    Attached Files Attached Files

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Resources files (.qrc) not working

    Quote Originally Posted by degs2k4 View Post
    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?

  10. The following user says thank you to jacek for this useful post:

    degs2k4 (21st February 2008)

  11. #9
    Join Date
    Jan 2008
    Posts
    26
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Re: Resources files (.qrc) not working

    Did you saw the image after that?
    No, I didn't....

    I think that the resource file is correctly added: I edited this line, adding an icon from the resource file:

    Qt Code:
    1. QIcon MyCanvasViewPlugin::icon() const
    2. {
    3. return QIcon(":/images/qtlogo.png");
    4. }
    To copy to clipboard, switch view to plain text mode 

    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:

    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?
    Last edited by degs2k4; 20th February 2008 at 12:52. Reason: updated contents

  12. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Resources files (.qrc) not working

    Quote Originally Posted by degs2k4 View Post
    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:
    Qt Code:
    1. l.setPixmap( QPixmap( ":/images/butterfly.png" ) );
    2. l.show();
    To copy to clipboard, switch view to plain text mode 
    just before return app.exec()?

    Quote Originally Posted by degs2k4 View Post
    Maybe I should use the class QAbstractGraphicsShapeItem instead?
    No, you have to use Q3CanvasItem subclass, if you want to stick with Q3Canvas.

  13. The following user says thank you to jacek for this useful post:

    degs2k4 (21st February 2008)

  14. #11
    Join Date
    Jan 2008
    Posts
    26
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Re: Resources files (.qrc) not working

    What happens if you add:
    Qt Code:
    Qt Code:
    1. l.setPixmap( QPixmap( ":/images/butterfly.png" ) );
    2. l.show();
    To copy to clipboard, switch view to plain text mode 
    just before return app.exec()?

    If I do that, I can see the image !

    So the problem is in addButterfly() slot then, right?
    Last edited by degs2k4; 20th February 2008 at 13:22. Reason: reformatted to look better

  15. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Resources files (.qrc) not working

    Quote Originally Posted by degs2k4 View Post
    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?

  16. The following user says thank you to jacek for this useful post:

    degs2k4 (21st February 2008)

  17. #13
    Join Date
    Jan 2008
    Posts
    26
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Re: Resources files (.qrc) not working

    Quote Originally Posted by jacek View Post
    Try showing Q3CanvasPixmap instead of your custom item.

    What does ImageItem contructor do with the second parameter?
    Does this:

    Qt Code:
    1. ImageItem::ImageItem( QImage img, Q3Canvas *canvas )
    2. : Q3CanvasRectangle( canvas ), image(img)
    3. {
    4. setSize( image.width(), image.height() );
    5. imageRTTI = 984376;
    6. #if !defined(Q_WS_QWS)
    7. pixmap.fromImage(image, Qt::OrderedAlphaDither);
    8. #endif
    9. }
    To copy to clipboard, switch view to plain text mode 

  18. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Resources files (.qrc) not working

    Quote Originally Posted by degs2k4 View Post
    Does this:
    This looks OK (except that image and pixmap variables esentially hold the same data).

    How does drawShape() implementation look like?

  19. The following user says thank you to jacek for this useful post:

    degs2k4 (21st February 2008)

  20. #15
    Join Date
    Jan 2008
    Posts
    26
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Red face Re: Resources files (.qrc) not working

    Quote Originally Posted by jacek View Post
    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:

    Qt Code:
    1. void ImageItem::drawShape( QPainter &p )
    2. {
    3. // On Qt/Embedded, we can paint a QImage as fast as a QPixmap,
    4. // but on other platforms, we need to use a QPixmap.
    5. #if defined(Q_WS_QWS)
    6. p.drawImage( int(x()), int(y()), image, 0, 0, -1, -1, OrderedAlphaDither );
    7. #else
    8. p.drawPixmap( int(x()), int(y()), pixmap );
    9. #endif
    10. }
    To copy to clipboard, switch view to plain text mode 

    And I changed it to this:

    Qt Code:
    1. void ImageItem::drawShape( QPainter &p )
    2. {
    3. // On Qt/Embedded, we can paint a QImage as fast as a QPixmap,
    4. // but on other platforms, we need to use a QPixmap.
    5. #if defined(Q_WS_WIN)
    6. p.drawImage( int(x()), int(y()), image, 0, 0, -1, -1, Qt::OrderedAlphaDither );
    7. #else
    8. p.drawPixmap( int(x()), int(y()), pixmap );
    9. #endif
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 

    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...)
    Last edited by degs2k4; 20th February 2008 at 23:30. Reason: updated contents

  21. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Resources files (.qrc) not working

    The problem is ImageItem::ImageItem(). It should be:
    Qt Code:
    1. pixmap = QPixmap::fromImage(image, Qt::OrderedAlphaDither);
    To copy to clipboard, switch view to plain text mode 

  22. The following user says thank you to jacek for this useful post:

    degs2k4 (21st February 2008)

  23. #17
    Join Date
    Jan 2008
    Posts
    26
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Smile Re: Resources files (.qrc) not working [SOLVED]

    Quote Originally Posted by jacek View Post
    The problem is ImageItem::ImageItem(). It should be:
    Qt Code:
    1. pixmap = QPixmap::fromImage(image, Qt::OrderedAlphaDither);
    To copy to clipboard, switch view to plain text mode 
    OH!!!! I see:

    The Qt3 version was returning a BOOL ! :
    bool QPixmap::convertFromImage ( const QImage & image, ColorMode mode )
    and the Qt4 version is returning the Pixmap which I have to grab explicitely !
    QPixmap QPixmap::fromImage ( const QImage & image, Qt::ImageConversionFlags flags = Qt::AutoColor )
    THANKS!!!!!!!!!!!!!!!!!!!!!!!!!!!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.