C++ (Qt)class MySrcData { public: int myCountRows(void) {return 5000000;} int myCountColumn(void) {return 8000000;} QString getMyDisplayString(int row, int column) { if(row == column) {return QString("Middle");} return QString(QString("Value = ") + QString::number(row+column)); } }; class MyModel : public QAbstractItemModel { Q_OBJECTprotected: MySrcData* SrcData;public: MyModel (MySrcData* Data, QObject *parent = 0); virtual ~MyModel (); virtual QModelIndex parent(const QModelIndex &child) const; virtual bool hasChildren(const QModelIndex &parent) const; virtual int rowCount(const QModelIndex &parent = QModelIndex()) const; virtual int columnCount(const QModelIndex &parent = QModelIndex()) const; virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; virtual Qt::ItemFlags flags(const QModelIndex &index) const;};
C++ (Qt)MyModel ::MyModel (MySrcData* Data, QObject *parent) : QAbstractItemModel (parent){ SrcData = Data;} MyModel ::~MyModel (){ } QModelIndex MyModel::parent(const QModelIndex & /*child*/) const { return QModelIndex();} bool MyModel::hasChildren(const QModelIndex &parent) const { if (parent.model() == this || !parent.isValid()) { return rowCount(parent) > 0 && columnCount(parent) > 0; } return false;} int MyModel::rowCount(const QModelIndex &parent) const { if (parent.isValid()) {return 0;} return SrcData->myCountRows();} int MyModel::columnCount(const QModelIndex &parent) const { if (parent.isValid()) {return 0;} return SrcData->myCountColumn();} QModelIndex MyModel::index(int row, int column, const QModelIndex &parent) const { if (parent.isValid()) {return QModelIndex();} if (row < 0 || row >= SrcData->myCountRows()) {return QModelIndex();} if (column < 0 || column >= SrcData->myCountColumn()) {return QModelIndex();} return createIndex(row, column, (void*)NULL);} QVariant MyModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) {return QVariant();} if (index.row() < 0 || index.row() >= SrcData->myCountRows()) {return QVariant();} if (index.column() < 0 || index.column() >= SrcData->myCountColumn()) {return QVariant();} if (role == Qt::DisplayRole) { return SrcData->getMyDisplayString(index.row(), index.column()); } else if(role == Qt::BackgroundColorRole) { if(index.row() == index.column()) {return QVariant();} if(index.row() <= index.column()) {return QVariant(QColor(Qt::green));} return QVariant(QColor(Qt::yellow)); } return QVariant();} Qt::ItemFlags MyModel::flags(const QModelIndex &index) const { return Qt::ItemIsEnabled | Qt::ItemIsSelectable;}
C++ (Qt){ ui.setupUi(this); MySrcData* Data = new MySrcData(); MyModel* Model = new MyModel(Data); ui.tableView->setModel(Model);}