cant read data from my model of QIdentityProxyModel
Hi everyone, im having trouble trying to fetch the data from my QTableView. I have the following structure:
I get the data from the database with a QSqlQueryModel, then I create my model extending from QIdentityProxyModel and set as its source model the Qsqlquerymodel. Finally I set my model as the model of the tableview. The thing is i can get the rows and collumns but not the data of a cell.
Here is the code from:
widget using the table
Code:
if(db.isOpen ()){
model_bajo_stock->setQuery("SELECT Descripcion, Stock_unidades, fecha_modificacion, proveedor FROM TABLE_PRODUCTOS WHERE stock_unidades < 50 ORDER BY Descripcion", db);
if (model_bajo_stock
->lastError
().
type() != QSqlError::NoError) ui->label_error->setText(model_bajo_stock->lastError().text());
else{
myBajoStock= new MyModelView();
myBajoStock->setSourceModel(model_bajo_stock);
ui->tableViewBajoStock->setModel(myBajoStock);
}
ui->tableViewBajoStock->setAlternatingRowColors(true);
ui->tableViewBajoStock->show();
my model: (WHERE I GET THAT I HAVE NO DATA {segmentation fault})
Code:
if (role== Qt::DisplayRole && proxyIndex.column()==1){
QVariant val
=QIdentityProxyModel
::data(proxyIndex, role
);
qDebug()<<val.toString();
}
if (role == Qt::BackgroundRole && proxyIndex.column()==1){
qDebug
()<<
((QSqlQueryModel*)proxyIndex.
model())->record
(proxyIndex.
column()).
value(1).
toString();
}
return QIdentityProxyModel::data(proxyIndex, role);
}
Quote:
"5"
""
"49"
""
"2"
""
I get 3 rows from the Select query as you can see. The values are there but i cannot fetch them when the role is not display.
Thank you all in advance.
Re: cant read data from my model of QIdentityProxyModel
I modified the code, now it paints red the values i want but i cant change the font because i cant feth the value:
Code:
if (role == Qt::BackgroundRole && proxyIndex.column()==1){
int value= proxyIndex.model()->data(proxyIndex.model()->index(proxyIndex.row(),1),Qt::DisplayRole).toInt();
if (value < 6){
//proxyIndex.model()->setData(proxyIndex.model()->index(proxyIndex.row(),0), QFont("Courier", 8, QFont::Bold, true), Qt::FontRole);
}
}
/*if (role == Qt::FontRole && proxyIndex.column()==0){
int value= proxyIndex.model()->data(proxyIndex.model()->index(proxyIndex.row(),1),Qt::DisplayRole).toInt();
if (value < 6){
return QFont("Courier", 8, QFont::Bold, true);
}
}*/
return QIdentityProxyModel::data(proxyIndex, role);
}
(I want to make the commented "if" to work ->when executed it throws segmentation faul)
Re: cant read data from my model of QIdentityProxyModel
try this
Code:
{
if((role == Qt::BackgroundRole) && (proxyIndex.column() == 1))
{
int value = QIdentityProxyModel::data(index(proxyIndex.row(), 1), Qt::DisplayRole).toInt();
if(value < 6)
}
if((role == Qt::FontRole) && (proxyIndex.column() == 0))
{
int value = QIdentityProxyModel::data(index(proxyIndex.row(), 1), Qt::DisplayRole).toInt();
if(value < 6)
}
return QIdentityProxyModel::data(proxyIndex, role);
}
Re: cant read data from my model of QIdentityProxyModel
If you were from Argentina, i d be yelling: "GROSOOOOO"