Question

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

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top