returning a custom QObject subclass from QAbstractListModel and using it in a ListView
<p>How can I return a custom QObject sub-class from QAbstractListModel and use it in a QML ListView.</p>

<p>I tried to return the objects as display role and I use in my qml display.property to access properties, it works fine but I saw on some posts people using model as the qobject from qml and accessing properties as model.property. Am I missing something?.</p>

<p>Another question: If I want to expose the object at the ListView level and using it to set some other panel like a master-view detail is exposing the role (in my case display) as a variant property in the delegate and setting it at the listview level with the onCurrentItemChanged signal is the correct way to do it ??</p>

<p>this is what I am trying but it does not work:</p>

<pre><code>#ifndef NOTE_H
#define NOTE_H

#include &lt;QObject&gt;

class Note : public QObject
{
    Q_OBJECT

    Q_PROPERTY(QString note READ note WRITE setNote NOTIFY noteChanged)
    Q_PROPERTY(int id READ id WRITE setId NOTIFY idChanged)

    QString m_note;
    int m_id;

public:
    explicit Note(QObject *parent = 0);
    Note(QString note, int id, QObject *parent = 0);

    QString note() const
    {
        return m_note;
    }

    int id() const
    {
        return m_id;
    }

signals:

    void noteChanged(QString note);

    void idChanged(int id);

public slots:
    void setNote(QString note)
    {
        if (m_note == note)
            return;

        m_note = note;
        emit noteChanged(note);
    }
    void setId(int id)
    {
        if (m_id == id)
            return;

        m_id = id;
        emit idChanged(id);
    }
};

#endif // NOTE_H
</code></pre>

<p>the view model:</p>

<pre><code>#ifndef NOTESVIEWMODEL_H
#define NOTESVIEWMODEL_H

#include &lt;QAbstractListModel&gt;
#include &lt;QVector&gt;
#include "note.h"

class NotesViewModel : public QAbstractListModel
{
    Q_OBJECT

    QVector&lt;Note*&gt; notes;


public:
    NotesViewModel();

    QVariant data(const QModelIndex &amp;index, int role) const override;
    int rowCount(const QModelIndex &amp;parent) const override;

};

#endif // NOTESVIEWMODEL_H
</code></pre>

<p>implementation of the view model:</p>

<pre><code>NotesViewModel::NotesViewModel()
{
    notes.append(new Note("note 1", 1));
    notes.append(new Note("note 2", 2));
    notes.append(new Note("note 3", 3));
    notes.append(new Note("note 4", 4));
    notes.append(new Note("note 5", 5));
}

QVariant NotesViewModel::data(const QModelIndex &amp;index, int role) const
{
    qDebug() &lt;&lt; "fetching data : " &lt;&lt; index.row();
    if(!index.isValid()) return QVariant();
    if(index.row() &gt;= 5) return QVariant();
    if(role == Qt::DisplayRole)
        return QVariant::fromValue(notes[index.row()]);
    return QVariant();
}

int NotesViewModel::rowCount(const QModelIndex &amp;parent) const
{
    Q_UNUSED(parent)
    return notes.count();
}
</code></pre>