Qt 无法将QAbstractListModel对象作为一个类';s属性

Qt 无法将QAbstractListModel对象作为一个类';s属性,qt,qobject,qabstractlistmodel,Qt,Qobject,Qabstractlistmodel,我的代码如下所示: class Wrapper: public QObject { Q_OBJECT Q_PROPERTY(PeopleListModel & list READ list) public: PeopleListModel & list() { return list_; } private: PeopleListModel list_; }; PeopleListModel是QAbstractListModel的一个子类

我的代码如下所示:

class Wrapper: public QObject {
  Q_OBJECT
  Q_PROPERTY(PeopleListModel & list READ list)
public:
  PeopleListModel & list() {
    return list_;
  }  

private:
  PeopleListModel list_;  
};
PeopleListModel是QAbstractListModel的一个子类

但我得到了编译错误:

       ^
In file included from plugin.cpp:131:0:
plugin.moc: In member function ‘virtual int Wrapper::qt_metacall(QMetaObject::Call, int, void**)’:
plugin.moc:184:52: error: cannot declare pointer to ‘class PeopleListModel&’
         case 0: *reinterpret_cast< PeopleListModel&*>(_v) = list(); break;
                                                    ^
In file included from ../../../../Qt5.2.0/5.2.0/gcc_64/include/QtCore/qnamespace.h:45:0,
                 from ../../../../Qt5.2.0/5.2.0/gcc_64/include/QtCore/qobjectdefs.h:45,
                 from ../../../../Qt5.2.0/5.2.0/gcc_64/include/QtCore/qobject.h:48,
                 from ../../../../Qt5.2.0/5.2.0/gcc_64/include/QtCore/qplugin.h:45,
                 from ../../../../Qt5.2.0/5.2.0/gcc_64/include/QtQml/qqmlextensionplugin.h:45,
                 from ../../../../Qt5.2.0/5.2.0/gcc_64/include/QtQml/QQmlExtensionPlugin:1,
                 from plugin.cpp:1:
../../../../Qt5.2.0/5.2.0/gcc_64/include/QtCore/qabstractitemmodel.h: In member function ‘PeopleListModel& PeopleListModel::operator=(const PeopleListModel&)’:
../../../../Qt5.2.0/5.2.0/gcc_64/include/QtCore/qglobal.h:967:12: error: ‘QAbstractListModel& QAbstractListModel::operator=(const QAbstractListModel&)’ is private
     Class &operator=(const Class &) Q_DECL_EQ_DELETE;
            ^
../../../../Qt5.2.0/5.2.0/gcc_64/include/QtCore/qabstractitemmodel.h:479:5: note: in expansion of macro ‘Q_DISABLE_COPY’
     Q_DISABLE_COPY(QAbstractListModel)
     ^
plugin.cpp:32:7: error: within this context
 class PeopleListModel : public QAbstractListModel {
       ^
In file included from plugin.cpp:131:0:
plugin.moc: In member function ‘virtual int Wrapper::qt_metacall(QMetaObject::Call, int, void**)’:
plugin.moc:184:59: note: synthesized method ‘PeopleListModel& PeopleListModel::operator=(const PeopleListModel&)’ first required here 
         case 0: *reinterpret_cast< PeopleListModel&*>(_v) = list(); break;
                                                           ^
make: *** [plugin.o] Error 1
^
在plugin.cpp:131:0中包含的文件中:
plugin.moc:在成员函数“virtual int Wrapper::qt_metacall(QMetaObject::Call,int,void**)”中:
plugin.moc:184:52:错误:无法声明指向“类PeopleListModel&”的指针
案例0:*重新解释cast(\u v)=list();打破
^
在../../../../../Qt5.2.0/5.2.0/gcc_64/include/QtCore/qnamespace.h:45:0中包含的文件中,
从../../../../../Qt5.2.0/5.2.0/gcc_64/include/QtCore/qobjectdefs.h:45,
从../../../../../Qt5.2.0/5.2.0/gcc_64/include/QtCore/qobject.h:48,
从../../../../../Qt5.2.0/5.2.0/gcc_64/include/QtCore/qplugin.h:45,
来自../../../../../Qt5.2.0/5.2.0/gcc_64/include/QtQml/qqmlextensionplugin.h:45,
来自../../../../../Qt5.2.0/5.2.0/gcc_64/include/QtQml/QQmlExtensionPlugin:1,
来自plugin.cpp:1:
../../../../Qt5.2.0/5.2.0/gcc_64/include/QtCore/qabstractitemmodel.h:在成员函数“PeopleListModel&PeopleListModel::operator=(const PeopleListModel&)”中:
../../../Qt5.2.0/5.2.0/gcc_64/include/QtCore/qglobal.h:967:12:错误:“QAbstractListModel和QAbstractListModel::operator=(const QAbstractListModel&)”是私有的
类和运算符=(常量类和)Q_DECL_EQ_DELETE;
^
../../../../Qt5.2.0/5.2.0/gcc_64/include/QtCore/qabstractemmodel.h:479:5:注意:在宏“Q_DISABLE_COPY”的展开中
Q_禁用_复制(QAbstractListModel)
^
plugin.cpp:32:7:错误:在此上下文中
类PeopleListModel:PublicQAbstractListModel{
^
在plugin.cpp:131:0中包含的文件中:
plugin.moc:在成员函数“virtual int Wrapper::qt_metacall(QMetaObject::Call,int,void**)”中:
plugin.moc:184:59:注意:这里首先需要合成方法'PeopleListModel&PeopleListModel::operator=(const-PeopleListModel&')
案例0:*重新解释演播(\u v)=list();中断;
^
make:**[plugin.o]错误1

尝试将属性声明为对象,而不是引用。引用应立即初始化,可能moc某处不会初始化。属性也应为可复制类。而
Q\u禁用复制(QAbstractListModel)
宏(
QAbstractListModel
-您的
PeopleListModel
的基类)不允许这样做。我首先使用属性而不是引用,但仍然得到上面的错误。我只想将一个类的属性公开给QML,这个属性(这里是一个listmodel)可以向qml上的my listview提供数据。复制遗留问题仍然存在。Moc尝试为您的属性赋值,并使用运算符=。这是
Q\u DISABLE\u COPY
宏的私人原因。它将与指针(
PeopleListModel*list;
)一起工作。