#include <QtGlobal>
#include <QApplication>
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
#include <QtWidgets>
#else
#include <QtGui>
#endif
{
Q_OBJECT
public:
explicit ProgressRect
(QWidget * parent
= 0) , mPenWidth(1)
{
}
protected:
{
int t = 20;
int w = mPenWidth / 2;
outRect.setSize(event->size());
outRect.adjust(w, w, -w, -w);
if((event->size().height() > (t * 2)) and (event->size().width() > (t * 2)))
inRect = outRect.adjusted(t, t, -t, -t);
// Set the clipping mask
outRegion = outRegion.subtracted(inRegion);
setMask(outRegion);
}
{
int w = 0;
int h = 0;
QPen pen
= painter.
pen();
pen.setWidth(mPenWidth);
pen.
setColor(QColor(Qt
::red));
QBrush brush
= painter.
brush();
brush.
setColor(QColor(Qt
::red));
brush.setStyle(Qt::SolidPattern);
// Paint red background rectangle
painter.setPen(pen);
painter.setBrush(brush);
w = pen.width() / 2;
QRect outRect
= event
->rect
().
adjusted(w, w,
-w,
-w
);
painter.drawRect(outRect);
// Paint green pie based on current value
pen.setColor(Qt::green);
brush.setColor(Qt::green);
painter.setPen(pen);
painter.setBrush(brush);
h = event->rect().size().height();
w = event->rect().size().width();
w = qSqrt(h * h + w * w);
rect.moveCenter(event->rect().center());
if(invertedAppearance())
painter.drawPie(rect, 90 * 16, value() * 360 * 16 / 100);
else
painter.drawPie(rect, 90 * 16, -value() * 360 * 16 / 100);
}
private:
const int mPenWidth;
};
{
public:
, mProgress(progress)
, mCount(0)
{
mProgress->setMaximum(100);
mProgress->setValue(0);
startTimer(100);
}
protected:
{
mCount++;
mProgress->setValue(mCount);
if(mCount == 100)
{
killTimer(event->timerId());
deleteLater();
}
}
private:
int mCount;
};
int main(int argc, char *argv[])
{
ProgressRect * rect = new ProgressRect;
new Timer(rect, rect);
layout->addWidget(rect);
widget.showMaximized();
return app.exec();
}
#include "main.moc"
#include <QtGlobal>
#include <QApplication>
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
#include <QtWidgets>
#else
#include <QtGui>
#endif
class ProgressRect : public QProgressBar
{
Q_OBJECT
public:
explicit ProgressRect(QWidget * parent = 0)
: QProgressBar(parent)
, mPenWidth(1)
{
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
}
protected:
void resizeEvent(QResizeEvent * event)
{
QProgressBar::resizeEvent(event);
int t = 20;
int w = mPenWidth / 2;
QRect outRect;
QRect inRect;
outRect.setSize(event->size());
outRect.adjust(w, w, -w, -w);
if((event->size().height() > (t * 2)) and (event->size().width() > (t * 2)))
inRect = outRect.adjusted(t, t, -t, -t);
// Set the clipping mask
QRegion outRegion(outRect);
QRegion inRegion(inRect);
outRegion = outRegion.subtracted(inRegion);
setMask(outRegion);
}
void paintEvent(QPaintEvent * event)
{
QPainter painter(this);
int w = 0;
int h = 0;
QPen pen = painter.pen();
pen.setWidth(mPenWidth);
pen.setColor(QColor(Qt::red));
QBrush brush = painter.brush();
brush.setColor(QColor(Qt::red));
brush.setStyle(Qt::SolidPattern);
// Paint red background rectangle
painter.setPen(pen);
painter.setBrush(brush);
w = pen.width() / 2;
QRect outRect = event->rect().adjusted(w, w, -w, -w);
painter.drawRect(outRect);
// Paint green pie based on current value
pen.setColor(Qt::green);
brush.setColor(Qt::green);
painter.setPen(pen);
painter.setBrush(brush);
h = event->rect().size().height();
w = event->rect().size().width();
w = qSqrt(h * h + w * w);
QRect rect(0, 0, w, w);
rect.moveCenter(event->rect().center());
if(invertedAppearance())
painter.drawPie(rect, 90 * 16, value() * 360 * 16 / 100);
else
painter.drawPie(rect, 90 * 16, -value() * 360 * 16 / 100);
}
private:
const int mPenWidth;
};
class Timer : public QObject
{
public:
explicit Timer(QProgressBar * progress, QObject * parent = 0)
: QObject(parent)
, mProgress(progress)
, mCount(0)
{
mProgress->setMaximum(100);
mProgress->setValue(0);
startTimer(100);
}
protected:
void timerEvent(QTimerEvent * event)
{
mCount++;
mProgress->setValue(mCount);
if(mCount == 100)
{
killTimer(event->timerId());
deleteLater();
}
}
private:
QProgressBar * const mProgress;
int mCount;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget widget;
QHBoxLayout * layout = new QHBoxLayout(&widget);
ProgressRect * rect = new ProgressRect;
new Timer(rect, rect);
layout->addWidget(rect);
widget.showMaximized();
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks