Results 1 to 8 of 8

Thread: how can i draw rectanglar percentage?

  1. #1
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    1

    Default how can i draw rectanglar percentage?

    image.png
    I'd like to create a widget that present a percentage like above.
    So, I use QPainterPath, and try to do that with "addRect" and "arcTo". But I can't get the willing result.
    image2.png
    Maybe I'm not good at order of drawing, so I hope to someone tell me the way.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: how can i draw rectanglar percentage?

    Hope this helps.
    ProgressRect.jpg
    Qt Code:
    1. #include <QtGlobal>
    2. #include <QApplication>
    3.  
    4. #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
    5. #include <QtWidgets>
    6. #else
    7. #include <QtGui>
    8. #endif
    9.  
    10. #include "windows.h"
    11.  
    12. class ProgressRect : public QProgressBar
    13. {
    14. Q_OBJECT
    15. public:
    16. explicit ProgressRect(QWidget * parent = 0) : QProgressBar(parent) { }
    17.  
    18. protected:
    19. void paintEvent(QPaintEvent * event)
    20. {
    21. QPainter painter(this);
    22. int w = 0;
    23. int h = 0;
    24.  
    25. QPen pen = painter.pen();
    26. pen.setWidth(1);
    27. pen.setColor(QColor(Qt::red));
    28.  
    29. QBrush brush = painter.brush();
    30. brush.setColor(QColor(Qt::red));
    31. brush.setStyle(Qt::SolidPattern);
    32.  
    33. // Paint red background rectangle
    34. painter.setPen(pen);
    35. painter.setBrush(brush);
    36. w = pen.width() / 2;
    37. painter.drawRect(event->rect().adjusted(w, w, -w, -w));
    38.  
    39. // Paint green pie based on current value
    40. pen.setColor(Qt::green);
    41. brush.setColor(Qt::green);
    42. painter.setPen(pen);
    43. painter.setBrush(brush);
    44. h = event->rect().size().height();
    45. w = event->rect().size().width();
    46. w = qSqrt(h * h + w * w);
    47. QRect rect(0, 0, w, w);
    48. rect.moveCenter(event->rect().center());
    49. if(invertedAppearance())
    50. painter.drawPie(rect, 90 * 16, value() * 360 * 16 / 100);
    51. else
    52. painter.drawPie(rect, 90 * 16, -value() * 360 * 16 / 100);
    53.  
    54. // Paint white rectangle to partially hide the pie
    55. pen.setColor(Qt::white);
    56. brush.setColor(Qt::white);
    57. painter.setPen(pen);
    58. painter.setBrush(brush);
    59. w = 20;
    60. if((event->rect().size().height() > w * 2) and (event->rect().size().width() > w * 2))
    61. painter.drawRect(event->rect().adjusted(w, w, -w, -w));
    62.  
    63. // Paint the value in text in the centre
    64. pen.setColor(QColor(Qt::black));
    65. painter.setPen(pen);
    66. painter.drawText(event->rect(), QString::number(value()), QTextOption(Qt::AlignCenter));
    67. }
    68. };
    69.  
    70. int main(int argc, char *argv[])
    71. {
    72. QApplication app(argc, argv);
    73.  
    74. ProgressRect rect;
    75. rect.setMaximum(100);
    76. rect.setFixedSize(200, 200);
    77. rect.show();
    78.  
    79. for(int i = 1; i <= 100; i++)
    80. {
    81. rect.setValue(i);
    82. qApp->processEvents();
    83. Sleep(100);
    84. }
    85.  
    86. return 0;
    87. }
    88.  
    89. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    Last edited by Santosh Reddy; 21st August 2013 at 11:21.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how can i draw rectanglar percentage?

    Use clipping on the painter to obtain the result you want.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    1

    Default Re: how can i draw rectanglar percentage?

    Thank you for your comment. But I hope that the white region to be transparent.

  5. #5
    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: how can i draw rectanglar percentage?

    ... and incomplete requirements are why software projects are notorious for being late

    What are you expecting to see in the transparent region?

  6. #6
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    1

    Default Re: how can i draw rectanglar percentage?

    I try to show splashscreen widget on my listView (icon based). So, I want to let the WIDGET shown up on item in listView.
    I just want to see the item trough the transparent region.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how can i draw rectanglar percentage?

    Again -- use clipping to filter out what regions of the widget the draw operations modify.Then you can just draw a red rectangle and a green pie segment over it and you'll obtain the effect you want.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: how can i draw rectanglar percentage?

    Thank you for your comment. But I hope that the white region to be transparent.
    I try to show splashscreen widget on my listView (icon based). So, I want to let the WIDGET shown up on item in listView.
    I just want to see the item trough the transparent region.
    Ok then here it is again with transparent background.
    Qt Code:
    1. #include <QtGlobal>
    2. #include <QApplication>
    3.  
    4. #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
    5. #include <QtWidgets>
    6. #else
    7. #include <QtGui>
    8. #endif
    9.  
    10. class ProgressRect : public QProgressBar
    11. {
    12. Q_OBJECT
    13. public:
    14. explicit ProgressRect(QWidget * parent = 0)
    15. : QProgressBar(parent)
    16. , mPenWidth(1)
    17. {
    18. setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    19. }
    20.  
    21. protected:
    22. void resizeEvent(QResizeEvent * event)
    23. {
    24. QProgressBar::resizeEvent(event);
    25.  
    26. int t = 20;
    27. int w = mPenWidth / 2;
    28.  
    29. QRect outRect;
    30. QRect inRect;
    31.  
    32. outRect.setSize(event->size());
    33. outRect.adjust(w, w, -w, -w);
    34.  
    35. if((event->size().height() > (t * 2)) and (event->size().width() > (t * 2)))
    36. inRect = outRect.adjusted(t, t, -t, -t);
    37.  
    38. // Set the clipping mask
    39. QRegion outRegion(outRect);
    40. QRegion inRegion(inRect);
    41. outRegion = outRegion.subtracted(inRegion);
    42. setMask(outRegion);
    43. }
    44.  
    45. void paintEvent(QPaintEvent * event)
    46. {
    47. QPainter painter(this);
    48. int w = 0;
    49. int h = 0;
    50.  
    51. QPen pen = painter.pen();
    52. pen.setWidth(mPenWidth);
    53. pen.setColor(QColor(Qt::red));
    54.  
    55. QBrush brush = painter.brush();
    56. brush.setColor(QColor(Qt::red));
    57. brush.setStyle(Qt::SolidPattern);
    58.  
    59. // Paint red background rectangle
    60. painter.setPen(pen);
    61. painter.setBrush(brush);
    62. w = pen.width() / 2;
    63. QRect outRect = event->rect().adjusted(w, w, -w, -w);
    64. painter.drawRect(outRect);
    65.  
    66. // Paint green pie based on current value
    67. pen.setColor(Qt::green);
    68. brush.setColor(Qt::green);
    69. painter.setPen(pen);
    70. painter.setBrush(brush);
    71. h = event->rect().size().height();
    72. w = event->rect().size().width();
    73. w = qSqrt(h * h + w * w);
    74. QRect rect(0, 0, w, w);
    75. rect.moveCenter(event->rect().center());
    76. if(invertedAppearance())
    77. painter.drawPie(rect, 90 * 16, value() * 360 * 16 / 100);
    78. else
    79. painter.drawPie(rect, 90 * 16, -value() * 360 * 16 / 100);
    80. }
    81.  
    82. private:
    83. const int mPenWidth;
    84. };
    85.  
    86. class Timer : public QObject
    87. {
    88. public:
    89. explicit Timer(QProgressBar * progress, QObject * parent = 0)
    90. : QObject(parent)
    91. , mProgress(progress)
    92. , mCount(0)
    93. {
    94. mProgress->setMaximum(100);
    95. mProgress->setValue(0);
    96. startTimer(100);
    97. }
    98.  
    99. protected:
    100. void timerEvent(QTimerEvent * event)
    101. {
    102. mCount++;
    103. mProgress->setValue(mCount);
    104.  
    105. if(mCount == 100)
    106. {
    107. killTimer(event->timerId());
    108. deleteLater();
    109. }
    110. }
    111.  
    112. private:
    113. QProgressBar * const mProgress;
    114. int mCount;
    115. };
    116.  
    117. int main(int argc, char *argv[])
    118. {
    119. QApplication app(argc, argv);
    120.  
    121. QWidget widget;
    122. QHBoxLayout * layout = new QHBoxLayout(&widget);
    123. ProgressRect * rect = new ProgressRect;
    124. new Timer(rect, rect);
    125. layout->addWidget(rect);
    126. widget.showMaximized();
    127. return app.exec();
    128. }
    129.  
    130. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  9. The following user says thank you to Santosh Reddy for this useful post:

    icapathos (23rd August 2013)

Similar Threads

  1. Replies: 0
    Last Post: 26th November 2012, 13:19
  2. Font size in percentage
    By pssss in forum Qt Programming
    Replies: 2
    Last Post: 9th September 2011, 16:27
  3. Replies: 10
    Last Post: 10th February 2011, 23:31
  4. QStyleSheet font-size as percentage?
    By dhalbert in forum Qt Programming
    Replies: 1
    Last Post: 26th June 2010, 06:00
  5. QGridLayout - specific widh-hight by percentage
    By elcuco in forum Qt Programming
    Replies: 5
    Last Post: 25th May 2010, 15:56

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.