Вот это я вызываю в конструкторе класса наследуемого от QTreeView в своем проекте
setItemDelegateForColumn(progressColumn, new ProgressBarDelegate);
Вот так выглядит заголовок делегата
#include <QtGui/QItemDelegate>
#include <QProgressBar>
class ProgressBarDelegate : public QItemDelegate
{
Q_OBJECT
public:
ProgressBarDelegate(QWidget *parent = 0, Qt::Alignment alignment = Qt::AlignCenter) : QItemDelegate(parent), align(alignment) {}
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const;
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const;
private:
Qt::Alignment align;
};
Вот так выглядит его реализация:
#include "progressbardelegate.h"
void ProgressBarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
int progress = index.data(Qt::DisplayRole).toInt();
QStyleOptionProgressBar progressBarOption;
progressBarOption.rect = option.rect;
progressBarOption.minimum = 0;
progressBarOption.maximum = 100;
progressBarOption.progress = progress;
if (progress == -1)
{
progressBarOption.text = tr("Waiting.");
}
else if (progress == -2)
{
progressBarOption.text = tr("Pausing.");
}
else
{
progressBarOption.text = QString::number(progress) + "%";
}
progressBarOption.textVisible = true;
progressBarOption.textAlignment = Qt::AlignHCenter;
QApplication::style()->drawControl(QStyle::CE_ProgressBar,
&progressBarOption, painter);
}
QWidget *ProgressBarDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/*option */,
const QModelIndex &/*index */) const
{
QProgressBar *progressBar = new QProgressBar(parent);
return progressBar;
}
void ProgressBarDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
int value = index.data(Qt::DisplayRole).toInt();
QProgressBar *progressBar = qobject_cast<QProgressBar *>(editor);
if (value == -1)
{
progressBar->setFormat(tr("Waiting."));
progressBar->setValue(0);
}
else if (value == -2)
{
progressBar->setFormat(tr("Pausing."));
progressBar->setValue(0);
}
else
{
progressBar->setFormat("%p%");
progressBar->setValue(value);
}
}
void ProgressBarDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QProgressBar *progressBar = qobject_cast<QProgressBar *>(editor);
QString str = QString::number(progressBar->value());
model->setData(index, str+"%", Qt::DisplayRole);
}
Вот так оно вызывается из модели (в переопределенной виртуальной ф-ции)
QVariant MyTableModel::data(const QModelIndex &index, int role) const
case progressColumn:
if (command->state() == CommandInformation::New)
{
return QString("-1");
}
else if (command->state() == CommandInformation::Pausing)
{
return QString("-2");
}
else
{
return QString::number(command->progress());
}
break;
Результатом является вполне функциональный прогрессбар в ячейке (у меня он отображает процент загрузки файла в дайнлоад менеджере)