Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

KDTableToListProxyModel: a flattening proxy model

QVariant MyModel::data(const QModelIndelIndex &index, int role) const override {
    Q_ASSERT(checkIndex(index), QAbstractItemModel::CheckIndexOption::IndexIsValid));
 
    int row = index.row(); // column is 0 by definition, this is a list model
 
    switch (role) {
    case NameRole: return ~~~;
    case PhoneNumberRole: return ~~~;
    }
 
    return {};
}
QHash<int, QByteArray> MyModel::roleNames() const override {
    return { { NameRole, "name" }, { PhoneNumberRole, "phoneNumber" } };
}
ListView {
    model: myModel
    delegate: Item {
        width: parent.width
        height: 30
        Text {
            anchors.left: parent.left
            text: model.name // <-- HERE
        }
        Text {
            anchors.right: parent.right
            text: model.phoneNumber // <-- HERE
        }
    }
}
auto tableToListProxy = new KDTableToListProxyModel;
tableToListProxy->setSourceModel(tableModel);
tableToListProxy->setRoleMapping(0, Qt::UserRole + 0, "name", Qt::DisplayRole);
tableToListProxy->setRoleMapping(0, Qt::UserRole + 1, "flag", Qt::DecorationRole);
tableToListProxy->setRoleMapping(1, Qt::UserRole + 2, "population", Qt::DisplayRole);

// expose tableToListProxy to QML and use it from there
tableToListProxy->setRoleMapping(1, Qt::UserRole + 2, "population", Qt::DisplayRole);

GiuseppeD'Angelo

Giuseppe D’Angelo

Senior Software Engineer

Learn Modern C++

Learn more