TableResult->setRowCount(10);
TableResult->setColumnCount(5);
QTableWidgetItem *tmpItem;
//why create a new item on the heap in every loop pass?
for(int c=0;c<4;c++) {
for(int r=0;r<9;r++){
if (r % 2 == 1)
tmpItem->setForeground(blue)
else
tmpItem->setForeground(red);
TableResult->setItem(r, c, tmpItem);
}
}
TableResult = new QTableWidget;
TableResult->setRowCount(10);
TableResult->setColumnCount(5);
QTableWidgetItem *tmpItem; //why create a new item on the heap in every loop pass?
QBrush blue(Qt::blue);
QBrush red(Qt::red);
for(int c=0;c<4;c++) {
for(int r=0;r<9;r++){
tmpItem= new QTableWidgetItem(tr("%1 x %2").arg(c + 1).arg(r + 1));
if (r % 2 == 1)
tmpItem->setForeground(blue)
else
tmpItem->setForeground(red);
TableResult->setItem(r, c, tmpItem);
}
}
To copy to clipboard, switch view to plain text mode
There you have it, pretty simple.
But if you really just want to alternate the row colors, this would be much better.
Edit:
I almost forgot: you're starting with 1 in both your for-loops. Remember that the TableWidgets indexes start at 0 and end at rowCount - 1. I have corrected that, s.a.
Edit 2:
Of course you can use a delegate with a QTableWidget, it's also part of the model-view architecture.
See here
Bookmarks