Access data from QSqlTableModel
Hi,
I am very raw with model/view hence the many questions..
On my QSqlTableModel I have the following code to present the data:
Code:
{
if (role == Qt::DisplayRole)
{
var = record(index.row()).value("cnty_nam").toString();
qDebug() << index.row() << displayColumnCode << var;
return var;
}
}
I am just displaying one column thus I just use row. From the debug I can see that the row numbers are fine, also the column exists, but in this case var is always empty! because record(index.row()).value("cnty_nam").isValid() is always false!!!
I initialize the model with:
Code:
m_mainmodel = new maintModel(this,db);
m_mainmodel->setTable("country");
if (m_mainmodel->select())
{
ui->ListView1->setModel(m_mainmodel);
}
For sure is something very silly... but I don't know what is happening!!!
Thanks,
Carlos.
Re: Access data from QSqlTableModel
One question:
"Why are you re-implementing this function?"
You can use QSqlTableModel as it is.
Re: Access data from QSqlTableModel
Well I have more than the display role. For example I need to display a different pixmap based on some extra data attached to every record (that is not stored in the database). I use this under Qt::DecorationRole. That's why i am reimplementing this.
Re: Access data from QSqlTableModel
Ok,
I suggest you to call the parent implementation when you have to fetch data and add your code only to get information specific to your implementation.
Code:
{
if (role == Qt::DecorationRole) {
// Your code
}
else
return result;
}
Re: Access data from QSqlTableModel
Yep!!!.
Many thanks,
Carlos