feat(ModelUtils): modelToArray: roles param made optional, support for nested models

This commit is contained in:
Michał Cieślak 2023-07-27 00:30:31 +02:00 committed by Michał
parent 22c07efcd9
commit f1d2646fec
3 changed files with 17 additions and 1 deletions

View File

@ -15,6 +15,8 @@ class ModelUtilsInternal : public QObject
public: public:
explicit ModelUtilsInternal(QObject* parent = nullptr); explicit ModelUtilsInternal(QObject* parent = nullptr);
Q_INVOKABLE bool isModel(const QVariant &obj) const;
Q_INVOKABLE int roleByName(QAbstractItemModel *model, Q_INVOKABLE int roleByName(QAbstractItemModel *model,
const QString &roleName) const; const QString &roleName) const;

View File

@ -25,6 +25,9 @@ QtObject {
if (!model) if (!model)
return [] return []
if (roles === undefined)
roles = roleNames(model)
const count = model.rowCount() const count = model.rowCount()
const array = [] const array = []
@ -34,8 +37,11 @@ QtObject {
roles.forEach(role => { roles.forEach(role => {
const entry = modelItem[role] const entry = modelItem[role]
const isModel = Internal.ModelUtils.isModel(entry)
if (entry !== undefined) if (isModel)
arrayItem[role] = modelToArray(entry)
else if (entry !== undefined)
arrayItem[role] = entry arrayItem[role] = entry
}) })

View File

@ -9,6 +9,14 @@ ModelUtilsInternal::ModelUtilsInternal(QObject* parent)
{ {
} }
bool ModelUtilsInternal::isModel(const QVariant &obj) const
{
if (!obj.canConvert<QObject*>())
return false;
return qobject_cast<QAbstractItemModel*>(obj.value<QObject*>()) != nullptr;
}
QStringList ModelUtilsInternal::roleNames(QAbstractItemModel *model) const QStringList ModelUtilsInternal::roleNames(QAbstractItemModel *model) const
{ {
if (model == nullptr) if (model == nullptr)