here is a compilable example
Qt Code:
  1. #include <QtGui>
  2. #include <QApplication>
  3.  
  4. int main(int argc, char **argv)
  5. {
  6. QApplication app(argc, argv);
  7.  
  8. QTableWidget table(10, 10);
  9. QAbstractItemModel *model = table.model();
  10. for (int row = 0; row < table.rowCount(); ++row) {
  11. for (int column = 0; column < table.columnCount(); ++column) {
  12. QTableWidgetItem *newItem = new QTableWidgetItem(QObject::tr("%1").arg((row+1)*(column+1)));
  13. table.setItem(row, column, newItem);
  14. const QModelIndex index = model->index(row, column);
  15. model->setData(index, QColor(qrand()%255, qrand()%255, qrand()%255), Qt::BackgroundRole);
  16. }
  17. }
  18. table.show();
  19.  
  20. return app.exec();
  21. }
To copy to clipboard, switch view to plain text mode