Question : drawing bar chart

I need to render bar chart based on some different length of bars that saved in array, I have try to writ this code but all bars come with same length I think I have problem in loops but I don't lnow how can I solve it
include this question the function code and the snapshot of current result.Also , is there easy way to draw x and y axis .

many thanks
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
void line::paintEvent(QPaintEvent *c)
{
    //lines
    QPainter line(this);
    line.setRenderHint(QPainter::Antialiasing,true);
    line.setPen(QPen(Qt::black,2,Qt::SolidLine,Qt::RoundCap,Qt::MiterJoin));
    line.setBrush(QBrush(Qt::black,Qt::SolidPattern));
    line.drawLine(50,50,50,200); // Y axis
    line.drawLine(50,200,250,200);  // x axis





    // rectangle
    int a[5] ={20,60,120,30,200};    //length = a[]
    int x= 50.0  ;
    int y= 200 ;

    for(int i=0 ; i<5 ;i++)
    {
        int inc = 200/5;
        for(int j=0 ; j<200 ;j=j+inc)
        {

            QRectF rect((x+j),(y-a[i]),0.0,a[i]);
            line.setPen(QPen(Qt::green,8,Qt::SolidLine,Qt::RoundCap,Qt::MiterJoin));
           line.drawRect(rect);

        }
    }

}
Attachments:

Answer : drawing bar chart

First you do not need two loops.  Your rectangle should be defined as:

Left:       (i * inc) + x
Top:       y - a[i]
right:      Left + line width
Bottom : Y

So, get rid of the inner loop entirely...  The code I posted should give you an idea of what is needed.  I have never done QT programming.  Let me know if you need more explanation.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
void line::paintEvent(QPaintEvent *c)
{
    //lines
    QPainter line(this);
    line.setRenderHint(QPainter::Antialiasing,true);
    line.setPen(QPen(Qt::black,2,Qt::SolidLine,Qt::RoundCap,Qt::MiterJoin));
    line.setBrush(QBrush(Qt::black,Qt::SolidPattern));
    line.drawLine(50,50,50,200); // Y axis
    line.drawLine(50,200,250,200);  // x axis

    // rectangle
    int a[5] ={20,60,120,30,200};    //length = a[]
    int x= 50.0  ;
    int y= 200 ;
    int inc = 200/5;

    for(int i=0 ; i<5 ;i++)
    {
        QRectF rect( (x + (i * inc)), (y - a[i]), (x + (i * inc) + 20), Y);
        line.setPen(QPen(Qt::green,8,Qt::SolidLine,Qt::RoundCap,Qt::MiterJoin));
        line.drawRect(rect);
    }

}
Random Solutions  
 
programming4us programming4us