void DetailProgressBar::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem
view_option(option);
if (view_option.state & QStyle::State_HasFocus) {
view_option.state = view_option.state ^ QStyle::State_HasFocus;
}
QStyledItemDelegate::paint(painter, view_option, index);
if (index.column() == 1) {
const QAbstractItemModel *item_model = index.model();
QModelIndex index1 = item_model->index(index.row(), 2, QModelIndex());
QString name = item_model->data(index1, Qt::DisplayRole).toString();
QModelIndex index2 = item_model->index(index.row(), 4, QModelIndex());
qint64 total_size = item_model->data(index2, Qt::DisplayRole).toLongLong();
QModelIndex index3 = item_model->index(index.row(), 5, QModelIndex());
double speed = item_model->data(index3, Qt::DisplayRole).toDouble();
QModelIndex index4 = item_model->index(index.row(), 3, QModelIndex());
qint64 size = item_model->data(index4, Qt::DisplayRole).toLongLong();
QString str_speed = Util::getSpeed(speed);
if(total_size <= 0)
total_size = 1;
double d_size = (size*1.0)/total_size;
QString q_size = QString::number(d_size, 'f', 2);
qint64 progress = q_size.toDouble() * 100;
if(progress > 100)
{
progress = 100;
}
//设置进度条的风格
QStyleOptionProgressBar
progress_bar_option;
progress_bar_option.textAlignment = Qt::AlignCenter;
progress_bar_option.rect = QRect(option.rect.x()+5, option.rect.y()+option.rect.height()-4-6,
option.rect.width()-20, 6);
progress_bar_option.minimum = 0;
progress_bar_option.maximum = 100;
progress_bar_option.progress = progress;
painter->drawText(QRect(option.rect.x()+5, option.rect.y()+4, option.rect.width(), 16), QString("%1").arg(name));
painter->drawText(QRect(option.rect.right()-50, option.rect.y()+4, option.rect.width(), 16), QString("%1").arg(str_speed));
QProgressBar progress_bar;
//绘制进度条
QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progress_bar_option, painter, &progress_bar);
}
}
1、进度条样式
QProgressBar{
border:none;
background:rgb(210, 225, 240);
border-radius:3px;
text-align:center;
}
QProgressBar::chunk {
background:rgb(60, 140, 220);
border-radius:3px;
}
2、进度条文本
注: