How can you implement the data function of QAbstractListModel such that it returns something with properties that are visible from a QML ListView's delegate?
For the item type, I tried implementing a QObject subclass that has Q_PROPERTYs, but QAbstractListModel::data returns QVariant, and a QObject* can't be converted to QVariant?
How do I create a QVariant that has named properties visible to QML?
class MyListItem : public QObject { Q_OBJECT Q_PROPERTY(type name READ name WRITE set_name NOTIFY nameChanged) /*...*/ public: MyListItem(QObject* parent, const QString& name) : QObject(parent) { set_name(name); } }; class MyList : public QAbstractListModel { public: MyList(QObject* parent); Q_INVOKABLE int rowCount(const QModelIndex &parent = QModelIndex()) const override; Q_INVOKABLE QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; QVector<MyListItem*> items; }; Q_INVOKABLE int MyList::rowCount(const QModelIndex &) const { return items.size(); } Q_INVOKABLE QVariant MyList::data(const QModelIndex &index, int) const { MyListItem* item = items[index.row()]; return item; // <--- ERROR } Getting:
In member function ‘virtual QVariant MyList::data(const QModelIndex&, int) const’: error: use of deleted function ‘QVariant::QVariant(void*)’ 18 | return item; | ^~~~

QObject*toQVariant