PDA

View Full Version : QAbstractItemDelegate and QLinearGradient



toutarrive
12th February 2010, 11:13
Bonjour,

I've implemented a delegate (inhereted from QAbstractItemDelegate) in a QtableView in order to show the data as a bar chart.



void Delegate:: paint (QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
int value = index.model()->data( index, Qt::DisplayRole ).toInt();
double percent = double(value)/100.0;

painter->save();

QLinearGradient gradient( QPoint ( option.rect.x(), option.rect.y() ), QPoint ( option.rect.x() , option.rect.height() ) );
gradient.setColorAt(0, QColor(42, 142,220) );
gradient.setColorAt(0.5, QColor(11, 90,163) );
gradient.setColorAt(1, QColor(4, 24,40) );
QBrush brush(gradient);
painter->setBrush(brush);
painter->drawRect(option.rect.x(), option.rect.y() , int(percent*(option.rect.width())), option.rect.height() );

painter->restore();

}


The gradient is not rendered at all. Instead Ive got a plain color.

Any ideas ?

Regards

toutarrive
12th February 2010, 11:31
The view is populated this way:




QTableView table;
QStandardItemModel model(totalFile, 2);
Delegate delegate;
table.setItemDelegateForColumn(1, &delegate)

for(int i =0; i<totalFile; ++i)
{
QStandardItem* item = new QStandardItem( QString(" %1 files") . arg(fileCount) );
model.setItem(r, 0, item);
model.setItem(r, 1, new QStandardItem(QString::number((value)));
}

table.setModel(&model);

toutarrive
12th February 2010, 18:20
It's was just a problem of coordonates in QlinearGradient.