I have a tableview that is populated via a QSFPM and QSqlRelationalTableModel and depending on different criteria I move columns using the code:
Qt Code:
  1. QHeaderView *header= ui->printView->horizontalHeader();
  2. header->moveSection( 12, 0 );
To copy to clipboard, switch view to plain text mode 
The Tableview displays properly and then I want to print the table. I do so using a HTML and iterate through the tableview using:
Qt Code:
  1. for (int i=0; i<viewColumns; i++)
  2. {
  3. if (!ui->printView->isColumnHidden(i))
  4. mainTable += "<td><font size=\"3\">" + ui->printView->model()->headerData(i,Qt::Horizontal).toString() + "</font></td>";
  5. }
  6. mainTable += "</tr>";
  7.  
  8. for (int r=0; r<viewRows; r++ )
  9. {
  10. if (ui->printView->isRowHidden(r))
  11. continue;
  12.  
  13. mainTable += "<tr>";
  14.  
  15. for (int c =0; c<viewColumns; c++)
  16. {
  17. if (!ui->printView->isColumnHidden(c))
  18. {
  19. if (ui->printView->model()->index(r,c).data(Qt::BackgroundRole)==Qt::yellow)
  20. mainTable +="<td bgcolor=\"yellow\"><font size=\"3\">" + ui->printView->model()->index(r,c).data(Qt::DisplayRole).toString()+ "</font></td>" ;
  21. else if (ui->printView->model()->index(r,c).data(Qt::BackgroundRole)==Qt::red)
  22. mainTable +="<td bgcolor=\"red\"><font size=\"3\">" + ui->printView->model()->index(r,c).data(Qt::DisplayRole).toString()+ "</font></td>" ;
  23. else
  24. mainTable +="<td><font size=\"3\">" + ui->printView->model()->index(r,c).data(Qt::DisplayRole).toString()+ "</font></td>" ;
  25. }
  26.  
  27. }
To copy to clipboard, switch view to plain text mode 
The HTML has the columns in the original order but it displays in the modified order. Why is this? I thought the tableView just displays the info from the model and it doesn't know about the underlying data structure. I might be able to correct this with some fancy coding by setting variables to keep track of the column changes, but think there must be an easier way. Any ideas?