3 Attachment(s)
The content doesn't show to preview when using QPrintPreviewDialog
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
Code:
void DemoClass::on_btnOK_clicked()
{
printer.
setResolution(QPrinter::HighResolution);
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
) {
painter.
setRenderHints(QPainter::Antialiasing |
painter.drawText(100, 100, "Hello World! 123");
}
On pushing the OK button, this dialog appears:
Attachment 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:
Attachment 9551
…without changing anything, I click OK and then the preview becomes correct:
Attachment 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?
Re: The content doesn't show to preview when using QPrintPreviewDialog
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.
Re: The content doesn't show to preview when using QPrintPreviewDialog
Quote:
Originally Posted by
wysota
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!
Re: The content doesn't show to preview when using QPrintPreviewDialog
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.
Re: The content doesn't show to preview when using QPrintPreviewDialog
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!
Re: The content doesn't show to preview when using QPrintPreviewDialog
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.
Re: The content doesn't show to preview when using QPrintPreviewDialog
Quote:
Originally Posted by
wysota
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 :(
Re: The content doesn't show to preview when using QPrintPreviewDialog
Hi all,
Finally I have solved my issue. I have just removed the following code
Code:
printer.
setResolution(QPrinter::HighResolution);