Using QPainter to draw on a QPixmap and save it
-
Based on a example (that I can not find again) that show the use of a
QPainter
and `QPdfWriterP , I am trying to create a PNG file like this:void MainWindow::on_pushButton_5_clicked() { QPixmap paintDevice; QPainter painter(&paintDevice); int x0{10}; int y0{10}; const int width{200}; const int height{200}; const int cols{5}; const int rows{3}; QPen line_pen(Qt::black); line_pen.setWidth(5); painter.setPen(line_pen); // calls to drawLine, setPen e setFont among other methods of `QPainter` const QString fileName("/var/tmp/mydoc.png"); paintDevice.save(fileName, "PNG"); }
But the file
/var/tmp/mydoc.png
is not generated, and the messagesQPainter::setFont: Painter not active
andQPainter::setPen: Painter not active
are displayed.I tried to find an example that showed
QPainter
andQPixmap
used this way, but I failed to find.Does anyone have an example or reference showing how to do this?
TIA
-
@canellas Start by checking return result of
QPixmap::save()
. I suppose it's false.Is
paintDevice
still a nullQPixmap
? I think it is, depending on what code you removed. Hence earlier
@Christian-Ehrlicher said in Using QPainter to draw on a QPixmap and save it:Your QPixmap is null. You should pass a valid QPixmap to the painter.
I think you have to pass a
QPixmap
big enough for your painting.Then maybe the
save()
will do something. It wants something valid to save. -
Your QPixmap is null. You should pass a valid QPixmap to the painter.
-
I understood
QPixmap
default constructor created a valid object, that could be used byQPainter
as a "drawing surface".Would you be kind to explain how should I do it?
-
@canellas
You have to call bool QPainter::begin(QPaintDevice *device).Apart from that, so far as I see in code shown you never draw into painter so you're not going to see much. All you do is set a pen and never craw anything. I don't know about not saving the file, check that line is reached and check
/var/tmp
files are not getting deleted. -
@canellas Start by checking return result of
QPixmap::save()
. I suppose it's false.Is
paintDevice
still a nullQPixmap
? I think it is, depending on what code you removed. Hence earlier
@Christian-Ehrlicher said in Using QPainter to draw on a QPixmap and save it:Your QPixmap is null. You should pass a valid QPixmap to the painter.
I think you have to pass a
QPixmap
big enough for your painting.Then maybe the
save()
will do something. It wants something valid to save. -
C canellas has marked this topic as solved on
-
This post is deleted!