PDA

View Full Version : QComboBox UserData Role not saved?



tranfuga25s
29th December 2011, 17:04
Hi, i'm subclasing a QComboBox class and trying to use the itemData() to store the ID of the table from the database using this code:


void ECBClientes::inicializar()
{
// Cargo los datos del modelo
QSqlQuery cola;
if( cola.exec( "SELECT id, razon_social FROM clientes ORDER BY razon_social ASC" ) ) {
int pos = 0;
while( cola.next() ) {
this->insertItem( pos, cola.record().value(1).toString() );
this->setItemData( pos , cola.record().value(0), Qt::UserRole );
if( this->itemData( pos, Qt::UserRole ).isValid() )
qDebug( this->itemData( pos, Qt::UserRole ).toString().toLocal8Bit() );
pos++;
}
if( pos == 0 ) {
qWarning( "No hay ningun cliente para cargar!" );
this->lineEdit()->setText( "No hay clientes cargados..." );
}
this->setEnabled( true );
this->setCurrentIndex( -1 );
} else {
qWarning( "Error al intentar ejecutar la cola para cargar los clientes" );
qDebug( cola.lastError().text().toLocal8Bit() );
qDebug( cola.lastQuery().toLocal8Bit() );
}
}


but if( this->itemData( pos, Qt::UserRole ).isValid() ) never returns true so no data is stored in the model?
Anyone have the same issue?

Qt 4.7 tested on Linux slackware and debian in two different machines with the same result.