title: Printing QTableView with vertical and horizontal headers
body: <p>I have a program that gets data from SQL (using QSQLQuery) and puts the result into a QTableView. </p>
  
  <p>For user ease of reading I needed to transpose the SQL output (swap rows and columns) but couldn't achieve this easily in SQL (even using PIVOT). Instead I used a proxy model in Qt which works great.</p>
  
  <p>Problem is I need to be able to print the entire QTableView. I can print the contents of the table with the horizontal headers but for the life of me, cant work out how to print the vertical headers (technically the first column since each row has a label due to the transposition). Qt doesn't recognize the first 'column' as being a column of row-names, nor do I know how to get it to treat it as vertical headers for printing. </p>
  
  <p>Proxy model code:</p>
  
  <pre><code>class Horizontal_proxy_model : public QAbstractProxyModel {
  public:
   Horizontal_proxy_model(QObject * parent = 0);
  QModelIndex mapToSource(const QModelIndex &amp;proxyIndex) const;
  QModelIndex mapFromSource(const QModelIndex &amp;sourceIndex) const;
  QModelIndex index(int row, int column, const QModelIndex &amp;parent = 
  QModelIndex()) const;
  QModelIndex parent(const QModelIndex &amp;child) const;
  int rowCount(const QModelIndex &amp;parent) const;
  int columnCount(const QModelIndex &amp;parent) const;
  QVariant headerData(int section, Qt::Orientation orientation, int role) 
  const;
  };
  
  Horizontal_proxy_model::Horizontal_proxy_model(QObject *parent) :
  QAbstractProxyModel(parent) {
  }
  
  QModelIndex Horizontal_proxy_model::mapToSource(const QModelIndex 
  &amp;proxyIndex) const {
  if (sourceModel()) {
    return sourceModel()-&gt;index(proxyIndex.column(), proxyIndex.row());
  } else {
   return QModelIndex();
  }
  }
  
   QModelIndex Horizontal_proxy_model::mapFromSource(const QModelIndex 
   &amp;sourceIndex) const {
   return index(sourceIndex.column(), sourceIndex.row());
   }
  
   QModelIndex Horizontal_proxy_model::index(int row, int column, const 
   QModelIndex &amp;) const {
   return createIndex(row, column, (void*) 0);
   }
  
   QModelIndex Horizontal_proxy_model::parent(const QModelIndex &amp;) const {
   return QModelIndex();
   }
  
   int Horizontal_proxy_model::rowCount(const QModelIndex &amp;) const {
   return sourceModel() ? sourceModel()-&gt;columnCount() : 0;
   }
  
  int Horizontal_proxy_model::columnCount(const QModelIndex &amp;) const {
  return sourceModel() ? sourceModel()-&gt;rowCount() : 0;
  }
  
  QVariant Horizontal_proxy_model::headerData(
         int section, Qt::Orientation orientation, int role) const {
   if (!sourceModel()) { return QVariant(); }
   Qt::Orientation new_orientation = orientation == Qt::Horizontal ?
    Qt::Vertical : Qt::Horizontal;
   return sourceModel()-&gt;headerData(section, new_orientation, role);
   }
  </code></pre>
  
  <p>Here are the basics of how I'm trying to print;</p>
  
  <pre><code>void Snapshot_finance::on_pushButton_print_clicked()
  {
  QString html;
  html = "&lt;html&gt;&lt;body&gt;&lt;table border=\"0\"&gt;";
  for(int row = 0; row &lt; ui-&gt;tableView-&gt;model()-&gt;rowCount(); row++) {
    html += "&lt;tr&gt;";
    for(int column = 0; column &lt; ui-&gt;tableView-&gt;model()-&gt;columnCount(); 
  column++) {
  QString data = ui-&gt;tableView-&gt;model()-&gt;data(ui-&gt;tableView-&gt;model()-&gt; 
  index(row, column), Qt::DisplayRole).toString();
      html += "&lt;td&gt;" + data + "&lt;/td&gt;";
    }
    html += "&lt;/tr&gt;";
  }
  html += "&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;";
  
  QPrinter printer;
  QPrintDialog *dialog = new QPrintDialog(&amp;printer);
  if(dialog-&gt;exec() == QDialog::Accepted) {
    QTextDocument document;
    document.setHtml(html);
    document.print(&amp;printer);
  }
  }
  </code></pre>
  
  <p>Really appreciate any help or advice!</p>