Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Changing the fore color of a progress bar
Forum Updated to NodeBB v4.3 + New Features

Changing the fore color of a progress bar

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 4.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gRicky
    wrote on last edited by
    #1

    Hi All,

    Simply, I'd like to know if there is a "style" color which I can change to change the fore-color on a progress bar. I'm able to change the background-color of my progress bar but event though I've changed the also the fore-core it didn't change.
    For "fore-color" I mean the color of the of the colored progressive bar.

    I hope it is clear what I need to know.....

    Thank you in advance.

    1 Reply Last reply
    0
    • jazzycamelJ Offline
      jazzycamelJ Offline
      jazzycamel
      wrote on last edited by
      #2

      I think what your looking for is "QProgressBar::chunk". The following example shows a QProgressBar that changes colour every 20% of its progress:

      @
      #include <QtGui>

      class ProgressBar : public QProgressBar {
      public:
      ProgressBar(QWidget *parent = 0) : QProgressBar(parent) {
      style="QProgressBar {"
      "text-align: center;"
      "}"
      "QProgressBar::chunk {"
      "background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 %1, stop:1 %2);"
      "text-align: center;"
      "margin: 2px 1px 1p 2px;"
      "}";
      setRange(0,100);
      }

      public slots:
      void setValue(int value){
      QString c,ac;
      switch(value/20){
      case 0:
      c="#ff0000";
      break;
      case 1:
      c="#00ff00";
      break;
      case 2:
      c="#0000ff";
      break;
      case 3:
      c="#ffff00";
      break;
      case 4:
      c="#ff00ff";
      break;
      case 5:
      c="#000000";
      break;
      }
      ac=adjust(c,80);
      setStyleSheet(style.arg(ac).arg(c));
      QProgressBar::setValue(value);
      }

      private:
      QString adjust(QString color, int factor){
      QColor *c=new QColor(color);
      c->setHsl(c->hslHue(),
      c->hslSaturation(),
      qMin(c->lightness()+factor, 255));
      return c->name();
      }

      QString style;
      

      };

      class Widget : public QWidget {
      public:
      Widget(QWidget *parent = 0) : QWidget(parent), value(0) {
      QVBoxLayout *l=new QVBoxLayout(this);
      progress=new ProgressBar(this);
      progress->setValue(value);
      l->addWidget(progress);
      startTimer(100);
      }

      protected:
      void timerEvent(QTimerEvent *){
      value=value<100 ? value+1 : 0;
      progress->setValue(value);
      }

      private:
      ProgressBar *progress;
      int value;
      };

      int main(int argc, char *argv[])
      {
      QApplication a(argc, argv);
      Widget w;
      w.show();
      return a.exec();
      }
      @

      The important bits are lines 9 thru 13. Obviously there is no need to subclass QProgressBar just to do this. Also, I use a linear gradient as the background to the chunk as I think a flat colour looks pretty awful, but that's up to you ;o)

      Hope this helps.

      For the avoidance of doubt:

      1. All my code samples (C++ or Python) are tested before posting
      2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
      1 Reply Last reply
      0
      • G Offline
        G Offline
        gRicky
        wrote on last edited by
        #3

        Thank you, it was very helpful.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved