0

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

在 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);

两者都有,我得到一个“复制构造函数”错误,虽然,我不复制构造函数(或者我不认为我这样做)。参见 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

知道为什么吗?

4

1 回答 1

4

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

于 2013-06-13T10:03:22.067 回答