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
QPainterand `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.pngis not generated, and the messagesQPainter::setFont: Painter not activeandQPainter::setPen: Painter not activeare displayed.I tried to find an example that showed
QPainterandQPixmapused this way, but I failed to find.Does anyone have an example or reference showing how to do this?
TIA
-
The code that actually draws was removed to reduce the size of the code.
The line that saves the file is executed, and the files at
var/tmpare not deleted by the OS automatically.@canellas Start by checking return result of
QPixmap::save(). I suppose it's false.Is
paintDevicestill 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
QPixmapbig enough for your painting.Then maybe the
save()will do something. It wants something valid to save. -
Based on a example (that I can not find again) that show the use of a
QPainterand `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.pngis not generated, and the messagesQPainter::setFont: Painter not activeandQPainter::setPen: Painter not activeare displayed.I tried to find an example that showed
QPainterandQPixmapused this way, but I failed to find.Does anyone have an example or reference showing how to do this?
TIA
Your QPixmap is null. You should pass a valid QPixmap to the painter.
-
Your QPixmap is null. You should pass a valid QPixmap to the painter.
I understood
QPixmapdefault constructor created a valid object, that could be used byQPainteras a "drawing surface".Would you be kind to explain how should I do it?
-
Based on a example (that I can not find again) that show the use of a
QPainterand `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.pngis not generated, and the messagesQPainter::setFont: Painter not activeandQPainter::setPen: Painter not activeare displayed.I tried to find an example that showed
QPainterandQPixmapused this way, but I failed to find.Does anyone have an example or reference showing how to do this?
TIA
@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/tmpfiles are not getting deleted. -
@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/tmpfiles are not getting deleted. -
The code that actually draws was removed to reduce the size of the code.
The line that saves the file is executed, and the files at
var/tmpare not deleted by the OS automatically. -
The code that actually draws was removed to reduce the size of the code.
The line that saves the file is executed, and the files at
var/tmpare not deleted by the OS automatically.@canellas Start by checking return result of
QPixmap::save(). I suppose it's false.Is
paintDevicestill 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
QPixmapbig enough for your painting.Then maybe the
save()will do something. It wants something valid to save. -
@canellas Start by checking return result of
QPixmap::save(). I suppose it's false.Is
paintDevicestill 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
QPixmapbig 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
-
Yes, you are correct.
Unlike
QPdfWriter,QPixmapmust be big enough for the image that is to be drawn.Thanks!
This post is deleted!