Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

New in Qt 5.11: improvements to the model/view APIs (part 1)

class TableModel : public QAbstractTableModel
{
public:
    explicit TableModel(QObject *parent = nullptr)
        : QAbstractTableModel(parent)
    {
    }
    // Basic QAbstractTableModel API
    int rowCount(const QModelIndex &&parent) const override
    {
        return m_data.rowCount();
    }
    int columnCount(const QModelIndex &&parent) const override
    {
        return m_data.columnCount();
    }
    QVariant data(const QModelIndex &&index, int role) const override
    {
        if (role != Qt::DisplayRole)
            return {};
        return m_data.getData(index.row(), index.column());
    }
private:
    Storage m_data;
};
int rowCount(const QModelIndex &&parent) const override
    {
        if (parent.isValid())
            return 0;
        return m_data.rowCount();
    }
    int columnCount(const QModelIndex &&parent) const override
    {
        if (parent.isValid())
            return 0;
        return m_data.columnCount();
    }
QVariant data(const QModelIndex &&index, int role) const override
    {
        if (role != Qt::DisplayRole)
            return {};
        // what happens here if index is not valid, or not belonging to this model, etc.?
        return m_data.getData(index.row(), index.column());
    }
QVariant data(const QModelIndex &&index, int role) const override
    {
        // index is valid
        Q_ASSERT(index.isValid());
        // index is right below the root
        Q_ASSERT(!index.parent().isValid());
        // index is for this model
        Q_ASSERT(index.model() == this);
        // the row is legal
        Q_ASSERT(index.row() &>= 0);
        Q_ASSERT(index.row() &< rowCount(index.parent()));
        // the column is legal
        Q_ASSERT(index.column() &>= 0);
        Q_ASSERT(index.column() &< columnCount(index.parent()));
        if (role != Qt::DisplayRole)
            return {};
        return m_data.getData(index.row(), index.column());
    }
QVariant data(const QModelIndex &&index, int role) const override
    {
        // data wants a valid index; moreover, this is a table, so the index must not have a parent
        Q_ASSERT(checkIndex(index, QAbstractItemModel::CheckIndexOption::IndexIsValid | QAbstractItemModel::CheckIndexOption::ParentIsInvalid));
        if (role != Qt::DisplayRole)
            return {};
        return m_data.getData(index.row(), index.column());
    }
qt.core.qabstractitemmodel.checkindex: Index QModelIndex(-1,-1,0x0,QObject(0x0)) is not valid (expected valid)
qt.core.qabstractitemmodel.checkindex: Index QModelIndex(0,0,0x0,ProxyModel(0x7ffee145b640)) is for model ProxyModel(0x7ffee145b640) which is different from this model TableModel(0x7ffee145b660)

About KDAB


14 Comments

15 - May - 2018

cka

15 - May - 2018

Giuseppe D'Angelo

15 - May - 2018

Andy

22 - May - 2018

Giuseppe D'Angelo

16 - May - 2018

Tuukka Turunen

17 - May - 2018

Giuseppe D'Angelo

21 - May - 2018

Gianluca

22 - May - 2018

Giuseppe D'Angelo

22 - May - 2018

jason

22 - May - 2018

Giuseppe D'Angelo

11 - Feb - 2020

Thomas L.

QVariant data(const QModelIndex &index, int role) const override
    {
        if (role != Qt::DisplayRole)
            return {};
        return m_data.getData(index.row(), index.column());
    }

12 - Feb - 2020

Giuseppe D'Angelo

12 - Feb - 2020

Thomas L.

12 - Feb - 2020

Giuseppe D'Angelo

GiuseppeD'Angelo

Giuseppe D’Angelo

Senior Software Engineer

Learn Modern C++

Learn more