class SimpleDelegate : public QItemDelegate {public: SimpleDelegate(QObject* pobj = 0) : QItemDelegate(pobj) { }; QWidget *SimpleDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &option,const QModelIndex &index) const{ QLineEdit *LineEdit = new QLineEdit(parent); return LineEdit;}void SimpleDelegate::setEditorData(QWidget *editor,const QModelIndex &index) const { QString Value =index.model()->data(index, Qt::EditRole).toString(); QLineEdit *LineEdit = qobject_cast<QLineEdit*>(editor); LineEdit->setText(Value); }void SimpleDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,const QModelIndex &index) const { QLineEdit *LineEdit = qobject_cast<QLineEdit*>(editor); model->setData(index, LineEdit->text()); }void SimpleDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/* index */) const { editor->setGeometry(option.rect); }void paint(QPainter *pPainter, const QStyleOptionViewItem &option, const QModelIndex &index ) const{ if (option.state & QStyle::State_MouseOver) { QRect rect = option.rect; QLinearGradient gradient(0, 0, rect.width(), rect.height()); gradient.setColorAt(0, Qt::white); gradient.setColorAt(0.5, Qt::blue); gradient.setColorAt(1, Qt::green); pPainter->setBrush(gradient); pPainter->drawRect(rect); } QItemDelegate::paint(pPainter, option, index); } };
C++ (Qt)bool SimpleDelegate::eventFilter ( QObject * editor, QEvent * event ){ QLineEdit *LineEdit = qobject_cast<QLineEdit*>(editor); if (LineEdit && event->type() == QEvent::FocusIn) { LineEdit->deselect(); LineEdit->setCursorPosition(0); } return QItemDelegate::eventFilter(editor, event);}