Results 1 to 20 of 25

Thread: QTableView printing

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: QTableView printing

    Maybe you need to save the QPainter always before scaling and translating and restore it afterwards. You are using the same QPainter object for painting all pages, this means you are also scaling it again and again.
    J-P Nurmi

  2. #2
    Join Date
    Sep 2006
    Posts
    38
    Qt products
    Qt4 Qt5
    Platforms
    Windows
    Thanks
    5
    Thanked 3 Times in 2 Posts

    Default Re: QTableView printing

    hmmm..I'm not exactly sure. Here's what I tried based on your idea...

    Qt Code:
    1. void PrintTableView::printTable(QPrinter* printer, QPainter* painter, const QRect& area)
    2. {
    3. const int rows = model()->rowCount();
    4. const int columns = model()->columnCount();
    5.  
    6. double totalWidth = 0.0;
    7. double totalPageHeight = 0.0;
    8. double totalHeight = 0.0;
    9. for (int c = 0; c < columns; ++c)
    10. {
    11. totalWidth += columnWidth(c);
    12. }
    13.  
    14. for (int p = 0; p < 45; ++p)
    15. {
    16. totalPageHeight += rowHeight(p);
    17. }
    18.  
    19. for (int r = 0; r<rows; ++r)
    20. {
    21. totalHeight += rowHeight(r);
    22. }
    23.  
    24. const double xscale = area.width() / totalWidth;
    25. const double yscale = area.height() / totalHeight;
    26. const double pscale = area.height() / totalPageHeight;
    27.  
    28. painter->scale(xscale, yscale);
    29. painter->translate(area.x() + xscale, area.y() + pscale);
    30. painter->save();
    31.  
    32. bool ok;
    33. int x=0;
    34.  
    35. for (int r = 0; r < rows; ++r) {
    36. ++x;
    37. for (int c = 0; c < columns; ++c) {
    38. QModelIndex idx = model()->index(r,c);
    39. QStyleOptionViewItem option = viewOptions();
    40. option.rect = visualRect(idx);
    41. itemDelegate()->paint(painter, option, idx);
    42. }
    43. if (x == 45)
    44. {
    45. ok = printer->newPage();
    46. x = 0;
    47. painter->restore();
    48. }
    49. }
    50. }
    To copy to clipboard, switch view to plain text mode 

    if I put painter->save() before the painter->scale(), it prints every page, but only in a very very small square. if I leave it the way it is shown here, it prints every page stretched to page width, but compressed to 1/8th the page. If I do painter->scale(xscale, pscale),and leave painter->save() after painter->translate(), then it prints the first page correctly, but all other pages are blank. I tried re-scaling the painter when x == 45, but that only hangs the program.

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: QTableView printing

    Ok, sorry for not paying enough attention. Forget about saving and restoring for now. Seems I didn't read your code carefully enough. I somehow thought you were calling the print function for every page...

    What I'm suspecting is that the areas returned by the visualRect(idx) don't match on the second page because it's not aware of that you have requested a new page and the coordinates have been reseted. So you would have to translate the painter in vertical direction after every new page request. Try to print the pageRect and the visualRects in the debug output or something and you'll see.
    J-P Nurmi

  4. The following user says thank you to jpn for this useful post:

    derrickbj (10th October 2006)

  5. #4
    Join Date
    Sep 2006
    Posts
    38
    Qt products
    Qt4 Qt5
    Platforms
    Windows
    Thanks
    5
    Thanked 3 Times in 2 Posts

    Default Re: QTableView printing

    J-P,

    You have NOTHING to apologize for!! You've been very helpful.

    That's EXACTLY what needed to be done!!! I ran visualRect through qDebug() after every page and found that y() was incrementing by 1350. I also found out that after printing 45 rows, option.rect.x() was 1000 and not 0. So, I did painter->translate(0, -1350) after calling printer->newPage() and everything prints beautifully. I can't thank you enough for your help!! I was really appreciate it!

    Qt Code:
    1. void PrintTableView::printTable(QPrinter* printer, QPainter* painter, const QRect& area)
    2. {
    3. const int rows = model()->rowCount();
    4. const int columns = model()->columnCount();
    5. double totalWidth = 0.0;
    6. double totalPageHeight = 0.0;
    7. double totalHeight = 0.0;
    8. for (int c = 0; c < columns; ++c)
    9. {
    10. totalWidth += columnWidth(c);
    11. }
    12.  
    13. for (int p = 0; p < 45; ++p)
    14. {
    15. totalPageHeight += rowHeight(p);
    16. }
    17.  
    18. for (int r = 0; r<rows; ++r)
    19. {
    20. totalHeight += rowHeight(r);
    21. }
    22.  
    23. const double xscale = area.width() / totalWidth;
    24. const double yscale = area.height() / totalHeight;
    25. const double pscale = area.height() / totalPageHeight;
    26. painter->scale(xscale, pscale);
    27. painter->translate(area.x() + xscale, area.y() + yscale);
    28.  
    29. bool ok;
    30. int x=0;
    31.  
    32. for (int r = 0; r < rows; ++r) {
    33. ++x;
    34. for (int c = 0; c < columns; ++c) {
    35. QModelIndex idx = model()->index(r,c);
    36. option = viewOptions();
    37. option.rect = visualRect(idx);
    38. itemDelegate()->paint(painter, option, idx);
    39. }
    40. if (x == 45)
    41. {
    42. ok = printer->newPage();
    43. x=0;
    44. painter->translate(0, -1350);
    45. }
    46. }
    47. }
    To copy to clipboard, switch view to plain text mode 

  6. The following 2 users say thank you to derrickbj for this useful post:

    Charlie37 (26th November 2010), thae (5th November 2006)

  7. #5
    Join Date
    Nov 2006
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    1

    Question Re: QTableView printing

    I don't understand what to do before calling the function and what to set before for the three arguments :
    Qt Code:
    1. void PrintTableView::printTable(QPrinter* printer, QPainter* painter, const QRect& area)
    To copy to clipboard, switch view to plain text mode 

    Do we call it from the mainwindow?

    What to do to print? Where is the line to print our QTableView? As the line used in the post from jpn the 29th September 2006 22:43 with :
    Qt Code:
    1. //[...]
    2. painter.drawPixmap( const QRect & target, const QPixmap & pixmap, const QRect & source )
    3. //[...]
    To copy to clipboard, switch view to plain text mode 

    Can you put the entire code on-line please with what to be done before and after the function printTable to print a QTableWidget. I am beginner with Qt and i need some full code example...

    Thank you for your help.
    Alex

Similar Threads

  1. Set height of QTableView to fit exact number of rows.
    By Ben.Hines in forum Qt Programming
    Replies: 3
    Last Post: 17th January 2019, 01:49
  2. QTableView sorting
    By gabriels in forum Qt Programming
    Replies: 11
    Last Post: 6th October 2010, 17:13
  3. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42
  4. Replies: 0
    Last Post: 28th June 2006, 20:49
  5. Multi-line messages in QTableView
    By Conel in forum Qt Programming
    Replies: 6
    Last Post: 13th April 2006, 13:49

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
  •  
Qt is a trademark of The Qt Company.