ChrisR
ChrisR

Reputation: 1096

Qt: Iterators and OBject: copy constructor error

In class called UserForms, I define a list (QList) or SqlQueryModel objects (subclass of QSqlQueryModel), as in this Qt tutorial.

In userforms.h:

QList<SqlQueryModel> userModels;

Then in my main code, I want to iterate over userModel, so I tried both of the following:

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.
}

And:

QListIterator<SqlQueryModel> umIt(selectedUF.userModels);

With both, I get a "copy constructor" error, although, I don't copy the constructor (or I don't think I do). 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();

};

Here's the full exact error:

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

Any idea why?

Upvotes: 0

Views: 427

Answers (1)

Pavel Strakhov
Pavel Strakhov

Reputation: 40522

QSqlQueryModel (and all other QObject-derived objects) can't be copied using copy constructor. QList requires its element type to have copy constructor. So, you need to use QList<SqlQueryModel*> instead of QList<SqlQueryModel>. You should create and delete your model objects manually using new and delete.

Upvotes: 4

Related Questions