1

I have one class called Load which is loading data from database. Another class is to show the data in a table. In the function I am returning QSqlQueryModel which is: At the moment is just basic because I was not able to compile it:

QSqlQueryModel PersistenceAdapter::loadServerList(){

    login();
    cout<<"Loading data"<<endl;

    QSqlQueryModel  model = new QSqlQueryModel();

    logout();
    return model;
}

definition in header file as:

QSqlQueryModel loadServerList();

In the other class I receive it as:

setServersList(PersistenceAdapter.loadServerList());

definition of this one is:

void MainWindow::setServersList(QSqlQueryModel serverdata) {

    //this->servers = serverdata;
    //this->amodel->addData(serverdata);
}

The error is:

PersistenceAdapter.cpp:66:48: error: conversion from ‘QSqlQueryModel*’ to non-scalar type ‘QSqlQueryModel’ requested /usr/include/qt4/QtCore/qabstractitemmodel.h: In copy constructor ‘QSqlQueryModel::QSqlQueryModel(const QSqlQueryModel&)’: /usr/include/qt4/QtCore/qabstractitemmodel.h:360:5: error: ‘QAbstractTableModel::QAbstractTableModel(const QAbstractTableModel&)’ is private /usr/include/qt4/QtSql/qsqlquerymodel.h:59:20: error: within this context PersistenceAdapter.cpp: In member function ‘QSqlQueryModel PersistenceAdapter::loadServerList()’: PersistenceAdapter.cpp:70:12: note: synthesised method ‘QSqlQueryModel::QSqlQueryModel(const QSqlQueryModel&)’ first required here

Appreciate if anyone can help me with that...

3
  • 1
    First of all, if you're allocating the QSqlQueryModel dynamically, you are receiving a pointer to it aren't you? So QSqlQueryModel* model = new QSqlQueryModel();. Then you should return a reference or a pointer to the model and not try to copy it on the return which seems to be prohibited. Commented May 5, 2014 at 8:19
  • 1
    Please don't edit your question with a new problem when your original problem is fixed. It renders the posted answer(s) useless. Create a new question instead, but only after you've given the new problem a attempt on your own. Commented May 5, 2014 at 9:11
  • Well I just asked another question... Commented May 5, 2014 at 9:28

1 Answer 1

2

/usr/include/qt4/QtCore/qabstractitemmodel.h:360:5: error: ‘QAbstractTableModel::QAbstractTableModel(const QAbstractTableModel&)’ is private

That error message means that you are trying to copy a QObject which does not quite have the "value" semantics, but more like "identity". QObjects are inherently not copyable. The reason is that what would you do with the parent/child hierarchy in such cases?

This gives you some hint that you are trying to misuse your QObject subclass instance, i.e. declaring it as a stack object rather than heap.

This is the problematic place in your code:

QSqlQueryModel  model = new QSqlQueryModel();

This seem to be missing the start as follows:

QSqlQueryModel *model = new QSqlQueryModel();
               ^

It seems that you are trying to allocate the object on the heap as opposed to the stack, and that is good, so it is just a typo, probably.

One additional note is that please make sure that it will not leak memory, i.e. either set a parent (directly or indirectly) for it, or use smart pointers. I would suggest the former. That will guarantee that your pointer is automatically deleted when the parent gets deleted.

Sign up to request clarification or add additional context in comments.

3 Comments

I changed it to allocate heap but now I get segmentation fault!
@Bernard: in setServersList?
@Bernard: OK, well, you have not shown the code yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.