Unsolved Pass Vector of Pointers to QML file
-
My goal is to pass a vector of pointers to a QML file, where I will be able to get the data(String) from it.
Q_PROPERTY(QVector<Thing*> getThing READ getThing)
and there's also a set and get functionsmain.cpp
engine.rootContext()->setContextProperty("ThingOBJ",&ThinOBJ); -
Use QVariantList instead of QVector<Thing*>
-
But how a QVariantList will store pointers, not the same as other containers?
-
if Thing is a QObject, everything is supported natively
-
@VRonin I just thought about, what if i will create a class for the QVector<Thing*> and provide the functionality like getThat, getData etc. and send an instance of an object to the QML, will that work?
-
@mandruk1331 As long as that class is a QObject or Q_GADGET (declared and registered as MetaType) and you declare Q_IVOKABLE, slots and/or Q_PROPERTY it will
-
@VRonin I created a class and inherited a QObject but how I can pass the data inside of the QVector<QObject*>? can you give a little example?
-
class Thing{ public: Thing(const QString& val = QString() ) : m_foo(val){} Thing(const Thing& other)=default; Thing& operator=(const Thing& other)=default; ~Thing() =default; void setFoo(const QString& val){m_foo=val;} const QString& foo() const {return m_foo;} private: QString m_foo; };
class ThingsWrapper : public QObject{ Q_OBJECT Q_PROPERTY(int countThings READ countThings) Q_DISABLE_COPY(ThingsWrapper) public: explicit ThingsWrapper(QObject* parent=Q_NULLPTR) :QObject(parent){} virtual ~ThingsWrapper(){ clearThings(); } int countThings() const { return m_things.size();} Q_INVOKABLE QString fooAt(int index){ if(index<0 || index>=m_things.size()) return QString(); return m_things.at(index)->foo(); } public slots: void addThing(const QString& foo){ m_things.append(new Thing(foo)); } void removeThing(int index){ if(index<0 || index>=m_things.size()) return; delete m_things.takeAt(index); } void clearThings(){ while(!m_things.isEmpty()) delete m_things.takeLast(); } private: QVector<Thing*> m_things; };
-
@VRonin at the moment I have a class which inherits from QObject, and inside of that class I have a struct with all the data (QString), and also I have created QVector<StructType*> , the problem is how I can access the data inside of the QVector<StructType*>obj, from QML. Because the inside of C++ I have to use obj.at(0)->StringData, and inside the QML file I can't use an arrow, how am I able to get the data from the Vector inside of the QML?
-
do something like I did here:
Q_INVOKABLE QString fooAt(int index){ if(index<0 || index>=m_things.size()) return QString(); return m_things.at(index)->foo(); }
-
@VRonin the index is getting set only in the function which changes the index number, but when I want to use it in a function like get ThinngData(){return thing.at(index)->Data} the index is the default value(the one which is on the start of the program), is there a way to tell the compiler not to do that?
And I forgot to say that I want to make a list view of the data, in that case I need to create a model? -
@mandruk1331 said in Pass Vector of Pointers to QML file:
is the default value(the one which is on the start of the program)
I did not get this, I'm sorry
@mandruk1331 said in Pass Vector of Pointers to QML file:
I want to make a list view of the data, in that case I need to create a model?
Exactly
-
This post is deleted! -
@VRonin So far I used your approach and create get functions which return the data i need, and I call them inside qml and just pass the index which I need, is that a correct approach?