Qt - draw pixmap (.png file) with QPainter from resources -
i have problems drawing image on qwidget qpainter resources. i'm sure i'm missing dont know what. if use absolute path, works fine.
so question is: should if want draw .png file resources qpainter? (what missing?)
here simple test code:
widget.h:
#ifndef widget_h #define widget_h #include <qwidget> #include <qpaintevent> #include <qpixmap> #include <qpainter> class widget : public qwidget { q_object public: widget(qwidget *parent = 0); protected: void paintevent(qpaintevent* e); }; #endif // widget_h
widget.cpp:
#include "widget.h" widget::widget(qwidget *parent): qwidget(parent) { } void widget::paintevent(qpaintevent *e) { qpainter painter(this); qpixmap pixmap1("c:/qt/projects/pixmaptest/image.png"); qpixmap pixmap2(":/img/image.png"); qpixmap pixmap3("qrc:/img/image.png"); painter.drawpixmap(10,10,50,50, pixmap1); // works painter.drawpixmap(10,70,50,50, pixmap2); // not painter.drawpixmap(10,130,50,50, pixmap3); // neither }
img.qrc file:
<rcc> <qresource prefix="/img"> <file>image.png</file> </qresource> </rcc>
and .pro file:
#------------------------------------------------- # # project created qtcreator 2015-04-01t17:11:38 # #------------------------------------------------- qt += core gui greaterthan(qt_major_version, 4): qt += widgets target = pixmaptest template = app sources += main.cpp\ widget.cpp headers += widget.h forms += resources += \ img.qrc
as expected, stupid problem. had clean project, run qmake , build... svlasov :)
edit: in order draw .png file qpainter , qpixmap resources, have to: add picture resources
<rcc> <qresource prefix="/img"> <file>image.png</file> </qresource> </rcc>
then can use relative path file in resources here (format ":/prefix/you/created/file.something" or can use alias - here documentation)
qpixmap pixmap2(":/img/image.png");
then draw it
qpainter painter(this); painter.drawpixmap(10,70,50,50, pixmap2);
and clean , build project , work :)
Comments
Post a Comment