PDA

View Full Version : what unit is used in QTextDocument::setPageSize() method?



jan
12th October 2009, 12:07
My program involve printing to a roll script which is just 8 cm width. I have set the paper size of the QPrinter accordingly. The content is rendered from a QTextDocument object. The output was cropped on the right side. Hence I would like to set the page size on the QTextDocument in order to render the content correctly. The problem is, what unit is used in the QTextDocument::setPageSize method?

Here is my code snip.

QTextDocument *lpDocument(ui.mpTextEditPrintOut->document());
QPrinter lrPrinter;

QPrintDialog* lpDlg(new QPrintDialog(&lrPrinter, this));
lrPrinter.setPaperSize(QSizeF(8.0, 10.0), QPrinter::Millimeter);
lrPrinter.setPageMargins(0.0, 0.0, 0.0, 0.0, QPrinter::Millimeter);
if (lpDlg->exec() != QDialog::Accepted)
{
return;
}

lpDocument->print(&lrPrinter);

development environment:
Qt 4.4.0.
MSVC 8.0 SP1 with Qt Visual Studio Integration 1.4.0.
Win XP SP3.

ChrisW67
13th October 2009, 04:59
I think you meant:

lrPrinter.setPaperSize(QSizeF(80, 100), QPrinter::Millimeter);


If you look at the mergins after this:


lrPrinter.setPageMargins(0.0, 0.0, 0.0, 0.0, QPrinter::Millimeter);

you might find they are not zero and shifting your output to the right. Something similar to this is happening to me on Windows (http://www.qtcentre.org/forum/f-qt-programming-2/t-qprinter-setfullpage-winlinux-diffs-23771.html), where even


setFullPage(true);

does not seem to help.

Does it look correct if you output to PDF?

gsoral
3rd June 2012, 15:26
The unit for QTextDocument is point (pt).


const qreal PT_MM = 25.4/72.0;
document.setDocumentMargin(20.0/PT_MM); // sets margin to 20mm

It also works with html images:

document.setHtml("<img src=\"anyimg.jpg\" width = 200 />); // creates an image 200pt = 200*PT_MM millimeters wide


To set a PageSize use:


QSizeF size = printer.paperSize(QPrinter::Point);
document.setPageSize(size);