QList<QObject*> as member that can be used as a list
-
Okay, I have been playing around with using QList<QObject*> as a member of an object that acts like a list model in QML. This works and I can get to the custom properties defined in my SubObject that derives from QObject.
Q_PROPERTY(QList<QObject*> subList READ subList NOTIFY subListChanged)
However, when I try to use an object that inherits QObject I cannot use that object as a list:
Q_PROPERTY(QList<SubObject*> subList READ subList NOTIFY subListChanged)
I tried registering this SubObject class using:
Q_DECLARE_METATYPE qmlRegisterType<SubObject>(); qRegisterMetaType<QList<SubObject*>>();
but I cannot get QML to recoqnize a new list type to use in a ListView as a model. The methods and properties seem to still work that define on the custom SubObject.
Is QList<QObject*> just special?
-
hi,
@fcarney said in QList<QObject*> as member that can be used as a list:However, when I try to use an object that inherits QObject I cannot use that object as a list:
you can pass your SubObjects to QML as QVariantList and use it as list model
Q_PROPERTY(QVariantList selectedPrograms READ selectedPrograms NOTIFY selectedProgramsChanged) QVariantList selectedPrograms(){ QVariantList progList; for(auto prg : m_selectedPrograms){ progList.append(QVariant::fromValue(prg)); } return progList; } private : QList<Job*> m_selectedPrograms;
-
@LeLev said in QList<QObject*> as member that can be used as a list:
you can pass your SubObjects to QML as QVariantList and use it as list model
Holy crap that was a lot easier than what I was trying. I looked all over for this and never found something this simple. I have been looking for a light weight list type for QML that was not based on the abstract list models. This is perfect!
Thanks.
-
@LeLev said in QList<QObject*> as member that can be used as a list:
you can pass your SubObject to QML as QVariantList and use it as list model
How is that better than a QObjectList?
-
@GrecKo said in QList<QObject*> as member that can be used as a list:
How is that better than a QObjectList?
I think QObjectList is just a QList<QObject*> alias from what I read. I think internally the QVariant is probably just seeing my SubObject* as QObject*. But you can add non-qobject based types with Q_DECLARE_METATYPE. So QVariant version would be able to handle that.
-
Yes
QObjectList
is just aQList<QObject*>
, but in your case, QVariantList is just adding another level of indirection.If you don't mind using a 3rd party library and a bit more intrusive solution, you can use :
QQmlObjectListModel
from http://gitlab.unique-conception.org/qt-qml-tricks/qt-qml-models .It behaves as a
QList<T*>
on the c++ side but also expose aQAIM
(QAbstractItemModel
) interface emitting the correct signals so you can plug a QML view onto it.As for your original question, yes
QList<QObject*>
is specially handled by QML views.