由于项目中需要在QtableView中加入复选框和进度条,故自定义tableViewDelegate类,用于实现复选框和进度条功能。
继承自QStyledItemDelegate,重写paint函数,并可在构造函数中加入自己需要的输入参数。
.h
class tableViewDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
explicit tableViewDelegate(QObject *parent = 0,int imust = 0);
~tableViewDelegate();
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
// 自定义响应鼠标事件,更新数据
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);
signals:
// void sent_checked_list(const QList<int> iList) const;
// void sent_checked(const int icheck) const;
private:
int must;
};
.cpp
tableViewDelegate::tableViewDelegate(QObject *parent, int imust)
: QStyledItemDelegate(parent)
{
must = imust;
}
tableViewDelegate::~tableViewDelegate()
{
}
// 绘制复选框
void tableViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem viewOption(option);
initStyleOption(&viewOption, index);
if (option.state.testFlag(QStyle::State_HasFocus))
viewOption.state = viewOption.state ^ QStyle::State_HasFocus;
QStyledItemDelegate::paint(painter, viewOption, index);
if (index.column() == 0)
{
// bool data = index.model()->data(index, Qt::UserRole).toBool();
if(must==1)
{
int data = index.model()->data(index,Qt::UserRole).toInt();
QStyleOptionButton checkBoxStyle;
if(data == 1)
{
//emit sent_checked(index.row());
checkBoxStyle.state = QStyle::State_On;
}else
{
checkBoxStyle.state = QStyle::State_Off;
}
// checkBoxStyle.state |= QStyle::State_Enabled;
checkBoxStyle.iconSize = QSize(20, 20);
checkBoxStyle.rect = option.rect;
checkBoxStyle.state = QStyle::State_On | QStyle::State_ReadOnly;//必测项,默认选择不可修改
QApplication::style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &checkBoxStyle, painter);//, &checkBox);
}else
{
int data = index.model()->data(index,Qt::UserRole).toInt();
QStyleOptionButton checkBoxStyle;
if(data == 1)
{
//emit sent_checked(index.row());
checkBoxStyle.state = QStyle::State_On;
}else
{
checkBoxStyle.state = QStyle::State_Off;
}
//第一行为初始化,不允许点击更改状态
checkBoxStyle.state |= QStyle::State_Enabled;
checkBoxStyle.iconSize = QSize(20, 20);
checkBoxStyle.rect = option.rect;
if(index.row()==0)
{
checkBoxStyle.state = QStyle::State_On | QStyle::State_ReadOnly;
}
QApplication::style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &checkBoxStyle, painter);//, &checkBox);
}
}else if(index.column() == 4)
{
int value = index.model()->data(index).toInt();
QStyleOptionProgressBarV2 progressBarOption;
progressBarOption.rect = option.rect.adjusted(4, 4, -4, -4);
progressBarOption.minimum = 0;
progressBarOption.maximum = 100;
progressBarOption.textAlignment = Qt::AlignRight;
progressBarOption.textVisible = true;
progressBarOption.progress = value;
progressBarOption.text = tr("%1%").arg(progressBarOption.progress);
painter->save();
painter->setPen(QColor(0x19,0x64,0x9f));
if (option.state & QStyle::State_Selected) {
painter->fillRect(option.rect, option.palette.highlight());
painter->setBrush(option.palette.highlightedText());
}
QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter);
painter->restore();
}
}
// 响应鼠标事件,更新数据
bool tableViewDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
QRect decorationRect = option.rect;
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
if (event->type() == QEvent::MouseButtonPress && decorationRect.contains(mouseEvent->pos()))
{
if (index.column() == 0)
{
bool data = model->data(index, Qt::UserRole).toBool();
model->setData(index, !data, Qt::UserRole);
}
}
return QStyledItemDelegate::editorEvent(event, model, option, index);
}