基类'QAbstractListModel'具有私有拷贝构造函数

时间:2014-03-03 22:51:22

标签: qt qml copy-constructor qabstractitemmodel qabstractlistmodel

我有一个QT QML项目。 (还很小)

我开始在我的UScenario模型上绑定一个listview,通过继承QAbstractListModel并对它进行了罚款。

现在,每个UScenario都有一个UTask列表,其中还有UCondition列表(因此,Utask也是子类QAbstractListModel)。但是,QT Creator给了我一个错误:

Core/Tasks/utask.h:6: erreur : base class 'QAbstractListModel' has private copy constructor
class UTask: public QAbstractListModel
      ^

所以我不确定我的问题在哪里。我尝试阅读有关QAbstractListModel vs QAbstractItemModel的文档,但我不知道。

我还试图看看我是否以错误的方式构建了UTask;我想不是。

// USCENARIO.h
#ifndef USCENARIO_H
#define USCENARIO_H

#include <QAbstractListModel>
#include "../Tasks/utask.h"

class UScenario : public QAbstractListModel
{
    Q_OBJECT

public slots:
     void cppSlot() { // Used to test the insertion from UI
         this->addTask(UTask());
     }

public:
    enum TaskRoles {
        IdRole = Qt::UserRole + 1
    };

    UScenario(QObject *parent = 0);

private:
    QList<UTask> m_tasks;

public:
    void addTask(const UTask &task);
    virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
    virtual QVariant data(const QModelIndex &index, int role) const;
    virtual QHash<int, QByteArray> roleNames() const;
};

#endif // USCENARIO_H



// USCENARIO.CPP

#include "uscenario.h"

UScenario::UScenario(QObject *parent)
    : QAbstractListModel(parent)
{
}

void UScenario::addTask(const UTask &task)
{
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_tasks.append(task);
    endInsertRows();
}

int UScenario::rowCount(const QModelIndex & parent) const {
    return m_tasks.count();
}

QVariant UScenario::data(const QModelIndex & index, int role) const {
    if (index.row() < 0 || index.row() >= m_tasks.count())
        return QVariant();

    const UTask &task = m_tasks[index.row()];
    if (role == IdRole)
        return task.id();

    return QVariant();
}

QHash<int, QByteArray> UScenario::roleNames() const {
    QHash<int, QByteArray> roles;
    roles[IdRole] = "id";
    return roles;
}






// UTASK.H
#ifndef UTASK_H
#define UTASK_H
#include <QAbstractListModel>
#include "../Conditions/ucondition.h"

class UTask: public QAbstractListModel
{
    Q_OBJECT

public:
    enum TaskRoles {
        typeRole = Qt::UserRole + 1
    };

    UTask(QObject *parent = 0);//:m_id(0){}
     int id() const{return m_id;}

private:
    int m_id;
    QList<UCondition> m_conditions;

    // QAbstractItemModel interface
public:
    void addCondition(const UCondition &cond);
    virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
    virtual QVariant data(const QModelIndex &index, int role) const;
    virtual QHash<int, QByteArray> roleNames() const;
};


#endif // UTASK_H







// UTASK.cpp
#include "utask.h"



UTask::UTask(QObject *parent):
    QAbstractListModel(parent), m_id(0)
{

}

void UTask::addCondition(const UCondition &cond)
{
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_conditions.append(cond);
    endInsertRows();
}

int UTask::rowCount(const QModelIndex &parent) const
{
    return m_conditions.count();

}

QVariant UTask::data(const QModelIndex &index, int role) const
{
    if (index.row() < 0 || index.row() >= m_conditions.count())
        return QVariant();

    const UCondition &cond = m_conditions[index.row()];
    if (role == typeRole)
        return cond.type();

    return QVariant();
}

QHash<int, QByteArray> UTask::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[typeRole] = "type";
    return roles;
}


// MAIN
#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include <qqmlengine.h>
#include <qqmlcontext.h>
#include <qqml.h>
#include <QtQuick/qquickitem.h>
#include <QtQuick/qquickview.h>
#include "../uCtrlCore/Scenario/uscenario.h"

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

    UScenario scenarioModel;
    scenarioModel.addTask(UTask());
    scenarioModel.addTask(UTask());
    scenarioModel.addTask(UTask());

    QtQuick2ApplicationViewer viewer;
    QQmlContext *ctxt = viewer.rootContext();
    ctxt->setContextProperty("myScenarioModel", &scenarioModel);
    viewer.setMainQmlFile(QStringLiteral("qml/uCtrlDesktopQml/main.qml"));

    QObject *item = viewer.rootObject()->findChild<QObject*>("btn");
    QObject::connect(item, SIGNAL(qmlSignal()), &scenarioModel, SLOT(cppSlot()));

    viewer.showExpanded();

    return app.exec();
}

1 个答案:

答案 0 :(得分:3)

问题在于如何在UTask类中存储UScenario个对象

QList<UTask> m_tasks

简单来说,当您调用m_tasks.append时,它会尝试通过默认的复制构造函数复制源UTask对象,在QList中分配新的UTask对象。在QAbstractListModel的情况下,它是私有的。这就是你收到错误的原因。

一个简单的解决方案是将存储类型更改为UTask指针列表QList< UTask* >以及支持代码,以便在销毁UScenario对象时正确释放内存。

例如,这里有一些但不是所有的变化,但应该指向正确的方向。只需确保先将m_tasks更改为QList< UTask* >

int main(int argc, char *argv[])
{
    ...

    UScenario scenarioModel;
    scenarioModel.addTask( new UTask() );
    scenarioModel.addTask( new UTask() );
    scenarioModel.addTask( new UTask() );

    ...    

    return app.exec();
}

void UScenario::cppSlot() 
{ 
     // Used to test the insertion from UI
     this->addTask( new UTask() );
}

// Change the signature to take a pointer
void UScenario::addTask( UTask* task )
{
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_tasks.append(task);
    endInsertRows();
}

// Make sure you define a destructor for UScenario
UScenario::~UScenario()
{
    QList< UTask* >::iterator task = m_tasks.begin();

    while( m_tasks.end() != task )
    {
        // Release the memory associated with the task.
        delete (*task);
        ++task;
    }

    m_tasks.clear();
}