I want to customize a QComboBox
by inserting QWidgets
(instead of strings) through a model and a delegate:
QComboBox *cb = new QComboBox(this);
FeatureModel *featureModel = new FeatureModel(cb);
cb->setModel(featureModel);
ComboBoxItemDelegate *comboBoxItemDelegate = new ComboBoxItemDelegate(cb);
cb->setItemDelegate(comboBoxItemDelegate);
FeatureModel inherits from QAbstractListModel and ComboBoxItemDelegate inherits from QStyledItemDelegate.
The problem is that the delegates methods are never called and therefore my custom widget is not inserted (I only see the strings of the FeatureModel
).
However, if I use a QTableView
instead of a QComboBox
, it works as it should.
Does someone know where the error lies? Am I missing some important aspect of the QT Model/View concept?
EDIT: Here is my Delegate. Except of the constructor (of course), none of the following methods are called (no ouput on the console).
ComboBoxItemDelegate::ComboBoxItemDelegate(QObject *parent) :
QStyledItemDelegate(parent)
{
qDebug() << "Constructor ComboBoxItemDelegate";
}
ComboBoxItemDelegate::~ComboBoxItemDelegate()
{
}
QWidget* ComboBoxItemDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
qDebug() << "createEditor"; // never called
QWidget *widget = new QWidget(parent);
Ui::ComboBoxItem cbi;
cbi.setupUi(widget);
cbi.label->setText(index.data().toString());
widget->show();
return widget;
}
void ComboBoxItemDelegate::setEditorData ( QWidget *editor, const QModelIndex &index ) const
{
qDebug() << "setEditorData"; // never called
}
void ComboBoxItemDelegate::setModelData ( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
{
qDebug() << "setModelData"; // never called
}