SpinBoxDelegate例子是Qt Assistant中提供的一个非常优秀的例子,虽然讲的是继承于QItemDelegate的例子。但对于我们理解Delegate-委托这个概念,非常有帮助。
它重载了必须的几个函数:
(1) QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,const QModelIndex &index) const;
(2) void setEditorData(QWidget *editor, const QModelIndex &index) const;
(3) void setModelData(QWidget *editor, QAbstractItemModel *model,const QModelIndex &index) const;
(4) void updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option, const QModelIndex &index) const;
Main.cpp
- #include <QApplication>
- #include <QHeaderView>
- #include <QItemSelectionModel>
- #include <QStandardItemModel>
- #include <QTableView>
- #include "delegate.h"
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
- //构建一个4行,2列的项模型
- QStandardItemModel model(4, 2);
- //声明一个TableView
- QTableView tableView;
- //绑定模型
- tableView.setModel(&model);
- //声明一个委托
- SpinBoxDelegate delegate;
- //设定视图的委托
- tableView.setItemDelegate(&delegate);
- //ensuring that the view does not waste any of the space assigned to it for its header
- //最后一列全部填充View
- tableView.horizontalHeader()->setStretchLastSection(true);
- //初始化Model
- for (int row = 0; row < 4; ++row) {
- for (int column = 0; column < 2; ++column) {
- QModelIndex index = model.index(row, column, QModelIndex());
- model.setData(index, QVariant((row+1) * (column+1)));
- }
- }
- tableView.setWindowTitle(QObject::tr("Spin Box Delegate"));
- tableView.show();
- return app.exec();
- }
delegate.h
- #ifndef DELEGATE_H
- #define DELEGATE_H
- #include <QItemDelegate>
- #include <QModelIndex>
- #include <QObject>
- #include <QSize>
- #include <QSpinBox>
- class SpinBoxDelegate : public QItemDelegate
- {
- Q_OBJECT
- public:
- SpinBoxDelegate(QObject *parent = 0);
- //返回一个编辑控件,用来编辑指定项的数据
- QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
- const QModelIndex &index) const;
- //将Model中数据赋值到控件上
- 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
delegate.cpp
- #include <QtGui>
- #include "delegate.h"
- SpinBoxDelegate::SpinBoxDelegate(QObject *parent)
- : QItemDelegate(parent)
- {
- }
- //返回一个编辑控件,用来编辑指定项的数据(用户双击时调用此函数,创建对应控件)
- QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
- const QStyleOptionViewItem &/* option */,
- const QModelIndex &/* index */) const
- {
- //返回该QSpinBox控件
- QSpinBox *editor = new QSpinBox(parent);
- editor->setMinimum(0);
- editor->setMaximum(100);
- return editor;
- }
- //将Model中数据赋值到控件上 (双击时调用此函数,控件刚显示时对应的数据)
- 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);
- }
- //设定模型数据,根据指定项中对应编辑控件的数据 (控件隐藏时调用,主要用于回显数据,提供数据给model,用户可以看见)
- 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
- {
- //根据option,设置编辑框位置
- editor->setGeometry(option.rect);
- }