I am having difficulties with implementing the QPrintPreviewDialog.
I included this code in the function that calls the printing:
QPrintPreviewDialog ppd(&printer,this,Qt::Window);
connect(&ppd,
SIGNAL(paintRequested
(QPrinter *)),
SLOT(printPreview
(QPrinter *)));
ppd.exec();
QPrinter printer(QPrinter::HighResolution);
QPrintPreviewDialog ppd(&printer,this,Qt::Window);
connect(&ppd, SIGNAL(paintRequested(QPrinter *)), SLOT(printPreview(QPrinter *)));
ppd.exec();
To copy to clipboard, switch view to plain text mode
And I also added this function at the end:
void PackageDialog
::printPreview( QPrinter* printer
) {
ui->editor->print(printer);
}
void PackageDialog::printPreview( QPrinter* printer)
{
ui->editor->print(printer);
}
To copy to clipboard, switch view to plain text mode
It says that the ui->editor part should be the parent of the dialog box but I am not sure if I understand what it needs there.
In the textedit example it uses textEdit = new QTextEdit(this); to initialize a value which is later used in the PrintPreview. But in my case what do I initialize?
Any help is appreciated.
Thanks,
Pericles
Added after 30 minutes:
I added the following code in the printPreview function instead of ui->editor->print(printer)
// create painter for drawing print page
// you can do formatting here...
font.setPointSize(18);
painter.setFont(font);
for (int i = 0; i < 3; i++) {
painter.
drawText(10,
10,
QString("page number: %1").
arg(i
));
// move to next page
if (i != 2) printer->newPage();
}
// create painter for drawing print page
QPainter painter( printer );
// you can do formatting here...
QFont font;
font.setPointSize(18);
painter.setFont(font);
for (int i = 0; i < 3; i++) {
painter.drawText(10, 10, QString("page number: %1").arg(i));
// move to next page
if (i != 2) printer->newPage();
}
To copy to clipboard, switch view to plain text mode
It opens the print preview dialog box but nothing is showing up in it.
Do I need to specify anywhere what to show?
Pericles
Added after 46 minutes:
Just found out that I needed to do widget->render();
Works now.
Thanks,
Pericles
Bookmarks