Results 1 to 8 of 8

Thread: how can i draw rectanglar percentage?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    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.

  2. 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, 14:19
  2. Font size in percentage
    By pssss in forum Qt Programming
    Replies: 2
    Last Post: 9th September 2011, 17:27
  3. Replies: 10
    Last Post: 11th February 2011, 00:31
  4. QStyleSheet font-size as percentage?
    By dhalbert in forum Qt Programming
    Replies: 1
    Last Post: 26th June 2010, 07:00
  5. QGridLayout - specific widh-hight by percentage
    By elcuco in forum Qt Programming
    Replies: 5
    Last Post: 25th May 2010, 16: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
  •  
Qt is a trademark of The Qt Company.