Qt中的委托由Delegate类来实现,Delegate类在Qt中的继承框架如下
图中可以看出QAbstractItemDelegate主要被两个类继承:QItemDelegate和QStyleItemDelegate。他们的区别是是否沿袭了用户提供的Qt style sheets。如果是使用了Qt style sheets,选择QStyleItemDelegate类较好。
使用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;
其中
下面实现一个在QTableview中插入QComboBox控件的小例子。
main函数:
#include "delegate.h"
#include
#include
#include
#include
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QStandardItemModel model(4, 2);
QTableView tableView;
tableView.setModel(&model);
ComboBoxDelegate delegate;
tableView.setItemDelegateForColumn(1,&delegate);
tableView.horizontalHeader()->setStretchLastSection(true);
model.setHorizontalHeaderLabels(QStringList() << "名字"<< "成绩");
model.setData(model.index(0, 0, QModelIndex()),QVariant("张三"));
model.setData(model.index(1, 0, QModelIndex()),QVariant("赵四"));
model.setData(model.index(2, 0, QModelIndex()),QVariant("李五"));
model.setData(model.index(3, 0, QModelIndex()),QVariant("王六"));
model.setData(model.index(0, 1, QModelIndex()),QVariant("B"));
model.setData(model.index(1, 1, QModelIndex()),QVariant("C"));
model.setData(model.index(2, 1, QModelIndex()),QVariant("A"));
model.setData(model.index(3, 1, QModelIndex()),QVariant("B"));
tableView.setWindowTitle(QObject::tr("演示程序"));
tableView.show();
return app.exec();
}
#include "delegate.h"
#include
#include
ComboBoxDelegate::ComboBoxDelegate(QObject *parent)
: QStyledItemDelegate(parent)
{
}
QWidget *ComboBoxDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/* option */,
const QModelIndex &/* index */) const
{
QComboBox *editor = new QComboBox(parent);
editor->insertItem(0, "A");
editor->insertItem(1, "B");
editor->insertItem(2, "C");
editor->insertItem(3, "D");
editor->insertItem(4, "E");
return editor;
// QMessageBox::information(NULL, "提示", "0");
}
void ComboBoxDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
QString value = index.model()->data(index, Qt::EditRole).toString();
QComboBox *ComboBox = static_cast(editor);
ComboBox->setCurrentText(value);
//QMessageBox::information(NULL, "提示", "1");
}
void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QComboBox *ComboBox = static_cast(editor);
QString value = ComboBox->currentText();
model->setData(index, value, Qt::EditRole);
// QMessageBox::information(NULL, "提示", "2");
}
void ComboBoxDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
editor->setGeometry(option.rect);
//QMessageBox::information(NULL, "提示", "3");
}
对于委托原理不是很清楚的同学,可以将每个函数下面的QMessageBox函数去注释,看一看程序到底是怎么执行的便可一目了然了。
程序运行结果