I have a tableview that is populated via a QSFPM and QSqlRelationalTableModel and depending on different criteria I move columns using the code:
QHeaderView *header
= ui
->printView
->horizontalHeader
();
header->moveSection( 12, 0 );
QHeaderView *header= ui->printView->horizontalHeader();
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:
for (int i=0; i<viewColumns; i++)
{
if (!ui->printView->isColumnHidden(i))
mainTable += "<td><font size=\"3\">" + ui->printView->model()->headerData(i,Qt::Horizontal).toString() + "</font></td>";
}
mainTable += "</tr>";
for (int r=0; r<viewRows; r++ )
{
if (ui->printView->isRowHidden(r))
continue;
mainTable += "<tr>";
for (int c =0; c<viewColumns; c++)
{
if (!ui->printView->isColumnHidden(c))
{
if (ui->printView->model()->index(r,c).data(Qt::BackgroundRole)==Qt::yellow)
mainTable +="<td bgcolor=\"yellow\"><font size=\"3\">" + ui->printView->model()->index(r,c).data(Qt::DisplayRole).toString()+ "</font></td>" ;
else if (ui->printView->model()->index(r,c).data(Qt::BackgroundRole)==Qt::red)
mainTable +="<td bgcolor=\"red\"><font size=\"3\">" + ui->printView->model()->index(r,c).data(Qt::DisplayRole).toString()+ "</font></td>" ;
else
mainTable +="<td><font size=\"3\">" + ui->printView->model()->index(r,c).data(Qt::DisplayRole).toString()+ "</font></td>" ;
}
}
for (int i=0; i<viewColumns; i++)
{
if (!ui->printView->isColumnHidden(i))
mainTable += "<td><font size=\"3\">" + ui->printView->model()->headerData(i,Qt::Horizontal).toString() + "</font></td>";
}
mainTable += "</tr>";
for (int r=0; r<viewRows; r++ )
{
if (ui->printView->isRowHidden(r))
continue;
mainTable += "<tr>";
for (int c =0; c<viewColumns; c++)
{
if (!ui->printView->isColumnHidden(c))
{
if (ui->printView->model()->index(r,c).data(Qt::BackgroundRole)==Qt::yellow)
mainTable +="<td bgcolor=\"yellow\"><font size=\"3\">" + ui->printView->model()->index(r,c).data(Qt::DisplayRole).toString()+ "</font></td>" ;
else if (ui->printView->model()->index(r,c).data(Qt::BackgroundRole)==Qt::red)
mainTable +="<td bgcolor=\"red\"><font size=\"3\">" + ui->printView->model()->index(r,c).data(Qt::DisplayRole).toString()+ "</font></td>" ;
else
mainTable +="<td><font size=\"3\">" + ui->printView->model()->index(r,c).data(Qt::DisplayRole).toString()+ "</font></td>" ;
}
}
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?
Bookmarks