I want to print rows of "QStandardItemModel". I use this code, It works but table is not beautiful. I don't know how to adjust row height and I don't know how to set column widths based on the width of cells in the first row of the table.

Qt Code:
  1. QPrinter printer;
  2. printer.setPageSize(QPrinter::A4);
  3. printer.setFullPage(true);
  4.  
  5. QPrintDialog *dlg = new QPrintDialog(&printer,0);
  6. if(dlg->exec() == QDialog::Accepted) {
  7.  
  8. QPainter painter;
  9. if (painter.begin(&printer)) {
  10.  
  11. painter.translate(0,0);
  12.  
  13. int position = 0;
  14. int rowCount=newMyModel->rowCount(QModelIndex());
  15.  
  16. for(int r=0;r<rowCount;r++)
  17. {
  18. if( position > painter.window().height()-100 )
  19. {
  20. printer.newPage();
  21.  
  22. position = 0;
  23. painter.resetTransform();
  24. painter.translate(0, 0);
  25. }
  26.  
  27. QString html="<table style='page-break-after:always' border='1' width='100%' cellpadding =10 style='border-width: 1px;border-style: solid;border-color: #9e9e9e;width:100%'>";
  28.  
  29. index=newMyModel->index(r, 0);
  30.  
  31. QVariant prop1=index.data(MyModel::prop1);
  32. QVariant prop2=index.data(MyModel::prop2);
  33.  
  34. '''
  35. '''
  36. '''
  37.  
  38. html.append(
  39. "<tr style='background-color:red'>"
  40. "<td style='border-width: 1px;padding:10; border-style: solid; border-color: #9e9e9e;width:16%'>"+prop1.toString()+" </td>"
  41. "<td style='border-width: 1px;padding:10; border-style: solid; border-color: #9e9e9e;width:16%'>"+prop2.toString()+" </td>"
  42. ...
  43. ...
  44. ...);
  45.  
  46. }
  47.  
  48. html.append("</table>");
  49.  
  50. QRect rect = painter.boundingRect(painter.window(),
  51. Qt::AlignJustify | Qt::TextWordWrap,
  52. html);
  53. doc.setHtml(html);
  54. doc.drawContents(&painter, rect);
  55.  
  56. painter.drawRect(rect);
  57. painter.translate(0, rect.height());
  58. position += rect.height();
  59. }
  60. painter.end();
  61. }
  62. }
To copy to clipboard, switch view to plain text mode 

And I use this code but it does not work (Print blank page).

Qt Code:
  1. QPrinter printer;
  2. printer.setPageSize(QPrinter::A4);
  3. printer.setFullPage(true);
  4.  
  5. QPrintDialog *dlg = new QPrintDialog(&printer,0);
  6. if(dlg->exec() == QDialog::Accepted) {
  7.  
  8. QTableView* pTableView = new QTableView;
  9. pTableView->setModel(newMyModel);
  10.  
  11. int width = 0;
  12. int height = 0;
  13. int columns = newMyModel->columnCount();
  14. int rows = newMyModel->rowCount();
  15.  
  16. pTableView->resizeColumnsToContents();
  17.  
  18. for( int i = 0; i < columns; ++i ) {
  19. width += pTableView->columnWidth(i);
  20. }
  21.  
  22. for( int i = 0; i < rows; ++i ) {
  23. height += pTableView->rowHeight(i);
  24. }
  25.  
  26. pTableView->setFixedSize(width, height);
  27. pTableView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  28. pTableView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  29.  
  30. pTableView->render(&printer);
To copy to clipboard, switch view to plain text mode