Results 1 to 3 of 3

Thread: QAbstractItemDelegate and QLinearGradient

  1. #1
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QAbstractItemDelegate and QLinearGradient

    Bonjour,

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

    Qt Code:
    1. void Delegate:: paint (QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
    2. {
    3. int value = index.model()->data( index, Qt::DisplayRole ).toInt();
    4. double percent = double(value)/100.0;
    5.  
    6. painter->save();
    7.  
    8. QLinearGradient gradient( QPoint ( option.rect.x(), option.rect.y() ), QPoint ( option.rect.x() , option.rect.height() ) );
    9. gradient.setColorAt(0, QColor(42, 142,220) );
    10. gradient.setColorAt(0.5, QColor(11, 90,163) );
    11. gradient.setColorAt(1, QColor(4, 24,40) );
    12. QBrush brush(gradient);
    13. painter->setBrush(brush);
    14. painter->drawRect(option.rect.x(), option.rect.y() , int(percent*(option.rect.width())), option.rect.height() );
    15.  
    16. painter->restore();
    17.  
    18. }
    To copy to clipboard, switch view to plain text mode 

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

    Any ideas ?

    Regards

  2. #2
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QAbstractItemDelegate and QLinearGradient

    The view is populated this way:

    Qt Code:
    1. QTableView table;
    2. QStandardItemModel model(totalFile, 2);
    3. Delegate delegate;
    4. table.setItemDelegateForColumn(1, &delegate)
    5.  
    6. for(int i =0; i<totalFile; ++i)
    7. {
    8. QStandardItem* item = new QStandardItem( QString(" %1 files") . arg(fileCount) );
    9. model.setItem(r, 0, item);
    10. model.setItem(r, 1, new QStandardItem(QString::number((value)));
    11. }
    12.  
    13. table.setModel(&model);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QAbstractItemDelegate and QLinearGradient

    It's was just a problem of coordonates in QlinearGradient.

Similar Threads

  1. about QAbstractItemDelegate
    By drizzlefsh in forum Qt Programming
    Replies: 4
    Last Post: 11th September 2009, 07:01
  2. Regarding qlineargradient
    By jjbabu in forum Qt Programming
    Replies: 6
    Last Post: 15th April 2009, 09:21
  3. Related QLinearGradient
    By ashishsaryar in forum Qt Programming
    Replies: 5
    Last Post: 26th May 2008, 10:09
  4. QLinearGradient question
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 7th March 2008, 16:52
  5. Use of QAbstractItemDelegate::closeEditor
    By jml in forum Qt Programming
    Replies: 3
    Last Post: 3rd August 2007, 22:55

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.