Results 1 to 3 of 3

Thread: QtItemDelegate for progress bars

  1. #1
    Join Date
    Oct 2008
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Exclamation QtItemDelegate for progress bars

    hi,
    i have a delegate used to draw progress bars in a tree widget.My application as top level items as well as child items.The no.of these items will not be know at compile time. My delegate has the following issues:

    1. Draws an incomplete progress bar for top level items..i.e the bar has no boundaries..i can only see the left boundary..the remaining 3 sides of the rect are missing.

    2.does not draw progress bar for child items..the text however changes.

    The code is included as an attachment.i hope some one can point out where I'm going wrong.i guess it is option.rect that causing a problem but I'm not sure.

    thanks!!
    Attached Files Attached Files

  2. #2
    Join Date
    Oct 2008
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QtItemDelegate for progress bars

    hi..i was very impressed by the example given by jpn and his snapshot (attached). Somehow i am failing to emulate the same in my application:

    1. i dont see alternating row colors.

    2. The boundary of the progress bar is not clear.

    3. i would like to have the progress text beside the bar rather than within it.(like jpn's snapshot)

    i hope some one can help me accomplish this. My delegate is as follows:

    Qt Code:
    1. class JobViewDelegate : public QItemDelegate
    2. {
    3.  
    4.  
    5. public:
    6.  
    7. inline JobViewDelegate(MainWindow *mainWindow) : QItemDelegate(mainWindow){}
    8.  
    9. inline void paint(QPainter *painter,const QStyleOptionViewItem &option,const QModelIndex &index) const
    10. {
    11.  
    12. QProgressBar progressbar;
    13.  
    14. if (index.column() != 1) {
    15. QItemDelegate::paint(painter, option, index);
    16. return;
    17. }
    18.  
    19. QStyleOptionProgressBarV2 progressBarOption;
    20.  
    21. QRect rect = option.rect;
    22. QSize size(rect.width()*3/4,rect.height()*3/4);
    23. rect.setSize(size);
    24.  
    25. progressBarOption.state = QStyle::State_Enabled;
    26. progressBarOption.direction = QApplication::layoutDirection();
    27. progressBarOption.rect =rect;
    28. progressBarOption.fontMetrics = QApplication::fontMetrics();
    29.  
    30. QPalette pal = progressBarOption.palette;
    31. QColor col;
    32. col.setNamedColor("#05B8CC");
    33. pal.setColor(QPalette::Highlight,col);
    34. //pal.setColor(QPalette::HighlightedText,Qt::black);
    35. progressBarOption.palette = pal;
    36.  
    37. progressBarOption.type = QStyleOption::SO_ProgressBar;
    38. progressBarOption.version = 2 ;
    39.  
    40. progressBarOption.minimum = 0;
    41. progressBarOption.maximum = 100;
    42. progressBarOption.textAlignment = Qt::AlignCenter;
    43. progressBarOption.textVisible = true;
    44.  
    45. QModelIndex ind = index.parent();
    46.  
    47. int progress = index.data(Qt::DisplayRole).toInt();
    48. // Set the progress and text values of the style option.
    49.  
    50. progressBarOption.progress = progress < 0 ? 0 : progress;
    51. progressBarOption.text = QString("%1%").arg(progressBarOption.progress);
    52.  
    53. // Draw the progress bar onto the view.
    54. QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter,0);
    55. }
    56. };
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

  3. #3
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QtItemDelegate for progress bars

    a) what do you see? give us a screenshot, please.
    b) probably you do not see the alternating background because the progress bar fills all of the background?
    c)
    3. i would like to have the progress text beside the bar rather than within it.(like jpn's snapshot)
    set progressBarOption.textVisible = false; and draw the text besides it in your paint() method. (maybe there are better ways?)

    HTH

    side note:
    * the progressbar variable in paint() is unused
    * paint() can not be inlined as it is virtual. (even if it could be, I doubt it should.)

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.