PDA

View Full Version : qt printer fit image to A4 paper



neda
30th April 2016, 10:45
I can Screenshot from a QML component using grabToImage and use QPrinter to print that image,

I want to display that image fit to A4 paper page or print to center of A4 paper.
But the image is draw left of paper.


void pr::print(QVariant data)

{
QImage img = qvariant_cast<QImage>(data);
QPrinter printer;
QPrintDialog *dlg = new QPrintDialog(&printer,0);
if(dlg->exec() == QDialog::Accepted) {

QPainter painter(&printer);
printer.setPageSize(QPrinter::A4);

//painter.setCompositionMode(QPainter::CompositionMo de_SourceIn);
painter.drawImage(QPoint(0,0),img);
printer.setFullPage(true);
painter.end();
}

}

anda_skoa
1st May 2016, 09:36
With that code it should be painted at the topleft.
Does it not do that?

Cheers,
_

neda
1st May 2016, 10:04
With that code it should be painted at the topleft.
Does it not do that?

Cheers,
_

Thank you.
Yes.
But I want draw image in top center of paper.

anda_skoa
1st May 2016, 11:30
Well, then you probably want to calculate the correct x value for the drawig position instead of using 0 :)

Cheers,
_

neda
1st May 2016, 12:06
Well, then you probably want to calculate the correct x value for the drawig position instead of using 0 :)

Cheers,
_

Yes that's right.
But howØŸ

I use this code:

int imgW=img.width();
int pW=printer.paperRect().width();
int x=(pW-imgW)/2;
painter.drawImage(QPoint(x,10),img);

Is this best solution?

anda_skoa
1st May 2016, 12:52
Looks about right, though I can't find a paperRect() method that is callable without argument.

You could alternatively also try painter.device()->width().

Also maybe have "x" as a qreal and use the drawImage() overload that takes a QPointF.

Cheers,
_