Results 1 to 3 of 3

Thread: QPrinter and QPrintDialog

  1. #1
    Join Date
    Feb 2016
    Posts
    11
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Lightbulb QPrinter and QPrintDialog

    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:
    Qt Code:
    1. void MainWindow::setup_print(void)
    2. {
    3. dialog = new QPrintDialog(&printer, this);
    4. if (dialog->exec() == QDialog::Accepted)
    5. {
    6. QPainter painter( &printer );
    7.  
    8. painter.setPen( Qt::black );
    9.  
    10. for( int page=0; page<5; page++ )
    11. {
    12. painter.drawRect( printer.pageRect() );
    13. painter.drawLine( printer.pageRect().topLeft(), printer.pageRect().bottomRight() );
    14. painter.drawLine( printer.pageRect().topRight(), printer.pageRect().bottomLeft() );
    15. painter.drawRect(this->contentsRect() );
    16. QRectF textArea(
    17. printer.pageRect().left() +printer.resolution() * 0.5,
    18. printer.pageRect().top() +printer.resolution() * 0.5,
    19. printer.pageRect().width() -printer.resolution() * 1.0,
    20. printer.pageRect().height()-printer.resolution() * 1.5 );
    21.  
    22. painter.drawRect( textArea );
    23.  
    24. painter.drawText( textArea, Qt::AlignTop | Qt::AlignLeft, QString( "Page %1" ).arg( page+1 ) );
    25.  
    26. if( page != 4 )
    27.  
    28. printer.newPage();
    29. }
    30. }
    31. }
    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QPrinter and QPrintDialog

    Well, it is obviously very uncommon to print a widget as screen and printer usually have widely different visual properties, but have you tried QWidget::render()?

    Cheers,
    _

  3. #3
    Join Date
    Feb 2016
    Posts
    11
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: QPrinter and QPrintDialog

    Thank you very much! That has worked. But now I encountered another problem.
    When I'm calling QPrintPreviewDialog, there is two pages, thow I rendered only my central widget. So I have one page with my central widget and another page empty.
    Also the Image is printed correctly on the page only if I open the app in fullscreen. If I call my app and it is not stretched to full screen than I see onle part of it on the first page.
    I provide the code of my print preview SLOT and connects.
    Qt Code:
    1. connect(prev_dialog, SIGNAL(paintRequested(QPrinter*)), this, SLOT(print_preview()));
    2. connect(ui->actionPrint_Preview, SIGNAL(triggered(bool)), this, SLOT(preview_called()));
    3. //////////////////////////////////////////////////////////////////////////////////////////
    4. void MainWindow::print_preview(void)
    5. {
    6. QPainter painter(&printer);
    7. painter.setWindow(this->geometry());
    8.  
    9. this->render(&painter);
    10. printer.newPage();
    11. /*if (prev_dialog->exec() == QDialog::Accepted)
    12.   {
    13.  
    14.   }*/
    15. }
    16.  
    17. void MainWindow::preview_called(void)
    18. {
    19. prev_dialog->exec();
    20. }
    To copy to clipboard, switch view to plain text mode 
    Thank you again for helping me to deal with this.

Similar Threads

  1. QPrinter / QPrintDialog issue
    By d_stranz in forum Qt Programming
    Replies: 1
    Last Post: 4th June 2014, 23:55
  2. Replies: 1
    Last Post: 14th May 2013, 08:54
  3. QPrintDialog (pyqt4)
    By jaybstory in forum Qt Programming
    Replies: 0
    Last Post: 7th February 2010, 05:35
  4. How to use QPrinter without QPrintDialog?
    By pascal456 in forum Qt Programming
    Replies: 2
    Last Post: 15th November 2006, 20:57
  5. QPrinter::PrinterMode and QPrinter::setResolution??
    By SkripT in forum Qt Programming
    Replies: 2
    Last Post: 28th April 2006, 12:59

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.