QItemDelegate的用法

本文介绍了如何在QTableView中添加控件,包括使用setIndexWidget方法直接添加始终显示的控件,以及利用QItemDelegate及其子类进行控件委托,仅在编辑状态下显示。通过示例详细解释了QItemDelegate的setEditorData和setModelData方法的使用,并强调了参数中的model和index应与delegate中的组件区分。

如果需要向QTableView这样继承于QAbstractItemView的控件中添加控件,有以下两种方法:

  • 使用QAbstractItemView的setIndexWidget方法向指定的位置添加一个控件。这个控件是始终显示的。
  • 使用QAbstractItemView的setItemDelegateForColumn、setItemDelegateForLow方法,向指定的列或行添加一个delegate。这个delegate是一个QItemDelegate对象、或是QItemDelegate的子类对象。添加的delegate并不是始终显示的,而只在进入编辑状态才显示。

示例如下:

#include <QItemDelegate>

// 这是一个用于设置日期的delegate,其实现调用来QDateEdit
class DateDelegate:public QItemDelegate
{
    Q_OBJECT
public:
    DateDelegate();

    // 创建一个控件
    virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;

    // 通过参数index.model()可以将model中对应位置的数据设置到控件中
    virtual void setEditorData(QWidget *editor, const QModelIndex &index) const;

    // 将在此控件中的数据更新到对应的model中
    virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;

    // 用于更新控件的位置
    virtual void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
};

#include "datedelegate.h"

#include <QDateTimeEdit>
#include <QDate>

DateDelegate::DateDelegate(){}

QWidget * DateDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QDateTimeEdit *pDateEditor = new QDateTimeEdit(parent);

    pDateEditor->setDisplayFormat("yyyy-MM-dd");
    pDateEditor->setCalendarPopup(true);
    pDateEditor->installEventFilter(const_cast<DateDelegate *>(this));
    return pDateEditor;
}

void DateDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    QDateTimeEdit *pDateEditor = static_cast<QDateTimeEdit *>(editor);
    QDate date = QDate::fromString(index.model()->data(index).toString(), Qt::ISODate);
    pDateEditor->setDate(date);
}

void DateDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    QDateTimeEdit *pDateEditor = static_cast<QDateTimeEdit *>(editor);
    model->setData(index, pDateEditor->date().toString(Qt::ISODate));
}

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

// 应用于QTableView的示例
QTableView *mpTable = new QTableView(this);
QStandardItemModel *model = new QStandardItemModel(1,4);
mpTable->setModel(model);
mpTable->setGeometry(0,0, 600,400);
DateDelegate *dd = new DateDelegate;
// 向mpTable的第2列(索引从0开始)设置DateDelegate,使这一列的数据都通过DateDelegate中的QDateEdit来编辑
mpTable->setItemDelegateForColumn(1,dd);

// 在mpTable第1行的第4列中添加设置按钮
mpTable->setIndexWidget(model->index(0,3,QModelIndex()), new QPushButton);

这里需要注意的是,setEditorData和setModelData中的index以及setModelData中的model参数都是使用该delegate的table的model index和model,而editor参数则是delegate中创建的组件,不能将参数列表中的model和index误以为是delegate中创建的组件的model和index。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值