如何在QML和Qt中访问QObject *的QList?

时间:2015-01-28 09:27:15

标签: c++ qt qml qt5 qtquick2

我需要一个类对象数组,这些对象应该可以在Qt和QML中访问。

考虑https://stackoverflow.com/a/15830797/462608以下是我的尝试。

现在我需要知道如何阅读和写入ClassAQtQML的功能。

请参阅.h文件中构造函数中的print语句。这会产生分段错误。

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:
        {

        }
    }
}

1 个答案:

答案 0 :(得分:1)

问题是您正在尝试访问在堆栈上分配的删除指针。而是在堆上分配ClassA,它应该解决您的问题。

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");
}

例如,我建议将其修改为:

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");
}

这应该可以解决您的分段错误。其他一切看起来还不错。