Results 1 to 6 of 6

Thread: qt printer fit image to A4 paper

  1. #1
    Join Date
    Jan 2016
    Posts
    81
    Thanks
    31
    Qt products
    Qt5
    Platforms
    Windows

    Default qt printer fit image to A4 paper

    I can Screenshot from a QML component using grabToImage and use QPrinter to print that image,

    I want to display that image fit to A4 paper page or print to center of A4 paper.
    But the image is draw left of paper.

    Qt Code:
    1. void pr::print(QVariant data)
    2.  
    3. {
    4. QImage img = qvariant_cast<QImage>(data);
    5. QPrinter printer;
    6. QPrintDialog *dlg = new QPrintDialog(&printer,0);
    7. if(dlg->exec() == QDialog::Accepted) {
    8.  
    9. QPainter painter(&printer);
    10. printer.setPageSize(QPrinter::A4);
    11.  
    12. //painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
    13. painter.drawImage(QPoint(0,0),img);
    14. printer.setFullPage(true);
    15. painter.end();
    16. }
    17.  
    18. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: qt printer fit image to A4 paper

    With that code it should be painted at the topleft.
    Does it not do that?

    Cheers,
    _

  3. #3
    Join Date
    Jan 2016
    Posts
    81
    Thanks
    31
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: qt printer fit image to A4 paper

    Quote Originally Posted by anda_skoa View Post
    With that code it should be painted at the topleft.
    Does it not do that?

    Cheers,
    _
    Thank you.
    Yes.
    But I want draw image in top center of paper.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qt printer fit image to A4 paper

    Well, then you probably want to calculate the correct x value for the drawig position instead of using 0

    Cheers,
    _

  5. #5
    Join Date
    Jan 2016
    Posts
    81
    Thanks
    31
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: qt printer fit image to A4 paper

    Quote Originally Posted by anda_skoa View Post
    Well, then you probably want to calculate the correct x value for the drawig position instead of using 0

    Cheers,
    _
    Yes that's right.
    But howØŸ

    I use this code:
    Qt Code:
    1. int imgW=img.width();
    2. int pW=printer.paperRect().width();
    3. int x=(pW-imgW)/2;
    4. painter.drawImage(QPoint(x,10),img);
    To copy to clipboard, switch view to plain text mode 

    Is this best solution?

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qt printer fit image to A4 paper

    Looks about right, though I can't find a paperRect() method that is callable without argument.

    You could alternatively also try painter.device()->width().

    Also maybe have "x" as a qreal and use the drawImage() overload that takes a QPointF.

    Cheers,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    neda (2nd May 2016)

Similar Threads

  1. Replies: 2
    Last Post: 26th May 2015, 14:59
  2. How do I set a custom page/paper size in QPrinter
    By alferjaani in forum Qt Programming
    Replies: 7
    Last Post: 26th July 2013, 04:42
  3. Replies: 0
    Last Post: 30th May 2013, 10:19
  4. Save Printer Page Settings to show on next print
    By arunkumaraymuo1 in forum Qt Programming
    Replies: 1
    Last Post: 6th August 2012, 23:16
  5. Printer paper layouts
    By IsoArska in forum Qt Programming
    Replies: 1
    Last Post: 17th July 2010, 14:22

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.