liuqin820222
3rd May 2013, 05:45
i want to implement the listwidget delegate,the code as follows:
QWidget *DeComBox::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if(index.column()==0)
{
QListWidget* editor=new QListWidget(parent);
return editor;
}
else{
return QItemDelegate::createEditor(parent, option, index);
}
}
void DeComBox::setEditorData(QWidget *editor, const QModelIndex &index) const
{
if(index.column()==0)
{
QListWidget* editor=qobject_cast<QListWidget*>(editor);
editor->addItem("eg1");
editor->addItem("eg2");
editor->addItem("eg3");
editor->addItem("eg4");
editor->setCurrentRow(1);
}
else {
QItemDelegate::setEditorData(editor, index);
}
}
void DeComBox::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
if(index.column()==0)
{
QListWidget* editor=qobject_cast<QListWidget*>(editor);
model->setData(index,editor->currentItem()->text(),Qt::EditRole);
}
else {
QItemDelegate::setModelData(editor, model, index);
}
}
but not successfully,why, help me , thanks!
QWidget *DeComBox::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if(index.column()==0)
{
QListWidget* editor=new QListWidget(parent);
return editor;
}
else{
return QItemDelegate::createEditor(parent, option, index);
}
}
void DeComBox::setEditorData(QWidget *editor, const QModelIndex &index) const
{
if(index.column()==0)
{
QListWidget* editor=qobject_cast<QListWidget*>(editor);
editor->addItem("eg1");
editor->addItem("eg2");
editor->addItem("eg3");
editor->addItem("eg4");
editor->setCurrentRow(1);
}
else {
QItemDelegate::setEditorData(editor, index);
}
}
void DeComBox::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
if(index.column()==0)
{
QListWidget* editor=qobject_cast<QListWidget*>(editor);
model->setData(index,editor->currentItem()->text(),Qt::EditRole);
}
else {
QItemDelegate::setModelData(editor, model, index);
}
}
but not successfully,why, help me , thanks!