Results 1 to 3 of 3

Thread: QtItemDelegate for progress bars

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #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

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
  •  
Qt is a trademark of The Qt Company.