Results 1 to 6 of 6

Thread: QProgressBar with % in decimal

  1. #1
    Join Date
    Jan 2020
    Posts
    47
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default QProgressBar with % in decimal

    What is the best/easiest way to modify QProgressBar to show the %-value as a decimal value? I presume somebody will have done this before, but I have not been able to find it.

  2. #2
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QProgressBar with % in decimal


  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QProgressBar with % in decimal

    Not sure I see how setFormat() can be used to get a percentage value that is not integer. The placeholder "%p" gets an integer as a string. You could get a string like "9 of 37" from "%v of %m" but you cannot compute with these placeholders.

    You could subclass QProgressBar and override QProgressBar::text():
    Qt Code:
    1. #ifndef FRACTIONALPROGRESSBAR_H
    2. #define FRACTIONALPROGRESSBAR_H
    3.  
    4. #include <QProgressBar>
    5.  
    6. class FractionalProgressBar : public QProgressBar
    7. {
    8. public:
    9. FractionalProgressBar(QWidget *p = nullptr);
    10.  
    11. // QProgressBar interface
    12. public:
    13. virtual QString text() const Q_DECL_OVERRIDE;
    14. };
    15.  
    16. #endif // FRACTIONALPROGRESSBAR_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "fractionalprogressbar.h"
    2.  
    3. FractionalProgressBar::FractionalProgressBar(QWidget *p):
    4. {
    5. }
    6.  
    7.  
    8. QString FractionalProgressBar::text() const
    9. {
    10. if ( minimum() == maximum() ) // divide by zero guard
    11. return QString();
    12.  
    13. double percent = 100.0 * (value() - minimum()) / (maximum() - minimum());
    14. return QString("%1%").arg(percent, 0, 'f', 1);
    15. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2020
    Posts
    47
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QProgressBar with % in decimal

    But the setValue() method converts the input argument to int, so the fractional part of the value is lost.

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

    Default Re: QProgressBar with % in decimal

    But the setValue() method converts the input argument to int, so the fractional part of the value is lost.
    Change the scale of the progress bar range. If you now have it set to range from 0 - 100, and you want one decimal place, change it to a range of 0 - 1000. Use ChrisW67's method to format the text by converting the value to a float, dividing by 10, and use the result to set a float formatted string.

    You will also have to change the code that sets the value to multiply the number passed to setValue() by 10.
    <=== 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. #6
    Join Date
    Jan 2020
    Posts
    47
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QProgressBar with % in decimal

    Works perfectly! Thanks. I tried to find a good excuse for not thinking of this myself, but couldn't find one ...

Similar Threads

  1. Qt and decimal number
    By Imhotep in forum Newbie
    Replies: 2
    Last Post: 19th September 2016, 17:41
  2. comma as decimal seperator
    By Malisha100ka in forum Qt Programming
    Replies: 2
    Last Post: 26th August 2015, 22:35
  3. Decimal to Binary conversion
    By anh5kor in forum Newbie
    Replies: 1
    Last Post: 5th February 2015, 13:16
  4. QTableWidget and Decimal places
    By pshah.mumbai in forum Qt Programming
    Replies: 3
    Last Post: 17th August 2008, 09:30
  5. converting the value from hexadecimal to decimal value
    By jjbabu in forum Qt Programming
    Replies: 2
    Last Post: 16th November 2007, 11:24

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.