一、效果图
二、工程构建思路
依据mvc模型,重写QTableView QAbstractTableModel QStyledItemDelegate QHeaderView,利用QStyleOptionProgressBar自定义进度条样式,重写createEditor等必要函数实现自定义编辑输入框。
三、代码片段
//样式代理部分
#include "tabledelegate.h"
TableDelegate::TableDelegate(QObject *parent):
QStyledItemDelegate(parent)
{
}
void TableDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
//去掉Focus
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() == 7){
int nProgress = index.model()->data(index, Qt::DisplayRole).toInt();
int nLeft = 8;
int nTop = 8;
int nWidth = option.rect.width() - 2 * nLeft;
int nHeight = option.rect.height() - 2 * nTop;
QStyleOptionProgressBar progressBarOption;
progressBarOption.initFrom(option.widget);
progressBarOption.rect = QRect(option.rect.left() + nLeft, option.rect.top() + nTop, nWidth, nHeight);
progressBarOption.minimum = 0;
progressBarOption.maximum = 100;
progressBarOption.textAlignment = Qt::AlignCenter;
progressBarOption.progress = nProgress;
progressBarOption.text = QString("%1%").arg(nProgress);
progressBarOption.textVisible = true;