Documentation: Update CommonTableModel/StringListModel snippets

Update CommonTableModel/StringListModel snippets:
  - 0 -> nullptr
  - use 'override'
  - replace "" with QString()
  - use QStringLiteral instead QString
  - pass role to dataChanged() signal

Change-Id: I5949d1bd6fee3186f12191f1f6235ae18908096e
Reviewed-by: Topi Reiniö <topi.reinio@qt.io>
This commit is contained in:
Christian Ehrlicher 2018-02-02 21:08:44 +01:00
parent a0717c60b3
commit 076087717e
4 changed files with 28 additions and 28 deletions

View File

@ -68,7 +68,7 @@ TableModel::TableModel(int rows, int columns, QObject *parent)
QStringList newList;
for (int column = 0; column < qMax(1, columns); ++column) {
newList.append("");
newList.append(QString());
}
for (int row = 0; row < qMax(1, rows); ++row) {
@ -129,9 +129,9 @@ QVariant TableModel::headerData(int section, Qt::Orientation orientation,
return QVariant();
if (orientation == Qt::Horizontal)
return QString("Column %1").arg(section);
return QStringLiteral("Column %1").arg(section);
else
return QString("Row %1").arg(section);
return QStringLiteral("Row %1").arg(section);
}
/*!
@ -164,7 +164,7 @@ bool TableModel::setData(const QModelIndex &index,
return false;
rowList[index.row()][index.column()] = value.toString();
emit dataChanged(index, index);
emit dataChanged(index, index, {role});
return true;
}
@ -180,7 +180,7 @@ bool TableModel::insertRows(int position, int rows, const QModelIndex &parent)
for (int row = 0; row < rows; ++row) {
QStringList items;
for (int column = 0; column < columns; ++column)
items.append("");
items.append(QString());
rowList.insert(position, items);
}
@ -202,7 +202,7 @@ bool TableModel::insertColumns(int position, int columns,
for (int row = 0; row < rows; ++row) {
for (int column = position; column < columns; ++column) {
rowList[row].insert(position, "");
rowList[row].insert(position, QString());
}
}

View File

@ -60,22 +60,22 @@ class TableModel : public QAbstractTableModel
Q_OBJECT
public:
TableModel(int rows = 1, int columns = 1, QObject *parent = 0);
TableModel(int rows = 1, int columns = 1, QObject *parent = nullptr);
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role) const override;
QVariant headerData(int section, Qt::Orientation orientation,
int role = Qt::DisplayRole) const;
int role = Qt::DisplayRole) const override;
Qt::ItemFlags flags(const QModelIndex &index) const;
Qt::ItemFlags flags(const QModelIndex &index) const override;
bool setData(const QModelIndex &index, const QVariant &value,
int role = Qt::EditRole);
int role = Qt::EditRole) override;
bool insertRows(int position, int rows, const QModelIndex &parent = QModelIndex());
bool insertColumns(int position, int columns, const QModelIndex &parent = QModelIndex());
bool removeRows(int position, int rows, const QModelIndex &parent = QModelIndex());
bool removeColumns(int position, int columns, const QModelIndex &parent = QModelIndex());
bool insertRows(int position, int rows, const QModelIndex &parent = QModelIndex()) override;
bool insertColumns(int position, int columns, const QModelIndex &parent = QModelIndex()) override;
bool removeRows(int position, int rows, const QModelIndex &parent = QModelIndex()) override;
bool removeColumns(int position, int columns, const QModelIndex &parent = QModelIndex()) override;
private:
QList<QStringList> rowList;

View File

@ -135,9 +135,9 @@ QVariant StringListModel::headerData(int section, Qt::Orientation orientation,
return QVariant();
if (orientation == Qt::Horizontal)
return QString("Column %1").arg(section);
return QStringLiteral("Column %1").arg(section);
else
return QString("Row %1").arg(section);
return QStringLiteral("Row %1").arg(section);
}
//! [2]
@ -174,7 +174,7 @@ bool StringListModel::setData(const QModelIndex &index,
if (index.isValid() && role == Qt::EditRole) {
stringList.replace(index.row(), value.toString());
emit dataChanged(index, index);
emit dataChanged(index, index, {role});
return true;
}
//! [4] //! [5]

View File

@ -61,26 +61,26 @@ class StringListModel : public QAbstractListModel
Q_OBJECT
public:
StringListModel(const QStringList &strings, QObject *parent = 0)
StringListModel(const QStringList &strings, QObject *parent = nullptr)
: QAbstractListModel(parent), stringList(strings) {}
int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role) const override;
QVariant headerData(int section, Qt::Orientation orientation,
//! [0] //! [1]
int role = Qt::DisplayRole) const;
int role = Qt::DisplayRole) const override;
//! [1]
//! [2]
Qt::ItemFlags flags(const QModelIndex &index) const;
Qt::ItemFlags flags(const QModelIndex &index) const override;
bool setData(const QModelIndex &index, const QVariant &value,
//! [2] //! [3]
int role = Qt::EditRole);
int role = Qt::EditRole) override;
//! [3]
//! [4]
bool insertRows(int position, int rows, const QModelIndex &index = QModelIndex());
bool removeRows(int position, int rows, const QModelIndex &index = QModelIndex());
bool insertRows(int position, int rows, const QModelIndex &index = QModelIndex()) override;
bool removeRows(int position, int rows, const QModelIndex &index = QModelIndex()) override;
//! [4]
//! [5]