Important: Please read the Qt Code of Conduct -

QPrinter: wrong margins/page width

  • 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.

    @QPrinter printer;
    // printer.setOutputFormat(QPrinter::PdfFormat);
    // printer.setOutputFileName("test.pdf");

    QRectF rect = printer.pageRect();

    double margin = rect.width()*0.05;

    QRectF body = QRectF(margin, margin, rect.width()-margin2, rect.height()-margin2);

    QPainter painter(&printer);

    QTextOption option;

    painter.drawText(body, "test right", option);
    painter.drawText(body, "test left");@

    [edit: fixed typo in title, eddy]

  • Hello
    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.


  • OK. I think that is the Qt problem because this WIN32 code works great (I have HP printer to):
    @PRINTDLG pd;
    ZeroMemory(&pd, sizeof(PRINTDLG));

    pd.lStructSize = sizeof(PRINTDLG);
    //pd.hwndOwner = m_hWnd;
    pd.hDevMode = NULL;
    pd.hDevNames = NULL;
    pd.nCopies = 1;
    pd.nFromPage = 0xFFFF;
    pd.nToPage = 0xFFFF;
    pd.nMinPage = 1;
    pd.nMaxPage = 0xFFFF;

    if (!PrintDlg(&pd))
    QMessageBox::warning(0,"Problem", "Cos z drukarka");

    int LM = 0.5GetDeviceCaps(pd.hDC, LOGPIXELSX);
    int TM = 0.5
    GetDeviceCaps(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);

    DOCINFO di;
    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?

Log in to reply