62 lines
1.7 KiB
C++
62 lines
1.7 KiB
C++
#include <QWidget>
|
||
#include <QLabel>
|
||
#include <QLineEdit>
|
||
#include <QGridLayout>
|
||
#include <qpainter.h>
|
||
class PieWidget : public QWidget {
|
||
Q_OBJECT
|
||
public:
|
||
explicit PieWidget(QSize size,QWidget* parent = nullptr) : QWidget(parent) {
|
||
goodRate = 1.0; // Ĭ��100%��Ʒ��
|
||
hasData = true;
|
||
this->setFixedSize(size);
|
||
}
|
||
|
||
void setGoodRate(double rate) {
|
||
goodRate = qBound(0.0, rate, 1.0);
|
||
hasData = true;
|
||
update();
|
||
}
|
||
|
||
void setGoodRate(double rate, bool hasDataValue) {
|
||
goodRate = qBound(0.0, rate, 1.0);
|
||
hasData = hasDataValue;
|
||
update();
|
||
}
|
||
|
||
protected:
|
||
void paintEvent(QPaintEvent* event) override {
|
||
Q_UNUSED(event);
|
||
QPainter p(this);
|
||
p.setRenderHint(QPainter::Antialiasing);
|
||
|
||
int size = qMin(width(), height()) - 10;
|
||
QRect rect((width() - size) / 2, (height() - size) / 2, size, size);
|
||
|
||
if (!hasData) {
|
||
p.setBrush(QColor(90, 90, 90));
|
||
p.setPen(Qt::NoPen);
|
||
p.drawEllipse(rect);
|
||
} else {
|
||
// ������Ʒ��ɫ����
|
||
p.setBrush(Qt::green);
|
||
p.setPen(Qt::NoPen);
|
||
int start = 1440;
|
||
p.drawPie(rect, start, int(goodRate * 360 * 16));
|
||
|
||
// ����ʣ�ಿ�֣���ѡ�������������Ҫ��ʾ��
|
||
p.setBrush(Qt::red);
|
||
p.drawPie(rect, start, int(-(1 - goodRate) * 360 * 16));
|
||
|
||
// ���ư�ɫ�ָ���
|
||
p.setPen(QPen(Qt::white, 2));
|
||
p.drawLine(rect.center(),
|
||
QPoint(rect.center().x(), rect.top()));
|
||
}
|
||
}
|
||
|
||
private:
|
||
double goodRate;
|
||
bool hasData;
|
||
};
|
||
#pragma once |