MyPlugin/MyPlugin.cpp:
#include <ViewModels/MyListClass.h> #include <ViewModels/DisplayViewModel.h> qmlRegisterUncreatableType<MyListClass>( a_uri, 1, 0, "MyListClass", "Use access via DisplayViewModel instead"); qmlRegisterType<DisplayViewModel>( a_uri, 1, 0, "DisplayViewModel");
Utils/MyQMLListUtils.h
#define MY_DECLARE_QML_LIST(type, name, owner, prop) \ QQmlListProperty<type> name(){ \ return QQmlListProperty<type>( \ this, 0,&owner::count ## type ## For ## name ## List, \ &owner::at ## type ## For ## name ## List); \ } \ static int count ## type ## For ## name ## List(QQmlListProperty<type>*property){ \ owner *m = qobject_cast<owner *>(property->object); \ return m->prop.size(); \ } \ static type *at ## type ## For ## name ## List( \ QQmlListProperty<type>*property, int index){ \ owner *m = qobject_cast<owner *>(property->object); \ return m->prop[index]; \ }
ViewModels/DisplayViewModel.h
#ifndef DISPLAYVIEWMODEL_H #define DISPLAYVIEWMODEL_H #include <QObject> #include <QtQml> #include <ViewModels/MyListClass.h> #include <Utils/MyQMLListUtils.h> class DisplayViewModel : public QObject { Q_OBJECT Q_PROPERTY(constQString title READ title WRITE setTitle NOTIFY titleChanged ) Q_PROPERTY(constQList<MyListClass*> objects READ objects NOTIFY objectsChanged ) Q_PROPERTY( QQmlListProperty<MyListClass> objectList READ objectList NOTIFY objectsChanged ) public: explicit DisplayViewModel( QObject *a_parent = nullptr ); explicit DisplayViewModel( const QString &a_title, QList<MyListClass*> a_objects, QObject *a_parent = nullptr ); const QString title() { return m_title; } void setTitle( const QString &a_title ); const QList<MyListClass*> objects () { return m_objects; } Q_INVOKABLE void appendObject( MyListClass *a_object); Q_INVOKABLE void deleteObject( MyListClass *a_object); Q_INVOKABLE void reset( ); protected: MY_DECLARE_QML_LIST(MyListClass, objectList, DisplayViewModel, m_objects) signals: void titleChanged(); void objectsChanged(); private: QString m_title; QList<MyListObject*> m_objects; }; #endif// DISPLAYVIEWMODEL_H
DisplayViewModel.cpp
#include "DisplayViewModel.h" DisplayViewModel::DisplayViewModel( const QString &a_title, QList<MyListClass*> a_objects, QObject *a_parent ) : QObject ( a_parent ) , m_title ( a_title ) , m_objects ( a_objects ) { foreach (MyListClass* mobject, m_objects) { mobject->setParent (this); } } void DisplayViewModel::setTitle (const QString &a_title ) { if ( m_title != a_title ) { m_title = a_title; emit titleChanged(); } } void DisplayViewModel::reset( ) { foreach ( MyListClass *mobject, m_objects ) { mobject->deleteLater(); } m_objects.clear(); emit objectsChanged(); } void DisplayViewModel::appendObject( MyListClass *a_object ) { a_object->setParent( this ); m_objects.append( a_object ); emit objectsChanged(); } void DisplayViewModel::deleteObject( MyListClass *a_object ) { if (m_objects.contains( a_object )) { m_objects.removeOne( a_object ); a_object->deleteLater(); emit objectsChanged(); } }
Tester.cpp
#include <ViewModels/DisplayViewModel.h> #include <ViewModels/MyListClass.h> QList<MyListClass*> objectList; for( int i = 0; i < 100 ; ++i ) { objectList.append ( new MyListClass (i) ); } DisplayViewModel *viewModel = new DisplayViewModel (objectList); viewModel->appendObject ( new MyListClass (101) );
Display.qml
import QtQuick 2.5 import MyPlugin 1.0 Repeater { property DisplayViewModel viewModel: DisplayViewModel { } model: viewModel.objectList delegate: Item { property MyListClass object: modelData Text { text: object.property } } }