[Qt-interest] Animated QProgressBar in a QTableView

Stephen Kelly steveire at gmail.com
Thu Sep 2 19:12:04 CEST 2010


Brad Howes wrote:
> void
> NameItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem&
> option,
> 
> const QModelIndex& index ) const
> {
>     const FileModel* model = static_cast<const FileModel*>( index.model()
>     ); const Emitter* emitter = model->getEmitter( index.row() );
>     double loadPercentage = emitter->getLoadPercentage();
>     if ( loadPercentage ) {
> 
> QRect rect = option.rect;
> 
> rect.setWidth( rect.width() * loadPercentage );
> 
> painter->fillRect( rect, option.palette.highlight() );
>     }
>     Super::paint( painter, option, index );
> }

Not on topic for this thread, but I have to explain how you are supposed to 
get the percentage without coupling the delegate to a particular model:

paint(...)
{
  double percentage = index.data(FileModel::PercentageRole).toDouble();
  if ( percentage )
...

}

and 

QVariant FileModel::data(index, role)
{
  if (role == PercentageRole)
  {
    return m_emitters[index.row()]->getLoadPercentage();
  }
  ...
}

That way you can put a proxy in the view in the future instead of the 
FileModel itself.

Of course, the delegate still knows there is a FileModel somewhere below it, 
but even the role to use for percentages could be passed into the delegate 
ctor if you wanted.

If you want real decoupling of model from everything else (the whole point) 
never put additional accessors (like getEmitters) on your model.

Maybe you already know that and don't care because you don't need a proxy 
(today!), but I thought I'd point it out for the lurkers (they're watching 
right now...).

All the best,

Steve.




More information about the Qt-interest-old mailing list