Search code examples
qtprintingqt4

QPrintPreviewDialog incorrect preview


Using QPrintPreviewDialog to preview the print, I use the following code

    QPrinter printer;
    printer.setResolution(QPrinter::HighResolution);
    printer.setPaperSize(QPrinter::A4);
    printer.setOrientation(QPrinter::Portrait);
    QPrintPreviewDialog *pd = new QPrintPreviewDialog(&printer);
    connect(pd,SIGNAL(paintRequested(QPrinter*)),this,SLOT(print(QPrinter*)));
    pd->exec();


void Class::print(QPrinter *p)
{
    QTextEdit *ted = new QTextEdit;
    ted->insertHtml("<center><img src='"+QString(":/img/logo.png")+"' width='90' height='72'/><b><font size='9'>Logo Text</font></b></center>");
    ted->document()->print(p);
}

On pushing the print button, this dialog appears:

enter image description here

As you can see the content is spread all over the page. Then I click the page setup button on the preview dialog and this appears:

enter image description here

without changing anything, I click OK and then the preview becomes correct:

enter image description here

The question is that how to correct the preview by code?


Solution

  • Add a QPageSetupDialog to show before preview.