【发布时间】:2012-11-17 12:41:59
【问题描述】:
我想通过 model 和 delegate 插入 QWidgets(而不是字符串)来自定义 QComboBox:
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 的字符串)。
但是,如果我使用QTableView 而不是QComboBox,它会正常工作。
有人知道错误在哪里吗? 我是否遗漏了 QT Model/View 概念的一些重要方面?
编辑: 这是我的代表。 除了构造函数(当然),没有调用以下方法(控制台上没有输出)。
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
}
【问题讨论】:
-
它应该可以正常工作,你能粘贴你的委托代码吗?
标签: c++ qt delegates qcombobox model-view