使用的是自定义委托的方式,重写了panit函数和editorEvent函数实现指定列内嵌复选框,单击实现选中状态切换
主要思路:paint函数里进行复选框的绘制,并实时读取模型数据显示状态,editorEvent函数里判断单元格点击情况进行处理。
源代码如下:
checkdelegate.h
#ifndef CHECKDELEGATE_H
#define CHECKDELEGATE_H
#include <QStyledItemDelegate>
#include <QtGui>
class CheckBoxDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
CheckBoxDelegate(QObject *parent = 0);
protected:
void paint(QPainter* painter,const QStyleOptionViewItem& option,const QModelIndex& index) const;
bool editorEvent(QEvent *event,QAbstractItemModel *model,const QStyleOptionViewItem &option,const QModelIndex &index);
};
#endif // CHECKDELEGATE_H
checkdelegate.cpp
#include "checkdelegate.h"
#include <QMouseEvent>
#include <QPainter>
#i
主要思路:paint函数里进行复选框的绘制,并实时读取模型数据显示状态,editorEvent函数里判断单元格点击情况进行处理。
源代码如下:
checkdelegate.h
#ifndef CHECKDELEGATE_H
#define CHECKDELEGATE_H
#include <QStyledItemDelegate>
#include <QtGui>
class CheckBoxDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
CheckBoxDelegate(QObject *parent = 0);
protected:
void paint(QPainter* painter,const QStyleOptionViewItem& option,const QModelIndex& index) const;
bool editorEvent(QEvent *event,QAbstractItemModel *model,const QStyleOptionViewItem &option,const QModelIndex &index);
};
#endif // CHECKDELEGATE_H
checkdelegate.cpp
#include "checkdelegate.h"
#include <QMouseEvent>
#include <QPainter>
#i