// Method 1: look up the string in the combobox
{
QItemDelegate::setEditorData( pEditor, index
);
// use base class to fill the strings
if ( pCombo )
{
QString displayStr
= index.
data( Qt
::DisplayRole ).
toString();
int selected = pCombo->findText( displayStr );
if ( selected > -1 )
pCombo->setCurrentIndex( selected );
}
}
// Method 2: use Qt::UserRole to store the index in the model
{
QItemDelegate::setEditorData( pEditor, index
);
// use base class to fill the strings
if ( pCombo )
{
int selected = index.data( Qt::UserRole ).toInt();
if ( selected > -1 )
pCombo->setCurrentIndex( selected );
}
}
{
QItemDelegate::setModelData( pEditor, pModel, index
);
// base class sets Qt::DisplayRole string
if ( pCombo )
{
int selected = pCombo->currentIndex();
pModel
->setData
( index,
QVariant( selected
), Qt
::UserRole );
}
}
{
if ( index.column() == 0 && role == Qt::UserRole )
{
int selected = value.toInt();
// store "selected" as a field in your model for that row.
}
else if ( role == Qt::EditRole )
{
// do whatever you would do otherwise to update your model
}
}
{
if ( index.column() == 0 && role == Qt::UserRole )
{
int selected; // retrieve "selected" from the field in your model for that row.
}
else if ( role == Qt::DisplayRole ) (or EditRole or whatever)
{
// do whatever you would do otherwise to get data from your model
}
}
// Method 1: look up the string in the combobox
void MyComboDelegate::setEditorData( QWidget * pEditor, const QModelIndex & index ) const
{
QItemDelegate::setEditorData( pEditor, index ); // use base class to fill the strings
QComboBox * pCombo = qobject_cast< QComboBox * >( pEditor );
if ( pCombo )
{
QString displayStr = index.data( Qt::DisplayRole ).toString();
int selected = pCombo->findText( displayStr );
if ( selected > -1 )
pCombo->setCurrentIndex( selected );
}
}
// Method 2: use Qt::UserRole to store the index in the model
void MyComboDelegate::setEditorData( QWidget * pEditor, const QModelIndex & index ) const
{
QItemDelegate::setEditorData( pEditor, index ); // use base class to fill the strings
QComboBox * pCombo = qobject_cast< QComboBox * >( pEditor );
if ( pCombo )
{
int selected = index.data( Qt::UserRole ).toInt();
if ( selected > -1 )
pCombo->setCurrentIndex( selected );
}
}
void MyComboDelegate::setModelData( QWidget * pEditor, QAbstractItemModel * pModel, const QModelIndex & index ) const
{
QItemDelegate::setModelData( pEditor, pModel, index ); // base class sets Qt::DisplayRole string
QComboBox * pCombo = qobject_cast< QComboBox * >( pEditor );
if ( pCombo )
{
int selected = pCombo->currentIndex();
pModel->setData( index, QVariant( selected ), Qt::UserRole );
}
}
void MyModel::setData( const QModelIndex & index, const QVariant & value, int role )
{
if ( index.column() == 0 && role == Qt::UserRole )
{
int selected = value.toInt();
// store "selected" as a field in your model for that row.
}
else if ( role == Qt::EditRole )
{
// do whatever you would do otherwise to update your model
}
}
QVariant MyModel::data( const QModelIndex & index, int role ) const
{
if ( index.column() == 0 && role == Qt::UserRole )
{
int selected; // retrieve "selected" from the field in your model for that row.
return QVariant( selected );
}
else if ( role == Qt::DisplayRole ) (or EditRole or whatever)
{
// do whatever you would do otherwise to get data from your model
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks