GenericListModel Class

  • GenericListModel
  • class Esri::ArcGISRuntime::Toolkit::GenericListModel

    This ListModel automatically exposes a list of QObjects with properties as a list of elements where each property is mapped to a user role. More...

    Header: #include <GenericListModel>
    Inherits: QAbstractListModel

    Public Types

    typedef FlagsCallback

    Properties

    Public Functions

    GenericListModel(const QMetaObject *elementType, QObject *parent = nullptr)
    GenericListModel(QObject *parent = nullptr)
    virtual ~GenericListModel() override
    bool append(QList<QObject *> objects)
    bool append(QObject *object)
    QString decorationPropertyName()
    QString displayPropertyName()
    T *element(const QModelIndex &index) const
    const QMetaObject *elementType() const
    void setDisplayPropertyName(const QString &propertyName)
    void setElementType(const QMetaObject *metaObject)
    void setFlagsCallback(Func &&f)
    QString tooltipPropertyName()

    Reimplemented Public Functions

    virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override
    virtual Qt::ItemFlags flags(const QModelIndex &index) const override
    virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override
    virtual bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override
    virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override
    virtual QHash<int, QByteArray> roleNames() const override
    virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override
    virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override

    Signals

    void countChanged()

    Detailed Description

    A GenericListModel addresses the need to take a collection of properties, on a QObject and expose them in a view (Widgets or QML).

    A GenericListModel takes a QMetaObject as an `elementType`. Each property in the QMetaObject is scanned and assigned a role in the order in which the property was declared in the class.

    For example, given a class Foo:

    class Foo : public QObject
    {
      Q_PROPERTY(int propertyA READ propertyA WRITE setPropertyA NOTIFY propertyAChanged)
      Q_PROPERTY(QString propertyB READ propertyB NOTIFY propertyBChanged)
      Q_PROPERTY(bool propertyC READ propertyC CONSTANT)
    };

    Then a GenericListModel will expose the following roles:

    NameValueType
    modelDataQt::UserRoleFoo*
    propertyAQt::UserRole + 1int
    propertyBQt::UserRole + 2QString
    propertyCQt::UserRole + 3bool

    A hard-coded modelData role is always exposed as Qt::UserRole, followed by each property on Foo in order of declaration.

    Both data and setData can be called respectively on these roles. Likewise, every time a property updates, the notify signal will be consumed and this model will emit a dataChanged signal.

    The class definition for a given QObject must have a constructor of form Q_INVOKABLE Foo(QObject* parent = nullptr). An ElementType that does not have this form of constructor will trigger undefined behaviour when insertRows is called.

    Member Type Documentation

    typedef GenericListModel::FlagsCallback

    This is a typedef for a pointer to a function with the following signature

    QFlags<Qt::ItemFlag> myFlagsCallback(const QModelIndex& index);

    Property Documentation

    count : const int

    This property holds the current number of elements in this list model.

    Notifier signal:

    void countChanged()

    Member Function Documentation

    GenericListModel::GenericListModel(const QMetaObject *elementType, QObject *parent = nullptr)

    Constructor

    • elementType the QMetaObject that defines the roles this object will expose.
    • parent Owning parent QObject.

    GenericListModel::GenericListModel(QObject *parent = nullptr)

    Constructor

    Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

    [signal] void GenericListModel::countChanged()

    Emitted when the size of the list changes.

    Note: Notifier signal for property count.

    [override virtual] GenericListModel::~GenericListModel()

    Destructor.

    bool GenericListModel::append(QList<QObject *> objects)

    Helper function append additional objects to this list.

    • objects List of object to append to this model.

    The append will fail if any object is null. The append will fail if any objects' MetaType does not match elementType.

    Return true If objects were appended.

    Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

    bool GenericListModel::append(QObject *object)

    Helper function append an additional object to this list.

    The append will fail if object is null. The append will fail if the MetaType of object does not match elementType.

    • object Object to append to this model.

    Returns true if object was appended.

    Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

    [override virtual] QVariant GenericListModel::data(const QModelIndex &index, int role = Qt::DisplayRole) const

    Reimplements: QAbstractItemModel::data(const QModelIndex &index, int role) const.

    For a given index in the model, returns the data read from that object's property and returns the data as a QVariant.

    If role is Qt::DisplayRole or Qt::EditRole, then the property read from is the property with the name defined by the displayPropertyName field.

    If the role is Qt::UserRole, then the QVariant return is the pointer to the underlying QObject in the list.

    If the role is is any value greater than Qt::UserRole, then the role is mapped to the property in the object associated with that role. See roleNames to get a list of those properties.

    • index The index of the object in the model to query
    • role The role to query data for which maps to properties

    Returns the data as read from the property cast as a QVariant.

    See also setData() and roleNames.

    QString GenericListModel::decorationPropertyName()

    Returns the name of the property which has been elevated to be used as the Qt::DecorationRole in this model.

    Returns name of property.

    QString GenericListModel::displayPropertyName()

    Returns the name of the property which has been elevated to be used as the Qt::DisplayRole and Qt::EditRole in this model.

    Returns name of property.

    See also setDisplayPropertyName().

    template <typename T> T *GenericListModel::element(const QModelIndex &index) const

    Helper function to grab the object at index and cast to type T.

    • T The type to cast to.
    • index Index of element to grab.

    Returns the cast of object in list to T. Nullptr is returned if the cast fails.

    const QMetaObject *GenericListModel::elementType() const

    Return the QMetaObject which dictates all the roles this model exposes.

    Returns a QMetaObject with the desired properties.

    See also setElementType().

    [override virtual] Qt::ItemFlags GenericListModel::flags(const QModelIndex &index) const

    Reimplements: QAbstractListModel::flags(const QModelIndex &index) const.

    Overridden QAbstractListModel function. Returns the Qt::ItemFlags for each item in the list.

    If setFlagsCallback is set, Qt::ItemsFlags are returned from the call-back. Otherwise, the default QAbstractListModel::flags will be called. Flags returned are used from the QListView to apply visual properties.

    • index Index of item

    See also Qt::ItemFlags and QFlags.

    [override virtual] QVariant GenericListModel::headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const

    Reimplements: QAbstractItemModel::headerData(int section, Qt::Orientation orientation, int role) const.

    Returns header data for the list.

    The horizontal header is the class-name of the elementType.

    Vertical header is the section numbering.

    • section Section to query
    • orientation Horizontal or vertical header.
    • role Role to query.

    Returns the header data as a QVariant.

    [override virtual] bool GenericListModel::insertRows(int row, int count, const QModelIndex &parent = QModelIndex())

    Reimplements: QAbstractItemModel::insertRows(int row, int count, const QModelIndex &parent).

    Default constructs count new objects and appends them to this list.

    For this function work, the type as defined by elementType must have the following constructor:

    Q_INVOKABLE MyClass(QObject* parent = nullptr);

    Otherwise behaviour is undefined.

    • row Row to start insertion at.
    • count Number of new objects to create and insert.
    • parent Not used for lists.

    Returns true If creation was successful.

    [override virtual] bool GenericListModel::removeRows(int row, int count, const QModelIndex &parent = QModelIndex())

    Reimplements: QAbstractItemModel::removeRows(int row, int count, const QModelIndex &parent).

    Removes and deletes the range of objects defined by row and count.

    • row Start index for deletion.
    • count Number of objects to delete.
    • parent Not used for lists.

    Returns true if deletion was successful.

    [override virtual] QHash<int, QByteArray> GenericListModel::roleNames() const

    Reimplements: QAbstractItemModel::roleNames() const.

    A collection of role names and the corresponding user role enum.

    This will always return the hard-coded (name, role) combination (modelData, Qt::UserRole).

    For each subsequent property it will also expose: (property_N, Qt::UserRole + N + 1) where property_N is the Nth property in the QMetaObject.

    I.E. for the Foo example given in the class documentation we would return:

    NameValue
    modelDataQt::UserRole
    propertyAQt::UserRole + 1
    propertyBQt::UserRole + 2QString
    propertyCQt::UserRole + 3

    See also elementType.

    [override virtual] int GenericListModel::rowCount(const QModelIndex &parent = QModelIndex()) const

    Reimplements: QAbstractItemModel::rowCount(const QModelIndex &parent) const.

    • parent Unused for lists.

    Returns the number of objects in the list if parent is an invalid QModelIndex, otherwise returns 0.

    [override virtual] bool GenericListModel::setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole)

    Reimplements: QAbstractItemModel::setData(const QModelIndex &index, const QVariant &value, int role).

    For a given index in the model, attempts to set the data in the corresponding property to value and returns whether the write succeeded or not.

    If role is Qt::DisplayRole or Qt::EditRole, then the property written to is the property with the name defined by the displayPropertyName field.

    If the role is Qt::UserRole, then we attempt to cast the QVariant to the type defined by elementType, and replace the object in the model with this new object.

    If the role is is any value greater than Qt::UserRole, then the role is mapped to the property in the object associated with that role. See roleNames to get a list of those properties.

    • index QObject in list to write to.
    • role The role to map to a property in the QObject.
    • value Value written to the property in the QObject.

    Returns true if the QMetaProperty::write call succeeded.

    See also data().

    void GenericListModel::setDisplayPropertyName(const QString &propertyName)

    The name of the property which is to be exposed as both the display role and the edit role.

    When data or setData is called with Qt::DisplayRole or Qt::EditRole as the given role, then the underlying property as defined by this setter will be read from or set to.

    This property is reset if setElementType is called.

    • propertyName name of property to expose as DisplayRole and EditRole.

    See also displayPropertyName().

    void GenericListModel::setElementType(const QMetaObject *metaObject)

    Set the QMetaObject that defines all the roles this model will expose.

    This function will reset the model and delete the current model contents if changed.

    See also elementType().

    template <typename Func> void GenericListModel::setFlagsCallback(Func &&f)

    Template member function used to set the callback function which calculates each item Qt::ItemFlags.

    QString GenericListModel::tooltipPropertyName()

    Returns the name of the property which has been elevated to be used as the Qt::ToolTipRole in this model.

    Returns name of property.

    Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.