5

我有一個QT QML項目。 (仍然很小)基類'QAbstractListModel'具有私有拷貝構造函數

我開始通過綁定我的UScenario模型上的列表視圖,通過子類化QAbstractListModel,它的工作被罰款。現在

,每個UScenarioUTask列表,其中也有UCondition列表(所以,Utask也子類QAbstractListModel)。但是,然後,QT造物主給了我一個錯誤:

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

所以我不知道我的問題在哪裏。我試着閱讀關於QAbstractListModelQAbstractItemModel的文檔,但我不知道。

我也嘗試看看我是否曾經以錯誤的方式構建了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(); 
} 

回答

3

有問題是如何你在你的UScenario

QList<UTask> m_tasks 

簡單來說,存儲UTask對象,當你調用m_tasks.append它正試圖通過分配在QListUTask對象通過默認的拷貝構造函數複製源對象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(); 
} 
+0

它的工作,謝謝!我怎麼會錯過那個提示...... – lcoderre

相關問題