Printing in custom size
-
wrote on 8 Mar 2022, 14:58 last edited by
Hello,
I have a QGraphicsScene with scene size of 4"x6" and I'd like print it in the same size. Unfortunately it gets scaled up to the paper size. How can I print it with the correct size (regardless of the used paper size)?
When I tried to set up the custom page size I got only an empty page :(
Win 10, Qt 5.15.2
QPageSize pageSize = QPageSize(QSizeF(4.0, 6.0), QPageSize::Unit::Inch, "4x6 in page", QPageSize::SizeMatchPolicy::ExactMatch); bool result = printer.setPageSize(pageSize); qDebug() << "page set result" << result; // true qDebug() << printer.pageLayout().pageSize().name(); // "4x6 in page" qDebug() << printer.pageLayout().pageSize().id(); // 30 - ID of custom page size printer.setPageOrientation(QPageLayout::Landscape); QPainter painter(&printer); painter.setRenderHint(QPainter::Antialiasing); workScene->render(&painter);
Any help is appreciated. Thanks!
-
Hi and welcome to devnet,
What do you get if you paint a rectangle rather than your scene ?
-
wrote on 9 Mar 2022, 09:23 last edited by wazzdaman 3 Sept 2022, 09:50
@SGaist If I replace the line
workScene->render(&painter);
with
painter.begin(printer); painter.drawRect(QRectF(0, 0, 100, 100)); painter.end();
then I get a little rectangle in the corner, although it complains that
QPainter::begin: A paint device can only be painted by one painter at a time.
-
Since you passed printer to QPainter's constructor, you shall not call begin, just end.
-
wrote on 11 Mar 2022, 16:26 last edited by
@SGaist It fixed that warning message, thanks!
I think I've found the solution for my problem: I should pass the correct size for target in QGraphicsScene::render() and it will render the scene correctly regardless of the paper size. Is this correct?
-
Sounds good.
1/6