QTableView 委任用法QStyledItemDelegate

The QStyledItemDelegate class provides display and editing facilities for data items from a model.

委任是提供展示编辑模型数据的一种工具,比如在表格中,你想让表格具有下拉框,spinbox等组件的功能。就可以用委任来实现。

The QStyledItemDelegate class is one of the Model/View Classes and is part of Qt's model/view framework. The delegate allows the display and editing of items to be developed independently from the model and view

委任类是模型/视图,以及模型试图框架的一部分,模型/试图允许委任独立的展示编辑以及开发。

实现委任的方法有两大类:来自于他自身的虚函数

virtual QWidget *

createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const

virtual void

paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const

virtual void

setEditorData(QWidget *editor, const QModelIndex &index) const

virtual void

setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const

virtual QSize

sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const

virtual void

updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const

1:qt 自身没有自带的组件

If the delegate does not support painting of the data types you need or you want to customize the drawing of items, you need to subclass QStyledItemDelegate, and reimplement paint() and possibly sizeHint(). The paint() function is called individually for each item, and with sizeHint(), you can specify the hint for each of them

如果委任不支持对数据类型的绘画,那末你就需要自己去绘画这些项目。你需要实现paint()可能还有sizehint().

2: qt自身拥有的组件

It is possible for a custom delegate to provide editors without the use of an editor item factory. In this case, the following virtual functions must be reimplemented:

一个定制的委托可以在不使用编辑器项目工厂的情况下提供编辑器。在这种情况下,必须重新实现以下虚拟功能;就是这四种函数。


本章节主要讲解第二部分:先看图:

QTableView 委任用法QStyledItemDelegate

main.cpp

#include <QApplication>

#include <QHeaderView>
#include <QStandardItemModel>
#include <QTableView>
#include<QHBoxLayout>
#include"delegate.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    ////////////////[1]
    QStandardItemModel *model=new QStandardItemModel;
    model->setColumnCount(2);
    model->setRowCount(4);
    ////////////////////////[2]
    QTableView *view=new QTableView;
    view->verticalHeader()->hide();
    view->setAlternatingRowColors(true);
    view->setStyleSheet("QTableView{background-color: #4682B4;"
            "alternate-background-color: #D8BFD8;}");
    view->setModel(model);
    spinboxdelegate delegate;
    view->setItemDelegateForColumn(1,&delegate);//将表格的第一列进行委任。
    view->horizontalHeader()->setStretchLastSection(true);
/////////////////////////[3]
    QWidget *widget=new QWidget;
    QPalette pal=widget->window()->palette();
    pal.setColor(QPalette::Window, "#B0C4DE");
    widget->setFixedSize(400,500);
    QHBoxLayout *hb=new QHBoxLayout;
    hb->setContentsMargins(50,100,50,200);
    hb->addWidget(view);
    widget->setLayout(hb);
    widget->setPalette(pal);
    widget->show();
    return a.exec();
}
delegate.cpp

#include"delegate.h"

#include<QSpinBox>
#include<QComboBox>
spinboxdelegate::spinboxdelegate(QObject *parent):QStyledItemDelegate(parent)
{
}
QWidget *spinboxdelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QComboBox *box=new  QComboBox(parent);
    box->addItem("男");
    box->addItem("女");
    box->setFrame(false);
    return box;
}
void spinboxdelegate::setEditorData(QWidget *editor, const QModelIndex &index)const
{
    QString  value=index.model()->data(index,Qt::EditRole).toString();
    QComboBox *box=static_cast<QComboBox*>(editor);
    box->setCurrentText(value);
}
void spinboxdelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index)const
{
    QComboBox *box=static_cast<QComboBox*>(editor);
          box->internalWinId();
          QString  value = box->currentText();
          model->setData(index, value, Qt::EditRole);
}
void spinboxdelegate::updateEditorGeometry(QWidget *editor,
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
    editor->setGeometry(option.rect);
}
delegate.h

#ifndef DELEGATE_H

#define DELEGATE_H
#include<QObject>
#include<QStyledItemDelegate>

class spinboxdelegate:public QStyledItemDelegate
{
    Q_OBJECT
public:
    explicit spinboxdelegate(QObject *parent=0);

      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 // DELEGATE_H