16 int offsetOf(int row, int column) const;
17 QStringList cities;
18 QVector
19 };
В этой модели мы используем две структуры данных:
01 CityModel::CityModel(QObject *parent)
02 : QAbstractTableModel(parent)
03 {
04 }
Конструктор передает параметр parent базовому классу и больше ничего не делает.
01 int CityModel::rowCount(const QModelIndex &
02 /* родительский элемент */) const
03 {
04 return cities.count();
05 }
06 int CityModel::columnCount(const QModelIndex &
07 /* родительский элемент */) const
08 {
09 return cities.count();
10 }
Поскольку мы имеем квадратную матрицу городов, количество строк и столбцов равно количеству городов в нашем списке.
01 QVariant CityModel::data(const QModelIndex &index, int role) const
02 {
03 if (!index.isValid())
04 return QVariant();
05 if (role == Qt::TextAlignmentRole) {
06 return int(Qt::AlignRight | Qt::AlignVCenter);
07 } else if (role == Qt::DisplayRole) {
08 if (index.row() == index.column())
09 return 0;
10 int offset = offsetOf(index.row(), index.column());
11 return distances[offset];
12 }
13 return QVariant();
14 }
Функция
01 QVariant CityModel::headerData(int section,
02 Qt::Orientation /* ориентация */,
03 int role) const
04 {
05 if (role == Qt::DisplayRole)
06 return cities[section];
07 return QVariant();
08 }
Функция
01 bool CityModel::setData(const QModelIndex &index,
02 const QVariant &value, int role)
03 {
04 if (index.isValid() && index.row() != index.column()
05 && role == Qt::EditRole) {
06 int offset = offsetOf(index.row(), index.column());
07 distances[offset] = value.toInt();
08 QModelIndex transposedIndex = createIndex(
09 index.column(), index.row());
10 emit dataChanged(index, index);
11 emit dataChanged(transposedIndex, transposedIndex);
12 return true;
13 }
14 return false;
15 }
Функция