PDA

View Full Version : how to implement the listwidget delegate?thanks



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!

Lykurg
3rd May 2013, 08:30
What do you want to achieve? Returning a list widget as a editor looks strange. How about a QComboBox?

liuqin820222
3rd May 2013, 08:42
i implement the QComboBox delegate,and i want to know how to implement a listwidget editor or a tablewidget editor even a custom widget (eg. a tablewidget + a pushbutton)? without regard to the list widget's strange looks,how to do it?thanks.