Ok, got a little further. The printpreview now works, but it isn't behaving the way I want. Now that I have added the QTextEdit to the main window, the text gets formatted according to the widget's dimension. I guess that is kind of expected. What I really wanted is to create a nice looking multi page report and my reason for using QTextEdit is the Richly formatted text. What I didn't anticipate is the way the text is depending on the QTextEdit widget's dimension. Is there no way for me to create nicely formatted text but defer line wrapping etc. until it gets drawn onto the printer/painter device?
Would I have to print to pdf in order to get what I want? I guess I could just try it, but I was wondering if I'm approaching this from the right direction.
Here is my latest code.
//Mainwindow constructor contains: printer = new QPrinter(QPrinter::ScreenResolution);
void MainWindow
::print(QPrinter *printer
) {
painter.begin(printer);
ui->textEdit->append("first page this is a fairly long line to see what happens when it is being wrapped");
ui->textEdit->document()->drawContents(&painter, printer->pageRect());
printer->newPage();
ui->textEdit->clear();
ui->textEdit->append("second page");
ui->textEdit->document()->drawContents(&painter, printer->pageRect());
ui->textEdit->clear();
painter.end();
}
void MainWindow::on_pushButton_clicked()
{
QPrintPreviewDialog preview(printer,this);
preview.exec();
}
//Mainwindow constructor contains: printer = new QPrinter(QPrinter::ScreenResolution);
void MainWindow::print(QPrinter *printer)
{
QPainter painter;
painter.begin(printer);
ui->textEdit->append("first page this is a fairly long line to see what happens when it is being wrapped");
ui->textEdit->document()->drawContents(&painter, printer->pageRect());
printer->newPage();
ui->textEdit->clear();
ui->textEdit->append("second page");
ui->textEdit->document()->drawContents(&painter, printer->pageRect());
ui->textEdit->clear();
painter.end();
}
void MainWindow::on_pushButton_clicked()
{
QPrintPreviewDialog preview(printer,this);
connect(&preview, SIGNAL(paintRequested(QPrinter*)),SLOT(print(QPrinter *)));
preview.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks