Results 1 to 2 of 2

Thread: how to scale according to the used resolution

  1. #1
    Join Date
    Jun 2007
    Location
    italy
    Posts
    126
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default how to scale according to the used resolution

    Hi guys,

    I was trying to to produce a printed copy of a QTableWidget.

    Thanks to JPN I get closer to the goal but I am still experiencing
    some problems..

    Here is the code as it is so far:

    Qt Code:
    1. QPrinter printer(QPrinter::ScreenResolution);
    2.  
    3. QPrintDialog dlg(&printer, this);
    4.  
    5. if (dlg.exec() == QDialog::Accepted)
    6. {
    7. // calculate the total width/height table would need without scaling
    8. const int rows = tableWidget->model()->rowCount();
    9. const int cols = tableWidget->model()->columnCount();
    10. double totalWidth = 0.0;
    11.  
    12. for (int c = 0; c < cols; ++c)
    13. {
    14. totalWidth += tableWidget->columnWidth(c);
    15. }
    16.  
    17. double totalHeight = 0.0;
    18.  
    19. for (int r = 0; r < rows; ++r)
    20. {
    21. totalHeight += tableWidget->rowHeight(r);
    22. }
    23.  
    24. // redirect table's painting on a pixmap
    25. QPixmap pixmap(totalWidth, totalHeight);
    26. QPainter::setRedirected(tableWidget->viewport(), &pixmap);
    27. QPaintEvent event(QRect(0, 0, totalWidth, totalHeight));
    28. QApplication::sendEvent(tableWidget->viewport(), &event);
    29. QPainter::restoreRedirected(tableWidget->viewport());
    30.  
    31. // print scaled pixmap
    32. QPainter painter(&printer);
    33. //painter.drawPixmap(printer.pageRect(), pixmap, pixmap.rect());
    34. painter.drawPixmap(printer.pageRect().topLeft(), pixmap, pixmap.rect());
    35. }
    To copy to clipboard, switch view to plain text mode 

    The problem is that I can't get the entire table fitting in the page.

    What I am doing wrong?

    Thank you for your attention,

    Any even partial reply is much appreciated
    Roby

  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: how to scale according to the used resolution

    I suggest you use the window-viewport transformation of the painter. This way you'll be using logical coordinates of the pixmap but they'll be scaled to device coordinates of the printer.

Similar Threads

  1. How to copy pixmap and keep scale ratio?
    By Teerayoot in forum Qt Programming
    Replies: 2
    Last Post: 11th May 2007, 11:42
  2. QGraphicsView scale question.
    By hickscorp in forum Qt Programming
    Replies: 20
    Last Post: 15th April 2007, 02:28
  3. Can QPicture scale and rotate ?
    By igor in forum Qt Programming
    Replies: 6
    Last Post: 25th January 2007, 22:48
  4. Screen Resolution
    By vijay anandh in forum Qt Programming
    Replies: 3
    Last Post: 6th October 2006, 13:47
  5. My own scale widget in Qwt
    By igrms in forum Qwt
    Replies: 7
    Last Post: 15th June 2006, 21:18

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.