PDA

View Full Version : Tableview after Model move section



poporacer
13th September 2011, 01:33
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 );
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>" ;
}

}
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?

norobro
13th September 2011, 04:12
That is the expected behavior. QHeaderView::moveSection() swaps columns in the view not the model.

ChrisW67
13th September 2011, 05:08
You should look at QHeaderView::logicalIndex() and QHeaderView::visualIndex() and the mapping they provide.

poporacer
13th September 2011, 05:43
I think I almost understand but not quite. I tried:

QHeaderView *header= ui->printView->horizontalHeader();
int column;
QString mainTable = "<table border=1 width = \"100%\"> <tr>";
int viewColumns =ui->printView->model()->columnCount();
int viewRows =ui->printView->model()->rowCount();

for (int i=0; i<viewColumns; i++)
{
if (!ui->printView->isColumnHidden(i))
column=header->logicalIndex(i);
mainTable += "<td><font size=\"3\">" + ui->printView->model()->headerData(column,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++)
{
column=header->logicalIndex(c);
if (!ui->printView->isColumnHidden(c))
{
if (ui->printView->model()->index(r,column).data(Qt::BackgroundRole)==Qt::yell ow)
mainTable +="<td bgcolor=\"yellow\"><font size=\"3\">" + ui->printView->model()->index(r,column).data(Qt::DisplayRole).toString()+ "</font></td>" ;
else if (ui->printView->model()->index(r,column).data(Qt::BackgroundRole)==Qt::red)
mainTable +="<td bgcolor=\"red\"><font size=\"3\">" + ui->printView->model()->index(r,column).data(Qt::DisplayRole).toString()+ "</font></td>" ;
else
mainTable +="<td><font size=\"3\">" + ui->printView->model()->index(r,column).data(Qt::DisplayRole).toString()+ "</font></td>" ;
}

}
But it didn't work...the table was all messed up! I am off to bed...gonna sleep on it and try again tomorrow. Am I close?

wysota
13th September 2011, 09:23
logicalIndex() returns the original order, use visualIndex() to get the order displayed by the table.

poporacer
14th September 2011, 01:13
Thanks everyone....I found the problem. It wasnt with the

column=header->logicalIndex()
This does return the index of the correct item in the tableview. The problem was with the test:

if (!ui->printView->isColumnHidden(i))
It should be:

column=header->logicalIndex(i);
if (!header->isSectionHidden(column))
I appreciate the help!