我想通过模型和委托QComboBox
插入QWidgets
(而不是字符串)来自定义 a :
QComboBox *cb = new QComboBox(this);
FeatureModel *featureModel = new FeatureModel(cb);
cb->setModel(featureModel);
ComboBoxItemDelegate *comboBoxItemDelegate = new ComboBoxItemDelegate(cb);
cb->setItemDelegate(comboBoxItemDelegate);
FeatureModel 继承自 QAbstractListModel,ComboBoxItemDelegate 继承自 QStyledItemDelegate。
问题是从未调用过委托方法,因此没有插入我的自定义小部件(我只看到 的字符串FeatureModel
)。但是,如果我使用 aQTableView
而不是 a QComboBox
,它可以正常工作。
有人知道错误在哪里吗?我错过了 QT模型/视图概念的一些重要方面吗?
编辑: 这是我的代表。除了构造函数(当然),没有调用以下方法(控制台上没有输出)。
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
}