Hello!
I’m implementing an item delegate to extend the QStyledItemDelegate. The idea is whe an value is a QStringList i use a QComboBox as editor in the item delegate. But i have some problems
1. When i set the QStringList in the data of a view (lets say a tree), the cell doesn’t show a text. When i double click the cell a combobox appears, i can make a selection, the combobox closes and nothin is diplayed in the cell
2. When i want to know what is the selection i can’t figure ir out how to get the item selected in the QComboBox.
Any ideas?
class ItemDelegate : public QStyledItemDelegate {
Q_OBJECT
public:
ItemDelegate
(QObject *parent
= 0): QStyledItemDelegate
(parent
) {;
} virtual ~ItemDelegate() { ; }
editor->setGeometry(option.rect);
}
};
class ItemDelegate : public QStyledItemDelegate {
Q_OBJECT
public:
ItemDelegate(QObject *parent = 0): QStyledItemDelegate(parent) {;}
virtual ~ItemDelegate() { ; }
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const;
inline void updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option, const QModelIndex& ) const {
editor->setGeometry(option.rect);
}
};
To copy to clipboard, switch view to plain text mode
QVariant data
= index.
model()->data
(index, Qt
::EditRole);
switch( data.type() ) {
dynamic_cast<QComboBox*>(editor)->setFrame(false);
break;
default:
editor = QStyledItemDelegate::createEditor(parent, option, index);
}
return editor;
}
void ItemDelegate
::setEditorData(QWidget *editor,
QVariant data
= index.
model()->data
(index, Qt
::EditRole);
switch( data.type() ) {
static_cast<QComboBox*>(editor)->addItems(data.toStringList());
break;
default:
QStyledItemDelegate::setEditorData(editor, index);
}
}
{
QVariant data
= index.
model()->data
(index, Qt
::EditRole);
switch( data.type() ) {
for( int i = 0; i < static_cast<QComboBox*>(editor)->count(); ++i )
list << static_cast<QComboBox*>(editor)->itemText(i);
model
->setData
(index,
QVariant(list
), Qt
::EditRole);
break;
}
default:
QStyledItemDelegate::setModelData(editor, model, index);
}
}
QWidget* ItemDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem & option ,
const QModelIndex & index ) const {
QVariant data = index.model()->data(index, Qt::EditRole);
QWidget* editor;
switch( data.type() ) {
case QVariant::StringList:
editor = new QComboBox(parent);
dynamic_cast<QComboBox*>(editor)->setFrame(false);
break;
default:
editor = QStyledItemDelegate::createEditor(parent, option, index);
}
return editor;
}
void ItemDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const {
QVariant data = index.model()->data(index, Qt::EditRole);
switch( data.type() ) {
case QVariant::StringList:
static_cast<QComboBox*>(editor)->addItems(data.toStringList());
break;
default:
QStyledItemDelegate::setEditorData(editor, index);
}
}
void ItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QVariant data = index.model()->data(index, Qt::EditRole);
switch( data.type() ) {
case QVariant::StringList: {
QStringList list;
for( int i = 0; i < static_cast<QComboBox*>(editor)->count(); ++i )
list << static_cast<QComboBox*>(editor)->itemText(i);
model->setData(index, QVariant(list), Qt::EditRole);
break;
}
default:
QStyledItemDelegate::setModelData(editor, model, index);
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks