Results 1 to 4 of 4

Thread: Changing colors of QProgressBar

  1. #1
    Join Date
    May 2009
    Posts
    75
    Thanks
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Changing colors of QProgressBar

    Hi,

    I need to change the color of a progress bar depending on its value.

    I tried in some ways with poor results.

    Qt Code:
    1. QPalette pal = ui->progressBar->palette();
    2. pal.setColor(QPalette::Normal, QPalette::Base, QColor("red"));
    3. //pal.setColor(QPalette::Highlight, Qt::red);
    4. //pal.setColor(QPalette::Background, Qt::red);
    5. ui->progressBar->setPalette(pal);
    To copy to clipboard, switch view to plain text mode 

    I also tried to change stylesheet from QT Creator, but nothing happens.

    where am I wrong?

    I'm using Qt 4.7, QT Creator 2.0.1, Ubuntu 10.10 64 bit.

    thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Changing colors of QProgressBar

    You have to alter QPalette::Highlight, but it also depends on your used window style.

    That works
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argC, char **argV)
    4. {
    5. QApplication::setStyle(new QWindowsStyle);
    6. QApplication myApp(argC, argV);
    7.  
    8. bar.setRange(0,100);
    9. bar.setValue(50);
    10. QPalette p = bar.palette();
    11. p.setColor(QPalette::Highlight, Qt::red);
    12. bar.setPalette(p);
    13. bar.show();
    14.  
    15. return myApp.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 

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

    polrus (5th April 2017)

  4. #3
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Changing colors of QProgressBar

    First of all palette works as a difference property. If something is missing it is taken from parent if there is no parent it is taken from QApplication (which always has fully resolved palette), so you don't have to fetch anything (and you shouldn't set full palette).

    My question is how you are do that? When this code is invoked?

    I would do new QObject for that:
    Qt Code:
    1. class MyPaletteChanger : public QObject {
    2. Q_OBJECT
    3.  
    4. ....
    5.  
    6. public slots:
    7. void setColorProgress(int value) {
    8. QColor color(someFunciton(value));
    9. if (color != lastColor) {
    10. lastColor = color;
    11. QPalette palette;
    12. palette.setBrush(QPalette::Highlight, QBrush(color));
    13. emit newPallete(palette);
    14. }
    15. }
    16.  
    17. signals:
    18. void newPallete(const QPalette &palette);
    19. }
    To copy to clipboard, switch view to plain text mode 
    Then connect valueChange of progress bar with setColorProgress and newPallete with setPalette of progress bar.

  5. The following user says thank you to MarekR22 for this useful post:

    Lykurg (17th January 2012)

  6. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Changing colors of QProgressBar

    Quote Originally Posted by MarekR22 View Post
    First of all palette works as a difference property.
    Nice, I wasn't aware of that. Thanks.

Similar Threads

  1. Replies: 2
    Last Post: 27th February 2011, 23:45
  2. Changing form colors and adding skins
    By srohit24 in forum Qt Programming
    Replies: 9
    Last Post: 28th October 2009, 15:35
  3. Replies: 2
    Last Post: 10th August 2009, 09:45
  4. Changing colors and textures?
    By Dumbledore in forum Qt Tools
    Replies: 3
    Last Post: 12th October 2007, 18:18
  5. Changing Progress Bar Colors
    By bpetty in forum Newbie
    Replies: 1
    Last Post: 11th August 2006, 17:29

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.