Greetings everyone!
I'm having a trouble with printing via Qt.
What I want - is to print the viewable content of MainWindow using QPrint. I have printed test sheets using this code:
void MainWindow::setup_print(void)
{
if (dialog
->exec
() == QDialog::Accepted) {
painter.setPen( Qt::black );
for( int page=0; page<5; page++ )
{
painter.drawRect( printer.pageRect() );
painter.drawLine( printer.pageRect().topLeft(), printer.pageRect().bottomRight() );
painter.drawLine( printer.pageRect().topRight(), printer.pageRect().bottomLeft() );
painter.drawRect(this->contentsRect() );
printer.pageRect().left() +printer.resolution() * 0.5,
printer.pageRect().top() +printer.resolution() * 0.5,
printer.pageRect().width() -printer.resolution() * 1.0,
printer.pageRect().height()-printer.resolution() * 1.5 );
painter.drawRect( textArea );
painter.
drawText( textArea, Qt
::AlignTop | Qt
::AlignLeft,
QString( "Page %1" ).
arg( page
+1 ) );
if( page != 4 )
printer.newPage();
}
}
}
void MainWindow::setup_print(void)
{
dialog = new QPrintDialog(&printer, this);
if (dialog->exec() == QDialog::Accepted)
{
QPainter painter( &printer );
painter.setPen( Qt::black );
for( int page=0; page<5; page++ )
{
painter.drawRect( printer.pageRect() );
painter.drawLine( printer.pageRect().topLeft(), printer.pageRect().bottomRight() );
painter.drawLine( printer.pageRect().topRight(), printer.pageRect().bottomLeft() );
painter.drawRect(this->contentsRect() );
QRectF textArea(
printer.pageRect().left() +printer.resolution() * 0.5,
printer.pageRect().top() +printer.resolution() * 0.5,
printer.pageRect().width() -printer.resolution() * 1.0,
printer.pageRect().height()-printer.resolution() * 1.5 );
painter.drawRect( textArea );
painter.drawText( textArea, Qt::AlignTop | Qt::AlignLeft, QString( "Page %1" ).arg( page+1 ) );
if( page != 4 )
printer.newPage();
}
}
}
To copy to clipboard, switch view to plain text mode
the "printer" object is mainwindow class member.
The problem is that I have no idea how to get the content of mainwindow be represented as a QPainter object.
Can anyone help me with this please?
Bookmarks