代码: #ifndef WIDGET_H #define WIDGET_H #include <qwidget> class QStringListModel; class QListView; namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); private: Ui::Widget *ui; QStringListModel *model; QListView *listView; }; #endif // WIDGET_H #ifndef SPINBOXDELEGATE_H #define SPINBOXDELEGATE_H #include <qstyleditemdelegate> #include <qmodelindex> #include <qstyleoptionviewitem> class SpinBoxDelegate : public QStyledItemDelegate { Q_OBJECT public: SpinBoxDelegate(QObject *parent = 0) : QStyledItemDelegate(parent) {} QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const; void setEditorData(QWidget *editor, const QModelIndex &index) const; void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const; void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const; }; #endif // SPINBOXDELEGATE_H #include "widget.h" #include "ui_widget.h" #include <qpushbutton> #include <qhboxlayout> #include <qvboxlayout> #include <qstringlist> #include <qpushbutton> #include <qhboxlayout> #include <qstringlistmodel> #include <qlistview> #include <qdebug> #include "SpinBoxDelegate.h" Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { QStringList data1; data1 << "0" << "1" << "2"; model = new QStringListModel(this); model->setStringList(data1); listView = new QListView(this); listView->setModel(model); listView->setItemDelegate(new SpinBoxDelegate(listView)); qDebug() <<listview->selectionModel(); QPushButton *btnShow = new QPushButton(("Show Model"), this); connect(btnShow, SIGNAL(clicked()), this, SLOT(showModel())); QHBoxLayout *buttonLayout = new QHBoxLayout; buttonLayout->addWidget(btnShow); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(listView); layout->addLayout(buttonLayout); setLayout(layout); ui->setupUi(this); } Widget::~Widget() { delete ui; } #include "SpinBoxDelegate.h" #include <qspinbox> QWidget *SpinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & /* option */, const QModelIndex & /* index */) const { QSpinBox *editor = new QSpinBox(parent); editor->setMinimum(0); editor->setMaximum(100); return editor; } void SpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { int value = index.model()->data(index, Qt::EditRole).toInt(); QSpinBox *spinBox = static_cast<qspinbox>(editor); spinBox->setValue(value); } void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { QSpinBox *spinBox = static_cast<qspinbox>(editor); spinBox->interpretText(); int value = spinBox->value(); model->setData(index, value, Qt::EditRole); } void SpinBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const { editor->setGeometry(option.rect); } #include "widget.h" #include <qapplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); return a.exec(); } </qapplication></qspinbox></qspinbox></qspinbox></listview-></qdebug></qlistview></qstringlistmodel></qhboxlayout></qpushbutton></qstringlist></qvboxlayout></qhboxlayout></qpushbutton></qstyleoptionviewitem></qmodelindex></qstyleditemdelegate></qwidget>