i need able print qt (the rendered contents of qgraphicsscene
, or qimage
), scale, on normal printer, pdf, on custom printers, including roll fed.
it seems works standard printers fails on custom printers, , reverse.
i have made printing work expected on custom printers (going , forth between works in different printers).
i set custom size required, , preferred orientation, based on length/width ratio.
i open print dialog (and check supply- paper set desired size, , orientation set expected)
print:
on custom printer, correct size, , if supply smaller, print clips needed. margins set correctly too.
on pdf, document of custom size requested, printed correctly - orientation not respected !!! (even though print dialog showed correctly) - see image on hp printer, white page - nothing gets printed.
#include <qapplication> #include <qgraphicsview> #include <qgraphicsscene> #include <qgraphicsrectitem> #include <qprinter> #include <qprintdialog> int main(int argc, char *argv[]) { qapplication app(argc, argv); // scene print - show rectangle easy measure qgraphicsscene* s = new qgraphicsscene(); s->setscenerect(-500, -500, 1500, 1500); s->setbackgroundbrush(qt::red); qgraphicsview* view = new qgraphicsview(); view->setscene(s); view->show(); qreal in = 72; qrectf canvasrect(-0.1*in, -0.1*in, 6*in, 4*in); qreal margins = 0.1*in; qrectf actualcanvasrect = canvasrect.adjusted(margins,margins,-margins,-margins); // show actual scene qgraphicsrectitem* contouritem = new qgraphicsrectitem(actualcanvasrect); contouritem->setbrush(qt::blue); s->additem(contouritem); // item partially on canvas (so can check margins) qgraphicsrectitem* item = new qgraphicsrectitem(-.5*in, -in, 2*in, 3*in); item->setbrush(qt::yellow); s->additem(item); // actual printing: // print scene, scale, using user margins, on given printer qprinter printer; qprinter::orientation orient = (actualcanvasrect.width() > actualcanvasrect.height() ? qprinter::landscape : qprinter::portrait); printer.setorientation(orient); printer.setpapersize(canvasrect.size(), qprinter::point); printer.setpagemargins(margins, margins, margins, margins, qprinter::point); qprintdialog printdialog(&printer); if (printdialog.exec() != qdialog::accepted) { qdebug("dialog canceled"); return 1; } qpainter painter; if (! painter.begin(&printer)) { qdebug("failed open printer"); return 1; } // render contents, clipped printer page size, , scaled point device pixel qrectf source = actualcanvasrect; // convert target rect devicepixel , clip page qrectf page = printer.pagerect(qprinter::devicepixel); qreal scale = printer.resolution()/in; qrectf target = qrectf(page.topleft(), source.size() * scale); target &= page; // clip target rect page // clip source rect page - without this, if printer paper smaller unwanted scaling source &= printer.pagerect(qprinter::point); s->render(&painter, target, source); painter.end(); return app.exec(); }
i don't understand why pdf creates portrait page though requested landscape (without changing print dialog: see image). (the width , height reversed, yet correct - document properties shows 4x6, , page attempts print correctly , scale)
more important, don't understand why typical laser jet printer prints nothing - blank page - or simple canvas scales fit.
if change properties in print dialog hp, irrelevant (like paper source, or paper type.... ), prints correctly.
what doing wrong ?
(using qt 4.7 , 5.5, must work on 4.7 - windows, have yet try linux)
Comments
Post a Comment