Same problem here:
{
QVariant value
= index.
data(Qt
::EditRole);
QVariant::Type type
= index.
data(SensorPropertyModel
::PropertyTypeRole).
type();
switch(type) {
{
QComboBox* comboBox
= static_cast<QComboBox
*>
(editor
);
value = index.data(SensorPropertyModel::ListIndexRole);
comboBox->setCurrentIndex(value.toInt());
break;
}
default:
break;
}
}
void PropertyItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QVariant value = index.data(Qt::EditRole);
QVariant::Type type = index.data(SensorPropertyModel::PropertyTypeRole).type();
switch(type) {
case QVariant::StringList:
{
QComboBox* comboBox = static_cast<QComboBox*>(editor);
value = index.data(SensorPropertyModel::ListIndexRole);
comboBox->setCurrentIndex(value.toInt());
break;
}
default:
break;
}
}
To copy to clipboard, switch view to plain text mode
void PropertyItemDelegate
::setModelData(QWidget* editor,
const QModelIndex& index) const
{
if(!index.isValid()) {
return;
}
QVariant value
= index.
data(Qt
::EditRole);
QVariant::Type type
= index.
data(SensorPropertyModel
::PropertyTypeRole).
type();
switch(type) {
{
QComboBox* comboBox
= static_cast<QComboBox
*>
(editor
);
value = comboBox->currentText();
int nValue = comboBox->currentIndex();
model->setData(index, nValue, SensorPropertyModel::ListIndexRole);
break;
}
default:
break;
}
model->setData(index, value, Qt::EditRole);
}
void PropertyItemDelegate::setModelData(QWidget* editor,
QAbstractItemModel* model,
const QModelIndex& index) const
{
if(!index.isValid()) {
return;
}
QVariant value = index.data(Qt::EditRole);
QVariant::Type type = index.data(SensorPropertyModel::PropertyTypeRole).type();
switch(type) {
case QVariant::StringList:
{
QComboBox* comboBox = static_cast<QComboBox*>(editor);
value = comboBox->currentText();
int nValue = comboBox->currentIndex();
model->setData(index, nValue, SensorPropertyModel::ListIndexRole);
break;
}
default:
break;
}
model->setData(index, value, Qt::EditRole);
}
To copy to clipboard, switch view to plain text mode
Any help is appreciated!
Added after 18 minutes:
Found a workaround:
{
QComboBox* comboBox
= static_cast<QComboBox
*>
(editor
);
value = index.data(SensorPropertyModel::ListIndexRole);
connect(comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
comboBox->setCurrentIndex(value.toInt());
break;
}
case QVariant::StringList:
{
QComboBox* comboBox = static_cast<QComboBox*>(editor);
value = index.data(SensorPropertyModel::ListIndexRole);
connect(comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
comboBox->setCurrentIndex(value.toInt());
break;
}
To copy to clipboard, switch view to plain text mode
Notice the connect-line.
void PropertyItemDelegate::onCurrentIndexChanged(int nIndex)
{
QComboBox* cb
= static_cast<QComboBox
*>
(sender
());
if(cb) {
emit commitData(cb);
}
}
void PropertyItemDelegate::onCurrentIndexChanged(int nIndex)
{
QComboBox* cb = static_cast<QComboBox*>(sender());
if(cb) {
emit commitData(cb);
}
}
To copy to clipboard, switch view to plain text mode
See here: http://qt-project.org/forums/viewthread/4531
Bookmarks