PDA

View Full Version : cant read data from my model of QIdentityProxyModel



KillGabio
11th July 2013, 08:31
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


model_bajo_stock = new QSqlQueryModel ();
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})


QVariant MyModelView::data(const QModelIndex &proxyIndex, int role) const{
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 QBrush(Qt::red);
}
return QIdentityProxyModel::data(proxyIndex, role);
}




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

KillGabio
12th July 2013, 20:15
I modified the code, now it paints red the values i want but i cant change the font because i cant feth the value:



QVariant MyModelView::data(const QModelIndex &proxyIndex, int role) const{
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);
return QBrush(Qt::red);
}
}
/*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)

Santosh Reddy
12th July 2013, 21:15
try this


QVariant data(const QModelIndex & proxyIndex, int role) const
{
if((role == Qt::BackgroundRole) && (proxyIndex.column() == 1))
{
int value = QIdentityProxyModel::data(index(proxyIndex.row(), 1), Qt::DisplayRole).toInt();
if(value < 6)
return QBrush(Qt::red);
}

if((role == Qt::FontRole) && (proxyIndex.column() == 0))
{
int value = QIdentityProxyModel::data(index(proxyIndex.row(), 1), Qt::DisplayRole).toInt();
if(value < 6)
return QFont("Courier", 8, QFont::Bold, true);
}

return QIdentityProxyModel::data(proxyIndex, role);
}

KillGabio
12th July 2013, 21:27
If you were from Argentina, i d be yelling: "GROSOOOOO"