Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QPrinter: wrong margins/page width
hp321 last edited by
Hi. I have problem with printing rectangle with QPrinter on real device. Rectangle which I would like to draw is often cliped by a printer. With bigger marins I have other issue. Right margin is larger than left. Code below works great when it's export to PDF file or PS.
What I am doing wrong? Please help me. Without this simple task I can not finish my project.
QRectF rect = printer.pageRect();
double margin = rect.width()*0.05;
QRectF body = QRectF(margin, margin, rect.width()-margin2, rect.height()-margin2);
painter.drawText(body, "test right", option);
painter.drawText(body, "test left");@
[edit: fixed typo in title, eddy]
nicomesas last edited by
Recently I found a similar problem when printing in Hewlett-Packard printers.
After many tests they decided to make a little app for test the same, but made with pure MFC (without using Qt). The result was the same.
Thus I have deduced that the problem is neither in my program or Qt, but must be in the printer driver as this problem has left me only with HP printers.
hp321 last edited by
OK. I think that is the Qt problem because this WIN32 code works great (I have HP printer to):
pd.lStructSize = sizeof(PRINTDLG);
//pd.hwndOwner = m_hWnd;
pd.hDevMode = NULL;
pd.hDevNames = NULL;
pd.Flags = PD_USEDEVMODECOPIESANDCOLLATE | PD_RETURNDC;
pd.nCopies = 1;
pd.nFromPage = 0xFFFF;
pd.nToPage = 0xFFFF;
pd.nMinPage = 1;
pd.nMaxPage = 0xFFFF;
QMessageBox::warning(0,"Problem", "Cos z drukarka");
int LM = 0.5GetDeviceCaps(pd.hDC, LOGPIXELSX);
int TM = 0.5GetDeviceCaps(pd.hDC, LOGPIXELSY);
int LO = LM - GetDeviceCaps(pd.hDC, PHYSICALOFFSETX);
int RO = LM - (GetDeviceCaps(pd.hDC, PHYSICALWIDTH) - GetDeviceCaps(pd.hDC, PHYSICALOFFSETX) - GetDeviceCaps(pd.hDC, HORZRES));
int TO = TM - GetDeviceCaps(pd.hDC, PHYSICALOFFSETY);
int BO = TM - (GetDeviceCaps(pd.hDC, PHYSICALHEIGHT) - GetDeviceCaps(pd.hDC, PHYSICALOFFSETY) - GetDeviceCaps(pd.hDC, VERTRES));
if (LO<0) LO = 0;
if (RO<0) RO = 0;
if (TO<0) TO = 0;
if (BO<0) BO = 0;
int PW = GetDeviceCaps(pd.hDC, HORZRES) - (LO+RO);
int PH = GetDeviceCaps(pd.hDC, VERTRES) - (TO+BO);
qDebug() << "margins: " << LM << TM;
qDebug() << "offsets: " << LO << RO << TO << BO;
qDebug() << "width, height: " << PW << PH;
qDebug() << "width, height2: " << GetDeviceCaps(pd.hDC, HORZRES) << GetDeviceCaps(pd.hDC, VERTRES);
di.cbSize = sizeof(DOCINFO);
di.lpszOutput = (LPTSTR)NULL;
di.fwType = 0;
StartDoc (pd.hDC, &di);
// OUR RECTANGLE !!!
Rectangle(pd.hDC, LO, TO, LO+PW, TO+PH);
Maybe the solution is in to mix WIN32 code with Qt?