0

I've tried with QVector<AClass*> instead of QVector<AClass*>* and it works with the following in qml:

ListView{
    model: testContext.list
    delegate: Text{
        text: modelData.name + " " + modelData.age
    }
}

When I added one more * to make it QVector<AClass*>* I got this error:

QMetaProperty::read: Unable to handle unregistered datatype 'QVector*' for property 'Test::list'

to fix it according to solution provided here, I've added qRegisterMetaType<QVector<AClass*>*>("QVector<AClass*>*"); in constructor like this:

Test::Test(QObject *parent) : QObject(parent)
{
    qRegisterMetaType<QVector<AClass*>*>("QVector<AClass*>*");
    engine.rootContext()->setContextProperty("testContext", this);
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
}

above constructor is in .cpp file of the following class:

class Test : public QObject
{  
    QQmlApplicationEngine engine;
    QVector<AClass*> *vector;

    Q_OBJECT
    Q_PROPERTY(QVector<AClass*>* list READ list WRITE setList NOTIFY listChanged)

public:
    explicit Test(QObject *parent = nullptr);
    QVector<AClass*>* list() {return vector;}
    void setList(QVector<AClass*>* value){vector = value; emit listChanged();}
    Q_SIGNAL void listChanged();
    Q_INVOKABLE void addItem();
    Q_INVOKABLE void removeItem(int index);
};

in my main.cpp I've these:

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);
    Test test;
    return app.exec();
}

QVector has items in it BUT nothing shows up in ListView! Here is AClass:

class AClass : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
    Q_PROPERTY(int age READ age WRITE setAge NOTIFY ageChanged)

public:
    explicit AClass(QObject *parent = nullptr);
    QString name() const {return mName;}
    void setName(QString value) {mName = value; emit nameChanged();}
    int age() const {return mAge;}
    void setAge(int value){mAge = value; emit ageChanged();}
    Q_SIGNAL void nameChanged();
    Q_SIGNAL void ageChanged();

private:
    QString mName;
    int mAge;
};

If I use QVector<AClass*> or QVector<AClass> instead of QVector<AClass*>* I see items in ListView.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 1
    The big question is: Why do you want to use `QVector*`? – eyllanesc Jan 19 '20 at 18:16
  • @eyllanesc, why not? It just came to my mind while reading a document and testing. –  Jan 19 '20 at 18:26
  • 2
    QML can only recognize some basic data types such as QObjects *, int, floats, QUrl, QString, etc. and the Qt containers(QVector, QList, etc) of those basic types **but cannot recognize a pointer of a container**. – eyllanesc Jan 19 '20 at 18:30

0 Answers0