简体   繁体   中英

How to access a QList of QObject* in QML and Qt?

I need to have an array of class objects which should be accessible in Qt as well as QML.

Considering https://stackoverflow.com/a/15830797/462608 following is what I have tried.

Now I need to know how to read and write to the functions of ClassA through Qt and QML .

See the print statement in the constructor in .h file. That produces a segmentation fault.

class.h

#ifndef CLASSA
#define CLASSA

#include <QQuickItem>

class ClassA : public QObject
{
private:
    Q_OBJECT

    Q_PROPERTY(int one READ one WRITE setOne NOTIFY oneChanged)
    int m_one;

public:
    ClassA () {}
    int one() const { return m_one; }

public slots:
    void setOne(int arg)
    {
        if (m_one == arg)
            return;

        m_one = arg;
        emit oneChanged(arg);
    }

signals:
    void oneChanged(int arg);
};

class ClassB : public QObject
{
private:
    Q_OBJECT

    Q_PROPERTY(QList <QObject*> objClassAList READ objClassAList WRITE setObjClassAList NOTIFY objClassAListChanged)
    QList <QObject*> m_objClassAList;

public:
    ClassB ()
    {
        QList <QObject*> localObjList;
        for (int i = 0; i < 5; i++)
        {
            ClassA localObj;
            localObj.setOne (100);
            localObjList.push_back (&localObj);
        }

        setObjClassAList (localObjList);
        qDebug () << "QQQQQQQQQQQQQQ: " << objClassAList ()[0]->property ("one");
    }

    QList <QObject*> objClassAList () const { return m_objClassAList; }

public slots:
    void setObjClassAList (QList <QObject*> arg)
    {
        if (m_objClassAList == arg)
            return;

        m_objClassAList = arg;
        emit objClassAListChanged(arg);
    }

signals:
    void objClassAListChanged (QList <QObject*> arg);
};

#endif // CLASSA

main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "class.h"

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    const char* what = "WHAT";

    qmlRegisterType <ClassA> (what, 1, 0, "A");
    qmlRegisterType <ClassB> (what, 1, 0, "B");

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

main.qml

import QtQuick 2.4
import QtQuick.Window 2.2
import WHAT 1.0

Window {
    visible: true

    B
    {
        onObjClassAListChanged:
        {

        }
    }
}

The problem is you're trying to access a delete pointer that was allocated on the stack. Allocate ClassA on the heap instead, and it should fix your problem.

ClassB ()
{
    QList <QObject*> localObjList;
    for (int i = 0; i < 5; i++)
    {
        ClassA localObj; //This object is delete after it goes out of scope
        localObj.setOne (100);
        localObjList.push_back (&localObj); //After this line &localObj is a dangling pointer to delete memory
    }

    setObjClassAList (localObjList);
    qDebug () << "QQQQQQQQQQQQQQ: " << objClassAList ()[0]->property ("one");
}

For example, I suggest modifying it to:

ClassB ()
{
    QList <QObject*> localObjList;
    for (int i = 0; i < 5; i++)
    {
        ClassA localObj = new ClassA(); 
        localObj->setOne (100);
        localObjList.push_back(localObj);
    }

    setObjClassAList (localObjList);

    //This should work now.
    qDebug () << "QQQQQQQQQQQQQQ: " << objClassAList ()[0]->property ("one");
}

This should solve your segmentation fault. Everything else looks okay.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM