PDA

View Full Version : color specified cell in a tableview



poporacer
26th March 2011, 19:41
I am trying to print a report based on a dialog. On the dialog there is a table based on a QSqlRelationalModel and a QTable view. The user will be able to sort the table and delete rows in the displayed table. Then the user can print the report to reflect the changes. I am converting the information to htm to be able to print it. I am able to create the report with the exception three things. In the tableview, I need to highlight the cell containing the highest and lowest number in a particular column. I am not sure if this is the best way as I see there is a QModelIndexClass, but I am not sure how to implement it. I am able to get the column and row from the tableview of the cells that need to be highlighted through this code. I am not sure if this will actually work as I believe the model will contain ALL the records and not reflect which ones were deleted in the tableview.

float fastTime=0;
float slowTime=0;
int rowFast=0;
int rowSlow=0;
int step;

while(printModel->canFetchMore())
printModel->fetchMore();//to show all the rows
int numRows = printModel->rowCount();
QSqlRecord record;

//get fastest and slowest times and save row

for (step=0; step <= numRows; step++)
{
record = printModel->record(step);
if (record.value(32).toFloat() < fastTime)
{
fastTime=record.value(32).toFloat();
rowFast = step;

}
if (record.value(32).toFloat() > slowTime)
{
slowTime=record.value(32).toFloat();
rowSlow = step;
}
}
I have tried searching but I was unable to find how to highlight one cell based on a row,col criteria.
So what I need is:
1) a way to iterate through a tableview to get the data. (in QModelIndexClass this will list all items?)

2) how to go to the specified cell and highlight the background color.

3) How to identify the items displayed in the tableView to get the data to populate the html code (the table view can hide several columns and if I use QModelIndex I will have to iterate through each hidden column and check if it is hidden?)

I have tried several different codes but none worked, so I am not sure which code to post as an example. I am sure it is probably because I did not implement them correctly. I have tried using QSortFilterProxyModel, creating a QItemDelegate with a paint function.

So what would be the best methods to achieve this?

majorwoody
29th March 2011, 20:52
1) a way to iterate through a tableview to get the data. (in QModelIndexClass this will list all items?)

2) how to go to the specified cell and highlight the background color.

3) How to identify the items displayed in the tableView to get the data to populate the html code (the table view can hide several columns and if I use QModelIndex I will have to iterate through each hidden column and check if it is hidden?)


1. You can iterate with QAbstractItemModel.index(row, column) with row from 0 to QAbstractItemModel.rowCount() and column = column with the value to search for

2. Look at http://doc.qt.nokia.com/4.7/modelview.html under 2.2

3. When you only want to export the columns that are visible, then you have to check all columns if they are visible.