PDA

View Full Version : The content doesn't show to preview when using QPrintPreviewDialog



LeeMinh
10th September 2013, 03:49
I am using Qt version 4.8.4 on Window 7. I don’t have conditions to test this issue on another platform such as Mac, Linux… and I use the following code to preview content



void DemoClass::on_btnOK_clicked()
{
QPrinter printer;
printer.setResolution(QPrinter::HighResolution);
printer.setPaperSize(QPrinter::A4);
printer.setOrientation(QPrinter::Portrait);
printer.setFullPage(true);

QPrintPreviewDialog *printPreview = new QPrintPreviewDialog(&printer);
connect(printPreview, SIGNAL(paintRequested(QPrinter*)), this, SLOT(print(QPrinter*)));

printPreview->setWindowTitle("Preview Dialog");
Qt::WindowFlags flags(Qt::WindowTitleHint);
printPreview->setWindowFlags(flags);
printPreview->exec();
}

void DemoClass::print(QPrinter *printer)
{
QPainter painter(printer);
painter.setRenderHints(QPainter::Antialiasing |
QPainter::TextAntialiasing |
QPainter::SmoothPixmapTransform, true);

painter.drawText(100, 100, "Hello World! 123");
}


On pushing the OK button, this dialog appears:

9550

As you see, the page is blank. The page doesn’t contain any content. Then I click the page setup button on the preview dialog and this appears:

9551

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

9552

I really don’t understand what the reason is. How can I show the content correctly without changing page setup?

Do you have any solutions?

wysota
10th September 2013, 08:22
The reason is that you didn't set up the printer object before showing the preview. Once you click the page setup button, the printer object gets initialized with proper values and the preview shows correctly.

LeeMinh
10th September 2013, 09:21
The reason is that you didn't set up the printer object before showing the preview. Once you click the page setup button, the printer object gets initialized with proper values and the preview shows correctly.

Thanks for your clearly response! I understood more about my issue. As a newbie in Qt, I don't have enough experience to solve this issue so could you give me sample code in this case or you show me the way to modify my code to the preview shows correctly?

Thanks!

wysota
10th September 2013, 09:54
The easiest way is to use QPrintDialog on a printer object before using it for the first time. Otherwise don't use a predefined printer object but let the preview dialog create one for you.

LeeMinh
10th September 2013, 10:24
I also used QPrintDialog object that you had mentioned. The preview content appeared well for the first time. But I couldn't get actions such as Zoom in, Zoom out, Fit width, Fit page....like QPrintPreviewDialog object. I have not solved this issue yet. Maybe I need to research deeply about Qt. Anyway, thanks for your help!

wysota
10th September 2013, 10:29
You should first setup the printer using the printer dialog and then call the preview dialog. It's not that you should use one instead of the other.

LeeMinh
10th September 2013, 11:49
You should first setup the printer using the printer dialog and then call the preview dialog. It's not that you should use one instead of the other.

Could you show me some sample code to solve this issue? It will help me save a lot of working in this case. I researched documents and technologies that followed your instruction but I still didn't solve this issue :(

LeeMinh
12th September 2013, 08:25
Hi all,

Finally I have solved my issue. I have just removed the following code


printer.setResolution(QPrinter::HighResolution);