QStyledItemDelegate在VATest项目中的应用

由于项目中需要在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);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值