PDA

View Full Version : relationalModel() pointer and segfault



tranfuga25s
30th July 2007, 14:51
Using qt4 with a QSqlRelationalTableModel, if I try to use one method of the relational model with:



MProductos *m = qobject_cast<MProductos *>(this->relationModel( 2 ));
flag = m->productoVendido( id, cant );
inside of the method productoVendido I get SegFault:


bool MProductos::productoVendido( QVariant id, QVariant cantidad )
{
qDebug( "Actualizando stock por venta" );
QModelIndex indice = index( id.toInt(), 6, QModelIndex() ); /// Here I get segfault
if( !indice.isValid() )
{
qDebug( "Error de indice al intentar actualizar productos! - indice invalido" );
return false;
}
QVariant cantAnterior = this->data( this->index( id.toInt(), 6 ), Qt::EditRole );
QVariant cantActual = QVariant::fromValue( cantAnterior.toDouble() - cantidad.toDouble() );
return this->setData( this->index( id.toInt(), 6 ), cantActual );
}
It's correct the using that I geave to the pointer o shud I make a new instance of the model?

Thanks:o

marcel
30th July 2007, 15:07
Either 6 is an out of range column or id is not valid.
Could you see what id.isNull() and id.isValid() return?

Regards

tranfuga25s
30th July 2007, 15:21
No, I cant, because the program gets segfault in that exact point.
The column 6 exists, I've used it on the constructor of the model:



setHeaderData( 6, Qt::Horizontal, tr( "Stock" ) );
and id, comes from the original model, I get it with:


QVariant id = QSqlTableModel::data( this->index( fila, 2 ) );
so, seems that the id it's valid.
Even I change the qobject_cast<.... with a "new" operator, still getting invalid index in every situation.

I'm using sqlite. May be that the problem, the method venderProducto( ... ) is called from inside another model. May be that i cant update that data because, the original model is locking the db??