feat(StatusQ): Generic attachd type for providing model count

Closes: #15374
This commit is contained in:
Michał Cieślak 2024-06-28 11:25:03 +02:00
parent 0b16749e54
commit 8bf44971c4
4 changed files with 57 additions and 0 deletions

View File

@ -101,6 +101,7 @@ add_library(StatusQ SHARED
include/StatusQ/formatteddoubleproperty.h
include/StatusQ/functionaggregator.h
include/StatusQ/leftjoinmodel.h
include/StatusQ/modelcount.h
include/StatusQ/modelentry.h
include/StatusQ/modelsyncedcontainer.h
include/StatusQ/modelutilsinternal.h
@ -128,6 +129,7 @@ add_library(StatusQ SHARED
src/formatteddoubleproperty.cpp
src/functionaggregator.cpp
src/leftjoinmodel.cpp
src/modelcount.cpp
src/modelentry.cpp
src/modelutilsinternal.cpp
src/movablemodel.cpp

View File

@ -0,0 +1,21 @@
#pragma once
#include <QObject>
#include <QQmlEngine>
class ModelCount : public QObject {
Q_OBJECT
Q_PROPERTY(int count READ count NOTIFY countChanged)
public:
explicit ModelCount(QObject* parent = nullptr);
static ModelCount* qmlAttachedProperties(QObject* object);
int count() const;
signals:
void countChanged();
};
QML_DECLARE_TYPEINFO(ModelCount, QML_HAS_ATTACHED_PROPERTIES)

View File

@ -0,0 +1,29 @@
#include "StatusQ/modelcount.h"
#include <QAbstractItemModel>
ModelCount::ModelCount(QObject* parent) : QObject(parent)
{
auto model = qobject_cast<QAbstractItemModel*>(parent);
if (model == nullptr)
return;
connect(model, &QAbstractItemModel::rowsInserted, this, &ModelCount::countChanged);
connect(model, &QAbstractItemModel::rowsRemoved, this, &ModelCount::countChanged);
connect(model, &QAbstractItemModel::modelReset, this, &ModelCount::countChanged);
connect(model, &QAbstractItemModel::layoutChanged, this, &ModelCount::countChanged);
}
ModelCount* ModelCount::qmlAttachedProperties(QObject* object)
{
return new ModelCount(object);
}
int ModelCount::count() const
{
if (auto model = qobject_cast<QAbstractItemModel*>(parent()))
return model->rowCount();
return 0;
}

View File

@ -11,6 +11,8 @@
#include "StatusQ/formatteddoubleproperty.h"
#include "StatusQ/functionaggregator.h"
#include "StatusQ/leftjoinmodel.h"
#include "StatusQ/modelcount.h"
#include "StatusQ/modelentry.h"
#include "StatusQ/modelutilsinternal.h"
#include "StatusQ/movablemodel.h"
#include "StatusQ/objectproxymodel.h"
@ -69,6 +71,9 @@ public:
qmlRegisterType<ModelEntry>("StatusQ", 0, 1, "ModelEntry");
qmlRegisterType<SnapshotObject>("StatusQ", 0, 1, "SnapshotObject");
qmlRegisterUncreatableType<ModelCount>("StatusQ", 0, 1,
"ModelCount", "This is attached type, cannot be created directly.");
qmlRegisterSingletonType<ModelUtilsInternal>(
"StatusQ.Internal", 0, 1, "ModelUtils", &ModelUtilsInternal::qmlInstance);