базовый класс QAbstractListModel имеет частный конструктор копирования

У меня есть проект QT QML. (еще очень маленький)

Я начал с привязки списка к моей модели UScenario, создав подкласс 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 против 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();
}

person lcoderre    schedule 03.03.2014    source источник


Ответы (1)


Проблема в том, как вы храните объекты 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();
}
person Matthew    schedule 04.03.2014