Results 1 to 1 of 1

Thread: Circle Progress Bar. Need help to improve

  1. #1
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Lightbulb Circle Progress Bar. Need help to improve

    Hi everyone,

    I am trying to make a circle progress bar. I would like to add it into my future mp3 music player. Here is my first shot :

    circleProgressBar.jpg

    It's not so bad, heh? Well, actually, it's kind of ugly code . How can I reuse "right" QProgressBar elements, like in QStyleOptionProgressBarV2 ? It would be great to improve it to fit for all platforms.

    Need to be done:
    • Reuse code from QStyleOptionProgressBarV2 ?
    • When drawing path, there are some errors using "groovePath.simplified();"
    • Add a square layout to keep ratio when resizing
    • Deploy everywhere!


    Qt Code:
    1. #ifndef CIRCLEPROGRESSBAR_H
    2. #define CIRCLEPROGRESSBAR_H
    3.  
    4. #include <QProgressBar>
    5.  
    6. class CircleProgressBar : public QProgressBar
    7. {
    8. Q_OBJECT
    9.  
    10. private:
    11. bool transparentCenter;
    12. qreal startAngle;
    13.  
    14. public:
    15. CircleProgressBar(QWidget *parent = 0);
    16.  
    17. inline void setTransparentCenter(bool value) { transparentCenter = value; }
    18. inline void setStartAngle(qreal startAngle) { this->startAngle = startAngle; }
    19.  
    20. protected:
    21. void paintEvent(QPaintEvent *event);
    22. };
    23.  
    24. #endif // CIRCLEPROGRESSBAR_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "circleprogressbar.h"
    2.  
    3. #include <QStylePainter>
    4. #include <QDebug>
    5.  
    6. CircleProgressBar::CircleProgressBar(QWidget *parent) :
    7. QProgressBar(parent), transparentCenter(false), startAngle(90.0)
    8. {
    9. this->setMinimumWidth(400);
    10. this->setMinimumHeight(400);
    11. }
    12.  
    13. void CircleProgressBar::paintEvent(QPaintEvent * /*event*/)
    14. {
    15. //TODO
    16. static double coefOuter = 0.6;
    17. static double coefInner = 0.4;
    18. static double penSize = 1.0;
    19. static QColor borderColor(178, 178, 178);
    20. static QColor grooveColor(202, 202, 202);
    21. static QColor chunkColor(0, 211, 40);
    22.  
    23. QPainter painter(this);
    24. painter.setRenderHint(QPainter::Antialiasing);
    25.  
    26. QRectF outerRect(rect().x()*coefOuter, rect().y()*coefOuter, rect().width()*coefOuter, rect().height()*coefOuter);
    27. QRectF innerRect(rect().x()*coefInner, rect().y()*coefInner, rect().width()*coefInner, rect().height()*coefInner);
    28. outerRect.moveCenter(rect().center());
    29. innerRect.moveCenter(rect().center());
    30.  
    31. if (isTextVisible()) {
    32. painter.save();
    33. }
    34.  
    35. QPainterPath borderInAndOut;
    36. borderInAndOut.addEllipse(rect().center(), rect().width()/2*coefOuter, rect().height()/2*coefOuter);
    37. borderInAndOut.addEllipse(rect().center(), rect().width()/2*coefInner, rect().height()/2*coefInner);
    38.  
    39. QPen borderPen(borderColor, penSize);
    40. painter.setPen(borderPen);
    41. painter.setBrush(grooveColor);
    42. painter.drawPath(borderInAndOut);
    43.  
    44. if (value() > 0) {
    45. QPainterPath groovePath(rect().center());
    46. qreal converterAngle = 3.6*value();
    47. groovePath.arcTo(outerRect, startAngle, -converterAngle);
    48. groovePath.moveTo(rect().center());
    49. groovePath.arcTo(innerRect, startAngle, -converterAngle);
    50. groovePath = groovePath.simplified();
    51. painter.setPen(Qt::NoPen);
    52. painter.setBrush(chunkColor);
    53. painter.drawPath(groovePath);
    54. }
    55.  
    56. if (!transparentCenter) {
    57. QPainterPath painterPathCenter;
    58. painterPathCenter.addEllipse(rect().center(), rect().width()/2*coefInner - penSize/2, rect().height()/2*coefInner - penSize/2);
    59. painter.setPen(Qt::NoPen);
    60. painter.setBrush(QColor(Qt::white));
    61. painter.drawPath(painterPathCenter);
    62. }
    63.  
    64. if (isTextVisible()) {
    65. painter.restore();
    66. QString val = QString::number(value()).append("%");
    67. //QStyleOptionProgressBarV2 styleOption;
    68. //style()->drawControl(QStyle::CE_ProgressBarGroove, &styleOption, &painter, this);
    69. //style()->drawPrimitive(QStyle::PE_FrameStatusBar, &styleOption, &painter, this);
    70. style()->drawItemText(&painter, rect(), Qt::AlignCenter, palette(), true, val);
    71. }
    72. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images
    Attached Files Attached Files

Similar Threads

  1. how can i improve CPU utilization
    By Askar in forum Qt Programming
    Replies: 3
    Last Post: 22nd February 2010, 16:42
  2. How to improve the font style?
    By aaron in forum Qt Programming
    Replies: 1
    Last Post: 19th January 2010, 07:58
  3. Improve performance QPainter
    By Programm3r in forum Qt Programming
    Replies: 2
    Last Post: 12th November 2009, 08:23
  4. Replies: 4
    Last Post: 11th March 2008, 12:44
  5. How to improve fonts in designer ?
    By probine in forum Qt Tools
    Replies: 3
    Last Post: 25th October 2006, 21:38

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.