Results 1 to 8 of 8

Thread: printing of dockWidget contents

  1. #1
    Join Date
    May 2007
    Location
    Australia
    Posts
    30
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default printing of dockWidget contents

    I use Qt 4.4.0 and I have a dockWidget which includes a label and a QwtPlot (Qwt 5.1.0) in a layout.
    I'm trying to print the dockWidget into a .pdf but I have a problem with the size of the printed widget.

    The resulting .pdf shows the dockWidget (label and plot) but it doesn't use the whole page. If I change the size of the dockWidget before printing, the printed dockWidget size changes as well (but not taking the full page size).
    When the renderFlags are set to (DrawWindowBackground | DrawChildren) The background takes the whole page but the window doesn't.

    This is the relevant bit of my code. I'll be happy to consider any other ways of printing my dockWidget.

    Qt Code:
    1. QPrinter printer;
    2. int w = printer.width();
    3. int h = printer.height();
    4. QRegion region(0,0,w,h);
    5. QPainter p(&printer);
    6. dockWidget_->render(&p, QPoint(), region);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: printing of dockWidget contents

    Scale the painter of the printer using QPainter::setWindow() and QPainter::setViewport().

  3. The following user says thank you to wysota for this useful post:

    user (14th May 2008)

  4. #3
    Join Date
    May 2007
    Location
    Australia
    Posts
    30
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: printing of dockWidget contents

    I've added those commands but they didn't seem to make a difference, probably because I use the print dialog before I define the painter. I should have mentioned it but I didn't think it was relevant.
    I had a look at the width and height values that are sent to the painter and the width and height of my wiget. The values for the printer didn't change but the values of the widget depend on it's size.
    My solution was to resize the widget to the width and height of the printer page just before I print it and then resize it back to it's original size after the print.
    It worked and the window doesn't have time to resize on the screen.

    I just discovered I will have to print only the plot and not the whole widget, so I guess I'll have to change the code and use something else anyway...

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: printing of dockWidget contents

    Quote Originally Posted by user View Post
    I've added those commands but they didn't seem to make a difference, probably because I use the print dialog before I define the painter.
    How is that possible if you need a painter to be able to print?

  6. #5
    Join Date
    May 2007
    Location
    Australia
    Posts
    30
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: printing of dockWidget contents

    The code I had was:

    Qt Code:
    1. QPrinter printer;
    2. QPrintDialog dialog(&printer);
    3. if ( dialog.exec() == QDialog::Accepted )
    4. {
    5. int w = printer.width();
    6. int h = printer.height();
    7. QRegion region(0,0,w,h);
    8. QPainter p(&printer);
    9. dockWidget_->render(&p, QPoint(), region);
    10. }
    To copy to clipboard, switch view to plain text mode 
    I thought maybe returning from the dialog the printer already had the page size defined.

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: printing of dockWidget contents

    It does have a page size defined, but it doesn't mean your widget will be scaled to fit the page.

  8. #7
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,312
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: printing of dockWidget contents

    Use QwtPlot::print() instead of QWidget::render().

    Uwe

  9. #8
    Join Date
    May 2007
    Location
    Australia
    Posts
    30
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: printing of dockWidget contents

    Yes, this is what I'm using now and it works the way I want.
    The problem started because I needed to show an extra title with the plot (the plot only has one title and I needed 2) but I solved that by using a marker and locating it always on the top or bottom of the plot (depending on user selection).

Similar Threads

  1. Printing problem in windows
    By joseph in forum Qt Programming
    Replies: 6
    Last Post: 12th July 2007, 08:04

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.