Results 1 to 4 of 4

Thread: Cannot make my progressbar change of color with specific progress value

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Cannot make my progressbar change of color with specific progress value

    I wanted to try to render QProgressBar widgets inside cells of a table and I wanted the progress bars to change color if the progress value was, for example, greater than 50 (to represent a warning). To test it I created a very small project using a QTableView, a QStandardItemModel and a QProgressBar delegate to render a single table cell.

    I tried different ways and none is working, the progress bar color is always green. Below the latest version of my code subclassing QProxyStyle.

    Qt Code:
    1. #include <QApplication>
    2. #include <QStyledItemDelegate>
    3. #include <QPainter>
    4. #include <QTableView>
    5. #include <QStandardItemModel>
    6. #include <QHeaderView>
    7. #include <QPushButton>
    8. #include <QVBoxLayout>
    9. #include <QProxyStyle>
    10.  
    11. class ProgressBarStyle : public QProxyStyle
    12. {
    13. public:
    14. ProgressBarStyle(QStyle *style = nullptr) : QProxyStyle(style) {
    15. qDebug() << "ProgressBarStyle constructor called";
    16. }
    17.  
    18. void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const override
    19. {
    20. if (element == CE_ProgressBar)
    21. {
    22. const QStyleOptionProgressBar *progressBarOption = qstyleoption_cast<const QStyleOptionProgressBar *>(option);
    23. if (progressBarOption && progressBarOption->progress > 50)
    24. {
    25. QStyleOptionProgressBar progressBarOptionCopy(*progressBarOption);
    26. progressBarOptionCopy.palette.setColor(QPalette::Highlight, Qt::red);
    27.  
    28. QProxyStyle::drawControl(element, &progressBarOptionCopy, painter, widget);
    29. return;
    30. }
    31. }
    32.  
    33. QProxyStyle::drawControl(element, option, painter, widget);
    34. }
    35. };
    36.  
    37. class ProgressBarDelegate : public QStyledItemDelegate
    38. {
    39. public:
    40. ProgressBarDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {}
    41.  
    42. void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
    43. {
    44. int progress = index.data().toInt();
    45. QStyleOptionProgressBar progressBarOption;
    46. progressBarOption.rect = option.rect;
    47. progressBarOption.minimum = 0;
    48. progressBarOption.maximum = 100;
    49. progressBarOption.progress = progress;
    50. progressBarOption.text = QString::number(progress) + "%";
    51. progressBarOption.textVisible = true;
    52.  
    53. QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter);
    54. }
    55. };
    56.  
    57. int main(int argc, char *argv[])
    58. {
    59. QApplication app(argc, argv);
    60. QWidget window;
    61. QVBoxLayout layout(&window);
    62. QTableView tableView;
    63. layout.addWidget(&tableView);
    64. QPushButton button("Update Progress");
    65. layout.addWidget(&button);
    66. QStandardItemModel model(5, 3); // 5 rows and 3 columns
    67. tableView.setModel(&model);
    68.  
    69. // Set up data
    70. for (int row = 0; row < model.rowCount(); ++row) {
    71. for (int col = 0; col < model.columnCount(); ++col) {
    72. model.setData(model.index(row, col), (row + col) * 2);
    73. }
    74. }
    75.  
    76. // Set up delegate
    77. ProgressBarDelegate delegate;
    78. tableView.setItemDelegate(&delegate);
    79.  
    80. // Set up style
    81. ProgressBarStyle *style = new ProgressBarStyle;
    82. tableView.setStyle(style);
    83. app.setStyle(style);
    84.  
    85. // Connect button to update progress
    86. QObject::connect(&button, &QPushButton::clicked, [&model, &tableView]() {
    87. for (int row = 0; row < model.rowCount(); ++row) {
    88. for (int col = 0; col < model.columnCount(); ++col) {
    89. int progress = model.data(model.index(row, col)).toInt();
    90. progress = (progress + 10) % 110;
    91. model.setData(model.index(row, col), progress);
    92. }
    93. }
    94. tableView.viewport()->update();
    95. });
    96.  
    97. tableView.horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    98. window.show();
    99. return app.exec();
    100. }
    To copy to clipboard, switch view to plain text mode 

    Any advise please? I am using Qt 6.5.0
    Franco Amato

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    502
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Cannot make my progressbar change of color with specific progress value

    Hi, have you tried CE_ProgressBarContents instead of using CE_ProgressBar?

    I remember having similar problems in the past, and only getting it to work using a style sheet for background-color in QProgressBar::chunk.

    Ginsengelf

  3. The following user says thank you to Ginsengelf for this useful post:

    franco.amato (12th May 2023)

  4. #3
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Cannot make my progressbar change of color with specific progress value

    Unfortunately changind to CE_ProgressBarContents didn't work
    Franco Amato

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Cannot make my progressbar change of color with specific progress value

    Maybe try a different color role instead of Highlight?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. The following user says thank you to d_stranz for this useful post:

    franco.amato (13th May 2023)

Similar Threads

  1. Replies: 4
    Last Post: 3rd July 2014, 15:52
  2. troubles with changing progressBar color
    By dilidpan in forum Qt Programming
    Replies: 13
    Last Post: 15th September 2012, 03:58
  3. Selection color for progressbar in treeview
    By Jennie Bystrom in forum Qt Programming
    Replies: 1
    Last Post: 11th February 2011, 18:32
  4. Replies: 7
    Last Post: 1st March 2010, 18:49

Tags for this Thread

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.