feat(ModelUtils): modelToArray: roles param made optional, support for nested models
This commit is contained in:
parent
22c07efcd9
commit
f1d2646fec
|
@ -15,6 +15,8 @@ class ModelUtilsInternal : public QObject
|
|||
public:
|
||||
explicit ModelUtilsInternal(QObject* parent = nullptr);
|
||||
|
||||
Q_INVOKABLE bool isModel(const QVariant &obj) const;
|
||||
|
||||
Q_INVOKABLE int roleByName(QAbstractItemModel *model,
|
||||
const QString &roleName) const;
|
||||
|
||||
|
|
|
@ -25,6 +25,9 @@ QtObject {
|
|||
if (!model)
|
||||
return []
|
||||
|
||||
if (roles === undefined)
|
||||
roles = roleNames(model)
|
||||
|
||||
const count = model.rowCount()
|
||||
const array = []
|
||||
|
||||
|
@ -34,8 +37,11 @@ QtObject {
|
|||
|
||||
roles.forEach(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
|
||||
})
|
||||
|
||||
|
|
|
@ -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
|
||||
{
|
||||
if (model == nullptr)
|
||||
|
|
Loading…
Reference in New Issue