{
comboBoxOption.rect = option.rect;
comboBoxOption.
state = QStyle::State_Enabled;
comboBoxOption.frame = true;
comboBoxOption.currentText = index.data().toString();
//QApplication::style()->drawControl(QStyle::CE_ComboBoxLabel, &comboBoxOption, painter);
}
{
qDebug() << "Editor created";
// ComboBox ony in column 2
// Create the combobox and populate it
int row = index.row();
cb
->addItem
(QString("one in row %1").
arg(row
));
cb
->addItem
(QString("two in row %1").
arg(row
));
cb
->addItem
(QString("three in row %1").
arg(row
));
return cb;
}
{
if(QComboBox *cb
= qobject_cast<QComboBox
*>
(editor
)) { // get the index of the text in the combobox that matches the current value of the itenm
QString currentText
= index.
data(Qt
::EditRole).
toString();
int cbIndex = cb->findText(currentText);
// if it is valid, adjust the combobox
if(cbIndex >= 0)
cb->setCurrentIndex(cbIndex);
} else {
QStyledItemDelegate::setEditorData(editor, index);
}
}
{
if(QComboBox *cb
= qobject_cast<QComboBox
*>
(editor
)) // save the current text of the combo box as the current value of the item
model->setData(index, cb->currentText(), Qt::EditRole);
else
QStyledItemDelegate::setModelData(editor, model, index);
}
{
editor->setGeometry(option.rect);
}
inline void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionComboBox comboBoxOption;
comboBoxOption.rect = option.rect;
comboBoxOption.state = QStyle::State_Enabled;
comboBoxOption.frame = true;
comboBoxOption.currentText = index.data().toString();
QApplication::style()->drawComplexControl(QStyle::CC_ComboBox, &comboBoxOption, painter);
//QApplication::style()->drawControl(QStyle::CE_ComboBoxLabel, &comboBoxOption, painter);
}
inline QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
qDebug() << "Editor created";
// ComboBox ony in column 2
// Create the combobox and populate it
QComboBox *cb = new QComboBox(parent);
int row = index.row();
cb->addItem(QString("one in row %1").arg(row));
cb->addItem(QString("two in row %1").arg(row));
cb->addItem(QString("three in row %1").arg(row));
return cb;
}
inline void setEditorData ( QWidget *editor, const QModelIndex &index ) const
{
if(QComboBox *cb = qobject_cast<QComboBox *>(editor)) {
// get the index of the text in the combobox that matches the current value of the itenm
QString currentText = index.data(Qt::EditRole).toString();
int cbIndex = cb->findText(currentText);
// if it is valid, adjust the combobox
if(cbIndex >= 0)
cb->setCurrentIndex(cbIndex);
} else {
QStyledItemDelegate::setEditorData(editor, index);
}
}
inline void setModelData ( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
{
if(QComboBox *cb = qobject_cast<QComboBox *>(editor))
// save the current text of the combo box as the current value of the item
model->setData(index, cb->currentText(), Qt::EditRole);
else
QStyledItemDelegate::setModelData(editor, model, index);
}
inline void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
editor->setGeometry(option.rect);
}
To copy to clipboard, switch view to plain text mode
Bookmarks