1.自定义的delegate继承自QItemDelegate
2.必须重载的一些函数:
//返回一个编辑控件,用来编辑指定项的数据
(1) QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,const QModelIndex &index) const;
//将Model中数据赋值到控件上
(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
#include
#include
#include "ComboDelegate.h"
#include "DateDelegate.h"
#include "SpinDelegate.h"
#pragma execution_character_set("utf-8")
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//构建一个4行,4列的项模型
QStandardItemModel model(4, 4);
QTableView tableView;
//绑定模型
tableView.setModel(&model);
//声明一个委托
DateDelegate dateDelegate;
tableView.setItemDelegateForColumn(1, &dateDelegate); //表格第一列安装代理
ComboDelegate comboDelegate;
tableView.setItemDelegateForColumn(2, &comboDelegate);
SpinDelegate spinDelegate;
tableView.setItemDelegateForColumn(3, &spinDelegate);
model.setHeaderData(0, Qt::Horizontal, QObject::tr("Name"));
model.setHeaderData(1, Qt::Horizontal, QObject::tr("Birthday"));
model.setHeaderData(2, Qt::Horizontal, QObject::tr("Job"));
model.setHeaderData(3, Qt::Horizontal, QObject::tr("Income"));
//赋值
for (int row = 0; row < 2; row++)
{
model.setData(model.index(row, 0, QModelIndex()), "张三");
model.setData(model.index(row, 1, QModelIndex()), "1992-10-15");
model.setData(model.index(row, 2, QModelIndex()), "律师");
model.setData(model.index(row, 3, QModelIndex()), "20W");
}
QString data = model.data(model.index(0, 1, QModelIndex())).toString();
tableView.setWindowTitle(QObject::tr("Delegate"));
tableView.show();
return a.exec();
}
DateDelegate.h
#include
class DateDelegate : public QItemDelegate
{
Q_OBJECT
public:
DateDelegate(QObject *parent = 0);
~DateDelegate();
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;
};
DateDelegate.cpp
#include "DateDelegate.h"
#include
DateDelegate::DateDelegate(QObject *parent)
: QItemDelegate(parent)
{
}
DateDelegate::~DateDelegate()
{
}
QWidget * DateDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QDateTimeEdit *editor = new QDateTimeEdit(parent);
editor->setDisplayFormat("yyyy-mm-dd");
editor->setCalendarPopup(true);
editor->installEventFilter(const_cast(this));
return editor;
}
void DateDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QString dataStr = index.model()->data(index).toString();
QDate date = QDate::fromString(dataStr, Qt::ISODate);
QDateTimeEdit *edit = static_cast(editor);
edit->setDate(date);
}
void DateDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QDateTimeEdit *edit = static_cast(editor);
QDate date = edit->date();
model->setData(index, QVariant(date.toString(Qt::ISODate)));
}
void DateDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}
ComboDelegate.h
#include
class ComboDelegate : public QItemDelegate
{
Q_OBJECT
public:
ComboDelegate(QObject *parent = 0);
~ComboDelegate();
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;
};
ComboDelegate.cpp
#include "ComboDelegate.h"
#include
ComboDelegate::ComboDelegate(QObject *parent)
: QItemDelegate(parent)
{
}
ComboDelegate::~ComboDelegate()
{
}
QWidget * ComboDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QComboBox *editor = new QComboBox(parent);
editor->addItem(QString::fromLocal8Bit("工人"));
editor->addItem(QString::fromLocal8Bit("农民"));
editor->addItem(QString::fromLocal8Bit("医生"));
editor->addItem(QString::fromLocal8Bit("律师"));
editor->addItem(QString::fromLocal8Bit("军人"));
editor->installEventFilter(const_cast(this));
return editor;
}
void ComboDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QString str = index.model()->data(index).toString();
QComboBox *box = static_cast(editor);
int i = box->findText(str);
box->setCurrentIndex(i);
}
void ComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QComboBox *box = static_cast(editor);
QString str = box->currentText();
model->setData(index, str);
}
void ComboDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}
SpinDelegate.h
#include
class SpinDelegate : public QItemDelegate
{
Q_OBJECT
public:
SpinDelegate(QObject *parent = 0);
~SpinDelegate();
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;
};
SpinDelegate.cpp
#include "SpinDelegate.h"
#include
SpinDelegate::SpinDelegate(QObject *parent)
: QItemDelegate(parent)
{
}
SpinDelegate::~SpinDelegate()
{
}
QWidget * SpinDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QSpinBox *editor = new QSpinBox(parent);
editor->setRange(0, 10000);
editor->installEventFilter(const_cast(this));
return editor;
}
void SpinDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
int value = index.model()->data(index).toInt();
QSpinBox *box = static_cast(editor);
box->setValue(value);
}
void SpinDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QSpinBox *box = static_cast(editor);
int value = box->value();
model->setData(index, value);
}
void SpinDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}