PDA

View Full Version : QSqlTableModel Unable to find table with SQlite



qlands
5th July 2011, 14:01
Hi,

I am getting "Unable to find table" when I execute QSqlTableModel.select(). Strange enough I don't get an error when querying the same table of the QSqlTableModel. Here is the code:



//Test query
QSqlQuery *mquery = new QSqlQuery(db);
if (!mquery->exec("SELECT * FROM country"))
{
qDebug() << "Query:" << mquery->lastError().driverText();
}
while (mquery->isValid())
{

mquery->next();
}

ui->setupUi(this);
m_mainmodel = new maintModel(this,db);
m_mainmodel->setTable("country");
m_mainmodel->setEditStrategy(QSqlTableModel::OnManualSubmit);
if (m_mainmodel->select())
{
m_mainmodel->loadStatus();
m_mainmodel->setHeaderData(0, Qt::Horizontal, tr("Name"));
//m_mainmodel->setDisplayColumn("CNTY_NAM",tr("Countries"));
ui->ListView1->setModel(m_mainmodel);
}
else
{
// It cannot find the table!!!!!
qDebug() << m_mainmodel->lastError().driverText();
}


In the code the test query works but not the model select.

The constructor of maintModel is:



maintModel::maintModel(QObject *parent, QSqlDatabase)
:QSqlTableModel(parent)
{

}


Any idea why?

Thanks,
Carlos.

mcosta
5th July 2011, 14:10
Try with



maintModel::maintModel(QObject *parent, QSqlDatabase db)
:QSqlTableModel(parent, db)
{

}


otherwise the model doesn't use your database connection

qlands
5th July 2011, 15:33
Yep! That was the little devil!!!

Thanks.