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. Specifying Color for progress bar in QProgressBar
Forum Updated to NodeBB v4.3 + New Features

Specifying Color for progress bar in QProgressBar

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 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.
  • O Offline
    O Offline
    Omni_Philm
    wrote on 14 Aug 2020, 15:41 last edited by
    #1

    Hello all,

    I am trying to find a way to change the color of the progress bar in QProgressBar. I tried this code snippet here:

    QPalette p = this->palette();
    p.setColor(QPalette::Highlight, QColor(Qt::green));
    this->setPalette(p);
    

    However, this did not work.

    I would need to change the colors dynamically so I wouldn't like to use stylesheets. I feel that I need to override the paint function but I am not sure.

    I will consider stylesheets as a last resort.

    P 1 Reply Last reply 14 Aug 2020, 15:59
    0
    • O Omni_Philm
      14 Aug 2020, 15:41

      Hello all,

      I am trying to find a way to change the color of the progress bar in QProgressBar. I tried this code snippet here:

      QPalette p = this->palette();
      p.setColor(QPalette::Highlight, QColor(Qt::green));
      this->setPalette(p);
      

      However, this did not work.

      I would need to change the colors dynamically so I wouldn't like to use stylesheets. I feel that I need to override the paint function but I am not sure.

      I will consider stylesheets as a last resort.

      P Offline
      P Offline
      Pl45m4
      wrote on 14 Aug 2020, 15:59 last edited by
      #2

      @Omni_Philm said in Specifying Color for progress bar in QProgressBar:

      I would need to change the colors dynamically so I wouldn't like to use stylesheets

      Stylesheet can be dynamic too.

      QColor barColor = QColor(255, 0, 0);
      
      QString style = QString("QProgressBar::chunk {
          background-color: %1;
          width: 20px;
      }").arg(barColor.name());
      
      this->setStylesheet(style);
      

      https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qprogressbar


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      O 1 Reply Last reply 15 Aug 2020, 22:40
      1
      • B Offline
        B Offline
        Bonnie
        wrote on 14 Aug 2020, 16:12 last edited by Bonnie
        #3

        If you override the paint function (paintEvent), then you'll need to paint the whole progress bar yourself. You don't want to do that...
        You know, sometimes the default QStyle don't use QPalette to get the color.
        In this situation, stylesheet is your best choice. Or you may consider changing to fusion style, which is not very "native" though.

        1 Reply Last reply
        1
        • P Pl45m4
          14 Aug 2020, 15:59

          @Omni_Philm said in Specifying Color for progress bar in QProgressBar:

          I would need to change the colors dynamically so I wouldn't like to use stylesheets

          Stylesheet can be dynamic too.

          QColor barColor = QColor(255, 0, 0);
          
          QString style = QString("QProgressBar::chunk {
              background-color: %1;
              width: 20px;
          }").arg(barColor.name());
          
          this->setStylesheet(style);
          

          https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qprogressbar

          O Offline
          O Offline
          Omni_Philm
          wrote on 15 Aug 2020, 22:40 last edited by Omni_Philm
          #4

          @Pl45m4 said in Specifying Color for progress bar in QProgressBar:

          @Omni_Philm said in Specifying Color for progress bar in QProgressBar:

          I would need to change the colors dynamically so I wouldn't like to use stylesheets

          Stylesheet can be dynamic too.

          QColor barColor = QColor(255, 0, 0);
          
          QString style = QString("QProgressBar::chunk {
              background-color: %1;
              width: 20px;
          }").arg(barColor.name());
          
          this->setStylesheet(style);
          

          https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qprogressbar

          Thanks however, each time I get into using stylesheets, there are always additional issues that pop up and I have to reformat the look of everything.

          I liked the the way the progress bar looked with the rounded corners. Now it is just a rectangle.

          Edit:
          Nevermind, I got it working!

          Edit 2:
          Ok, I got it mostly working. I am trying to change the width of the chunk and it seems that width: 20px is breaking it

          1 Reply Last reply
          0

          1/4

          14 Aug 2020, 15:41

          • Login

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