Qt:迭代器和OBject:复制构造函数错误

时间:2013-06-13 09:57:30

标签: qt iterator copy-constructor

在名为UserForms的类中,我定义了一个列表(QList)或SqlQueryModel对象(QSqlQueryModel的子类),如this Qt tutorial中所示。

在userforms.h中:

QList<SqlQueryModel> userModels;

然后在我的主代码中,我想迭代userModel,所以我尝试了以下两种方法:

UserForm selectedUF = formSelectorDiag.getSelectedUF();
QList<SqlQueryModel>::iterator umIt;

for(umIt = selectedUF.userModels.begin(); umIt != selectedUF.userModels.end(); umIt++){

    SqlQueryModel model = *umIt;
    // Additional code removed since error happens on previous line.
}

QListIterator<SqlQueryModel> umIt(selectedUF.userModels);

使用两者,我得到一个“复制构造函数”错误,但是,我不复制构造函数(或者我不认为我这样做)。 Cf sqlquerymodel.h:

class SqlQueryModel : public QSqlQueryModel
{
    Q_OBJECT

    void generateRoleNames();

public:
    explicit SqlQueryModel(QObject *parent = 0);

    void setQuery(const QString &query, const QSqlDatabase &db = QSqlDatabase());
    void setQuery(const QSqlQuery &query);
    QVariant data(const QModelIndex &index, int role) const;
    QHash<QString, QVariant> bindings; // Not used yet. Will be replaced by Binding class.
    QString modelName;
    QString query;

signals:
    void bindedValueChanged();

public slots:
    void search(QString);
    void reset();
    void updateBindings();

};

以下是完全准确的错误:

In file included from ..\..\..\..\Qt\4.8.4\include\QtSql/qsqlquerymodel.h:1:0,
                 from ..\..\..\..\Qt\4.8.4\include\QtSql/QSqlQueryModel:1,
                 from ..\SSL_Tool\/sqlquerymodel.h:4,
                 from ..\SSL_Tool\main.cpp:12:
..\..\..\..\Qt\4.8.4\include/QtCore/../../src/corelib/kernel/qabstractitemmodel.h: In copy constructor 'QSqlQueryModel::QSqlQueryModel(const QSqlQueryModel&)':
..\..\..\..\Qt\4.8.4\include\QtSql/../../src/sql/models/qsqlquerymodel.h:37:20:   instantiated from 'void QList<T>::node_copy(QList<T>::Node*, QList<T>::Node*, QList<T>::Node*) [with T = SqlQueryModel]'
..\..\..\..\Qt\4.8.4\include/QtCore/../../src/corelib/tools/qlist.h:689:9:   instantiated from 'void QList<T>::detach_helper(int) [with T = SqlQueryModel]'
..\..\..\..\Qt\4.8.4\include/QtCore/../../src/corelib/tools/qlist.h:703:5:   instantiated from 'void QList<T>::detach_helper() [with T = SqlQueryModel]'
..\..\..\..\Qt\4.8.4\include/QtCore/../../src/corelib/tools/qlist.h:100:80:   instantiated from 'QList<T>::QList(const QList<T>&) [with T = SqlQueryModel]'
..\SSL_Tool\/userform.h:17:7:   instantiated from here
..\..\..\..\Qt\4.8.4\include/QtCore/../../src/corelib/kernel/qabstractitemmodel.h:338:5: error: 'QAbstractTableModel::QAbstractTableModel(const QAbstractTableModel&)' is private
..\..\..\..\Qt\4.8.4\include\QtSql/../../src/sql/models/qsqlquerymodel.h:37:20: error: within this context
In file included from ..\SSL_Tool\main.cpp:12:0:
..\SSL_Tool\/sqlquerymodel.h: In copy constructor 'SqlQueryModel::SqlQueryModel(const SqlQueryModel&)':
..\SSL_Tool\/sqlquerymodel.h:14:7: note: synthesized method 'QSqlQueryModel::QSqlQueryModel(const QSqlQueryModel&)' first required here 
In file included from ..\..\..\..\Qt\4.8.4\include/QtCore/qlist.h:1:0,
                 from ..\..\..\..\Qt\4.8.4\include/QtCore/../../src/corelib/kernel/qobject.h:28,
                 from ..\..\..\..\Qt\4.8.4\include/QtCore/qobject.h:1,
                 from ..\..\..\..\Qt\4.8.4\include/QtCore/../../src/corelib/kernel/qcoreapplication.h:23,
                 from ..\..\..\..\Qt\4.8.4\include/QtCore/qcoreapplication.h:1,
                 from ..\..\..\..\Qt\4.8.4\include\QtGui/../../src/gui/kernel/qapplication.h:23,
                 from ..\..\..\..\Qt\4.8.4\include\QtGui/qapplication.h:1,
                 from ..\..\..\..\Qt\4.8.4\include\QtGui/QApplication:1,
                 from ..\SSL_Tool\main.cpp:1:
..\..\..\..\Qt\4.8.4\include/QtCore/../../src/corelib/tools/qlist.h: In member function 'void QList<T>::node_copy(QList<T>::Node*, QList<T>::Node*, QList<T>::Node*) [with T = SqlQueryModel]':
..\..\..\..\Qt\4.8.4\include/QtCore/../../src/corelib/tools/qlist.h:689:9:   instantiated from 'void QList<T>::detach_helper(int) [with T = SqlQueryModel]'
..\..\..\..\Qt\4.8.4\include/QtCore/../../src/corelib/tools/qlist.h:703:5:   instantiated from 'void QList<T>::detach_helper() [with T = SqlQueryModel]'
..\..\..\..\Qt\4.8.4\include/QtCore/../../src/corelib/tools/qlist.h:100:80:   instantiated from 'QList<T>::QList(const QList<T>&) [with T = SqlQueryModel]'
..\SSL_Tool\/userform.h:17:7:   instantiated from here
..\..\..\..\Qt\4.8.4\include/QtCore/../../src/corelib/tools/qlist.h:377:17: note: synthesized method 'SqlQueryModel::SqlQueryModel(const SqlQueryModel&)' first required here 
mingw32-make[1]: Leaving directory `C:/Users/XXX/build-SSL_Tool-Desktop-Debug'
mingw32-make[1]: *** [debug/main.o] Error 1
mingw32-make: *** [debug] Error 2

知道为什么吗?

1 个答案:

答案 0 :(得分:4)

无法使用复制构造函数复制

QSqlQueryModel(以及所有其他QObject派生的对象)。 QList要求其元素类型具有复制构造函数。因此,您需要使用QList<SqlQueryModel*>而不是QList<SqlQueryModel>。您应该使用newdelete手动创建和删除模型对象。