Unsolved QAbstractListModel get method
-
Hello,
I implemented templated QAbstractListModel with get method which I want to be accessible from QML:template <class T> class ListModel : public QAbstractListModel { // ... public: Q_INVOKABLE T *get(int index) { return m_objects[index]; } // ... private: QList<T *> m_objects; // ... }
I instantiate a list in C++, register it, expose it and use it in QML:
Q_PROPERTY(ListModel<MyData> *myDataList READ myDataList CONSTANT) ListModel<MyData> *myDataList (void) { return &m_dataList; } ListModel<MyData> m_dataList; Q_DECLARE_METATYPE(ListModel<MyData>*) qRegisterMetaType<ListModel<MyData>*>(); qmlRegisterType<ListModel<MyData>>("xyz", 1, 0, "ListModelMyData");
However, when I call in QML:
myDataList .get(0)
I get:
qrc:/MyComponent.qml:1: TypeError: Property 'get' of object QAbstractListModel(0xc26748) is not a function
Can you please help?
-
@MartinD said in QAbstractListModel get method:
template <class T> class ListModel : public QAbstractListModel
{
// ...
public:did you maybe miss the
Q_OBJECT
macro? -
@raven-worx said in QAbstractListModel get method:
Q_OBJECT
Yes, I missed that. But it seems there is another problem - template classes don't support Q_OBJECT. I think some workaround will be needed...
-
@MartinD
Hi
I dont think template classes and the moc tool can ever work.
There was talk about it on mailing lists
http://lists.qt-project.org/pipermail/interest/2016-February/021029.html
(Thanks to mr @kshegunov )
But it seems that the code changes to allow it was never integrated/moved along.