How can I draw a png image using QPixmap and QPainter?
Solved
QML and Qt Quick
-
I'm trying to draw an image into my widget window, what is the right way of doing, the following code is not displaying anything on the window :
#include <QCoreApplication> #include <QApplication> #include <QWidget> #include <QPainter> #include <QColor> #include <QRectF> #include <QPicture> class Win: public QWidget{ public: void paintEvent(QPaintEvent *){ QPainter painter(this); QRectF target(10.0, 20.0, 800.0, 600.0); QRectF source(0.0, 0.0, 400.0, 400.0); QPixmap pixmap("qrc:/browser_web_internet_website.png"); painter.drawPixmap(target, pixmap, source); } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); Win *win = new Win(); win->show(); return a.exec(); }
-
Hi,
Check that your pixmap is valid.
-
No, it's always been ":/". AFAR, qrc:/ is used in QML and when using QUrl.
-
@SGaist sorry for reopening the issue, I wanted to add a picture to a pre-existing QWidget object, is it possible to do that ? this method requires creating another class which inherits from qwidget, and implement the paintEvent, but I just want to add a picture to an already existing qwidget window.