I have created a class name QSector to draw a sector like the following : http://www.philadelphia-reflections.com/images/GDP_Composition_By_Sector_Graph.jpg
QValue is a class with 2 attributes Label (String) and Value (double) the QSector class is composed of 4 attributes and is inherited from QWidget
- QPainter (to draw things)
- QRect (size and position)
- QVector (store all the data of the sector, values and labels)
- Double total (compute the total of QValue's Values (to compute percentage later))
here is the code ::
// <c++>
class QValue
{
public:
QValue(QString a = "", double b = 0): f_label(a), f_value(b) {}
double value() { return f_value; }
QString label() { return f_label; }
void setValue(double a) { f_value = a; }
void setLabel(QString a) { f_label = a; }
void set(QString a, double b) { f_label = a; f_value = b; }
private:
QString f_label;
double f_value;
};
class QSector : public QWidget
{
Q_OBJECT
public:
QSector(int width, int height, QWidget *parent = 0)
: QWidget(parent), f_total(0)
{
f_rect = new QRect(1, 1, width - 3 , height - 3);
this->resize(width, height);
f_paint = new QPainter;
}
void paintEvent(QPaintEvent* event = 0)
{
QBrush brush;
brush.setColor(QColor(25, 25, 255));
brush.setStyle(Qt::SolidPattern);
int startAngle = 0;
int spanAngle = 0;
f_paint->begin(this);
for (int i = 0; i < f_data.size(); i++)
{
int c = ( i * 150) % 255;
brush.setColor(QColor(c, 25, 255));
f_paint->setBrush(brush);
// 5760 = 360 * 16 = 100%; total = 100% => Value * 5760 / total = span angle
spanAngle = (5760 * f_data[i].value()) / f_total;
f_paint->drawPie(*f_rect, startAngle, spanAngle);
startAngle = spanAngle;
}
f_paint->end();
}
void add(QString Label, double Value)
{
f_data.push_back(QValue(Label, Value));
f_total = f_total + Value;
update(); // => paintEvent();
}
void add(QValue a)
{
f_data.push_back(a);
f_total = f_total + a.value();
update(); // => paintEvent();
}
signals:
public slots:
private:
QPainter *f_paint;
QRect *f_rect;
QVector<QValue> f_data;
double f_total;
};
Everything Compile.
the problem comes when I do ::
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
int w = 300;
int h = 300;
QSector test(w, h);
for (int i = 0, n = 10; i < n; i++)
test.add("", 10);
test.show();
return app.exec();
}
the program only draws the first 2 parts and stops (the sector should have 10 equal parts it has only 2)
I don't understand why it stops drawing. if I cut the sector in two it works fine but starting with 3 it only draws 2 parts
summary of the problem : https://i.stack.imgur.com/5jyi6.png (image 1, sector divided in 1) (image 2, sector divided in 2) (image 3, sector divided in 3) (image 4, sector divided in 10)
I suspect that
should be
It looks like you're just repainting over the same pie slice over and over at the same angle.