Custom Delegate of QT's TableView

1 Introduction

    When using Table, we hope that we can do better personalization of the table. For example, you need to beautify the table, add controls to the table, and so on. Delegate will be used at this time. To put it bluntly, Delegate adds a layer of rendering and processing functions between the data and the interface.

2.QStyledItemDelegate

        QT provides two base classes, QStyleDelegate and QStyledItemDelegate, to provide us with custom delegates. QStyleDelegate was used a lot before QT4.4, and QStyledItemDelegate was basically used after QT4.4. We implement customization by subclassing QStyledItemDelegate, and reimplement paint(), and possibly sizeHint(). The paint() function is called separately for each item. Using sizeHint(), you can specify a hint for each item. (These two functions beautify the form). If we need to add some controls to the table, like Button. . . We need to implement the following four functions when we wait:                                                                          createEditor() returns the control used to change the data from the model, which can be reimplemented to customize the editing behavior.                                                                  setEditorData() provides the data to be manipulated for the control.                                                                                                                                    updateEditorGeometry()Ensure that the editor is displayed correctly relative to the item view.                                                                                                       setModelData() returns the updated data to the model.

3. Code

delegate.h

class ButtonDelegate : public QStyledItemDelegate
{
    Q_OBJECT

public:
    ButtonDelegate(QObject *parent = 0);
    //index是表中的item索引,可以获取表中item的相关信息。
    //option可以获得表格中格子的大小,位置。。等信息
    QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;

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

    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    //editor就是自定义控件
    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;
};

delegate.cc

#include "ui_button_delegate.h"

QWidget *ButtonDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if (index.row() % 2)
    {
        QPushButton* box = new QPushButton(parent);
        box->setText("Delete");
        return box;
    }
    else
    {
        QLineEdit *lineEdit = new QLineEdit(parent);
        return lineEdit;
    }
}

void ButtonDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
}

void ButtonDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{

}

void ButtonDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    editor->setGeometry(option.rect);
}

ButtonDelegate::ButtonDelegate(QObject *parent)
    : QStyledItemDelegate(parent)
{
}

QSize ButtonDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    return QSize(10, 10);
}

void ButtonDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if (!index.isValid() || option.state == QStyle::State_None || !index.isValid())
        return;
    if (index.row() % 2)
    {
        painter->fillRect(option.rect, QColor(Qt::red));
    }
    else
    {
        painter->fillRect(option.rect, QColor(Qt::yellow));
    }
}

effect:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/Mario_z/article/details/85334016