All depends on what you want of course.
But here's an example.

Note: this example demonstrates how to draw a series of bars in a chart like style using a custom widget.

This, however, will not scale and is very very inefficient.

To create a better charting system, make full use of the model view design.
Datapoints should not contain any more information than their actual values. In the example below, it also contains view parameters.

Take for example the width of a bar. I added this parameter to the datapoint. I'm sure you can imagine another kind of chart that doesn't use the width to draw a datapoint.

You might think, why not add it to the widget itself? That too is not a good idea because then it would be a lot of work to add new kinds of charts.

Better is to make use of some kind of delegate.
Your datapoints contain the usefull point information.
The view handles the drawing.
How things are drawn is the task of this delegate. You can have a barchart delegate, a piechart delegate etc...

But, to keep things simple, here's an inefficient way to draw barcharts.

screenshot.PNG

datapoint.h
Qt Code:
  1. #ifndef DATAPOINT_H
  2. #define DATAPOINT_H
  3.  
  4. #include <QtGlobal>
  5. #include <QColor>
  6. #include <QList>
  7. #include <QMetaType>
  8.  
  9. class DataPoint
  10. {
  11. public:
  12. DataPoint();
  13. DataPoint(qreal x, qreal Y);
  14.  
  15. void setX(qreal x);
  16. void setY(qreal y);
  17.  
  18. void setColor(const QColor &color);
  19. void setWidth(int width);
  20.  
  21. qreal x() const;
  22. qreal y() const;
  23.  
  24. QColor color() const;
  25. int width() const;
  26.  
  27. private:
  28. qreal m_x;
  29. qreal m_y;
  30. QColor m_color;
  31. int m_width;
  32. };
  33.  
  34. typedef QList<DataPoint*> DataPointList;
  35.  
  36. #endif // DATAPOINT_H
To copy to clipboard, switch view to plain text mode 


datapoint.cpp
Qt Code:
  1. #include "datapoint.h"
  2.  
  3. DataPoint::DataPoint() :
  4. m_x(0),
  5. m_y(0),
  6. m_color(QColor(qRgb(200, 200, 200))),
  7. m_width(10)
  8. {
  9. }
  10.  
  11. DataPoint::DataPoint(qreal x, qreal y) :
  12. m_x(x),
  13. m_y(y),
  14. m_color(QColor(qRgb(200, 200, 200))),
  15. m_width(10)
  16. {
  17. }
  18.  
  19. void DataPoint::setX(qreal x)
  20. {
  21. m_x = x;
  22. }
  23.  
  24. void DataPoint::setY(qreal y)
  25. {
  26. m_y = y;
  27. }
  28.  
  29. void DataPoint::setColor(const QColor &color)
  30. {
  31. m_color = color;
  32. }
  33.  
  34. void DataPoint::setWidth(int width)
  35. {
  36. m_width = width;
  37. }
  38.  
  39. qreal DataPoint::x() const
  40. {
  41. return m_x;
  42. }
  43.  
  44. qreal DataPoint::y() const
  45. {
  46. return m_y;
  47. }
  48.  
  49. QColor DataPoint::color() const
  50. {
  51. return m_color;
  52. }
  53.  
  54. int DataPoint::width() const
  55. {
  56. return m_width;
  57. }
To copy to clipboard, switch view to plain text mode 


barchart.h
Qt Code:
  1. #ifndef BARCHART_H
  2. #define BARCHART_H
  3.  
  4. #include <QWidget>
  5. #include <QPaintEvent>
  6. #include <QtGlobal>
  7.  
  8. #include "datapoint.h"
  9.  
  10. class BarChart : public QWidget
  11. {
  12. Q_OBJECT
  13. public:
  14. explicit BarChart(QWidget *parent = 0);
  15.  
  16. void setDataPointList(const DataPointList &pointList);
  17.  
  18. protected:
  19. void paintEvent(QPaintEvent *event);
  20.  
  21. signals:
  22.  
  23. public slots:
  24.  
  25. private:
  26. DataPointList m_dataPointList;
  27.  
  28. qreal maximumHeight();
  29. qreal maximumWidth();
  30.  
  31. qreal yAxisWidth();
  32. qreal xAxisHeight();
  33.  
  34. qreal spaceBetweenBars;
  35. qreal chartOuterMargin;
  36.  
  37. qreal dataPointHeight(qreal y);
  38.  
  39. void drawEmptyBarChart(QPainter *painter);
  40. void drawYAxis(QPainter *painter);
  41. void drawXAxis(QPainter *painter);
  42. void drawDataPoints(QPainter *painter);
  43. };
  44.  
  45. #endif // BARCHART_H
To copy to clipboard, switch view to plain text mode 


barchart.cpp
Qt Code:
  1. #include "barchart.h"
  2.  
  3. #include <QPainter>
  4. #include <QFontMetricsF>
  5. #include <QPointF>
  6. #include <QPen>
  7. #include <QBrush>
  8.  
  9. BarChart::BarChart(QWidget *parent) :
  10. QWidget(parent)
  11. {
  12. spaceBetweenBars = 5;
  13. chartOuterMargin = 10;
  14. }
  15.  
  16. void BarChart::setDataPointList(const DataPointList &pointList)
  17. {
  18. m_dataPointList = pointList;
  19. update();
  20. }
  21.  
  22. void BarChart::paintEvent(QPaintEvent *event)
  23. {
  24. QWidget::paintEvent(event);
  25.  
  26. QPainter painter(this);
  27.  
  28. if (m_dataPointList.isEmpty())
  29. drawEmptyBarChart(&painter);
  30. else {
  31. drawXAxis(&painter);
  32. drawYAxis(&painter);
  33. drawDataPoints(&painter);
  34. }
  35. }
  36.  
  37. qreal BarChart::maximumHeight()
  38. {
  39. qreal height = 0;
  40.  
  41. foreach(DataPoint *point, m_dataPointList) {
  42. if (point->y() > height)
  43. height = point->y();
  44. }
  45.  
  46. return height;
  47. }
  48.  
  49. qreal BarChart::maximumWidth()
  50. {
  51. qreal width = 0;
  52.  
  53. foreach(DataPoint *point, m_dataPointList) {
  54. width += point->width() + spaceBetweenBars;
  55. }
  56.  
  57. return width;
  58. }
  59.  
  60. qreal BarChart::yAxisWidth()
  61. {
  62. QString maximumValue = QString::number(maximumHeight());
  63. QFontMetrics fm(font());
  64. qreal textWidth = fm.width(maximumValue);
  65.  
  66. return textWidth + 5;
  67. }
  68.  
  69. qreal BarChart::xAxisHeight()
  70. {
  71. QFontMetrics fm(font());
  72. qreal textHeight = fm.height();
  73.  
  74. return textHeight + 5;
  75. }
  76.  
  77. qreal BarChart::dataPointHeight(qreal y)
  78. {
  79. QFontMetrics fm(font());
  80. qreal textHeight = fm.height();
  81.  
  82. return y * ((height() - (2 * (chartOuterMargin + (textHeight / 2))) - xAxisHeight()) / maximumHeight());
  83. }
  84.  
  85. void BarChart::drawEmptyBarChart(QPainter *painter)
  86. {
  87. QString text("No datapoints available!");
  88.  
  89. qreal centreX = width() / 2;
  90. qreal centreY = height() / 2;
  91.  
  92. QFontMetricsF fontMetrics(font());
  93. qreal textWidth = fontMetrics.width(text);
  94. qreal textHeight = fontMetrics.height();
  95.  
  96. qreal textX = centreX - (textWidth / 2);
  97. qreal textY = centreY - (textHeight / 2);
  98.  
  99. painter->drawText(QPointF(textX, textY), text);
  100. }
  101.  
  102. void BarChart::drawYAxis(QPainter *painter)
  103. {
  104. QPen yAxisPen(QColor(qRgb(0, 0, 0)));
  105.  
  106. QString maximumValue = QString::number(maximumHeight());
  107. QFontMetrics fm(font());
  108. qreal textWidth = fm.width(maximumValue);
  109. qreal textHeight = fm.height();
  110.  
  111. qreal yAxisX = chartOuterMargin + textWidth + 5;
  112. qreal yAxisY = chartOuterMargin + (textHeight / 2);
  113. qreal yAxisHeight = height() - yAxisY - xAxisHeight();
  114.  
  115. painter->save();
  116.  
  117. painter->setPen(yAxisPen);
  118. painter->drawLine(QPointF(yAxisX, yAxisY), QPointF(yAxisX, yAxisHeight));
  119.  
  120. painter->drawLine(QPointF(yAxisX - 3, yAxisY), QPointF(yAxisX, yAxisY));
  121. painter->drawLine(QPointF(yAxisX - 3, yAxisHeight), QPointF(yAxisX, yAxisHeight));
  122.  
  123. painter->drawText(QPointF(chartOuterMargin, chartOuterMargin + textHeight), maximumValue);
  124. painter->drawText(QPointF(chartOuterMargin, height() - chartOuterMargin - xAxisHeight()), "0");
  125.  
  126. painter->restore();
  127. }
  128.  
  129. void BarChart::drawXAxis(QPainter *painter)
  130. {
  131. QPen xAxisPen(QColor(qRgb(0, 0, 0)));
  132.  
  133. QFontMetrics fm(font());
  134. qreal textHeight = fm.height();
  135.  
  136. qreal xAxisX = chartOuterMargin + yAxisWidth();
  137. qreal xAxisY = height() - xAxisHeight() - chartOuterMargin - (textHeight/2);
  138. qreal xAxisWidth = xAxisX + maximumWidth() + spaceBetweenBars;
  139. painter->save();
  140.  
  141. painter->setPen(xAxisPen);
  142. painter->drawLine(QPointF(xAxisX, xAxisY), QPointF(xAxisWidth, xAxisY));
  143.  
  144. qreal xAxisMark = xAxisX + spaceBetweenBars; // Keep some space between the first bar and the Y axis
  145.  
  146. foreach(DataPoint *point, m_dataPointList) {
  147. xAxisMark += point->width()/2;
  148.  
  149. painter->drawLine(QPointF(xAxisMark, xAxisY), QPointF(xAxisMark, xAxisY + 3));
  150.  
  151. QFontMetrics fm(font());
  152. qreal markTextWidth = fm.width(QString::number(point->x()));
  153. qreal markTextHeight = fm.height();
  154.  
  155. painter->drawText(QPointF(xAxisMark - (markTextWidth/2), xAxisY + markTextHeight + 5), QString::number(point->x()));
  156.  
  157. xAxisMark += (point->width() / 2) + spaceBetweenBars;
  158. }
  159.  
  160. painter->restore();
  161. }
  162.  
  163. void BarChart::drawDataPoints(QPainter *painter)
  164. {
  165. qreal dataPointCentre = chartOuterMargin + yAxisWidth() + spaceBetweenBars;
  166.  
  167. QFontMetrics fm(font());
  168. qreal textHeight = fm.height();
  169.  
  170. foreach(DataPoint *point, m_dataPointList) {
  171. QBrush dataPointBrush(point->color());
  172. QPen dataPointPen(QColor(qRgb(0, 0, 0)));
  173.  
  174. dataPointCentre += point->width()/2;
  175.  
  176. painter->save();
  177.  
  178. painter->setBrush(dataPointBrush);
  179. painter->setPen(dataPointPen);
  180.  
  181. qreal dpHeight = dataPointHeight(point->y());
  182.  
  183. painter->drawRect(QRectF(dataPointCentre - (point->width()/2), height() - xAxisHeight() - chartOuterMargin - (textHeight/2), point->width(), -dpHeight));
  184.  
  185. painter->restore();
  186.  
  187. dataPointCentre += (point->width() / 2) + spaceBetweenBars;
  188. }
  189. }
To copy to clipboard, switch view to plain text mode 

Usage:
Qt Code:
  1. dataPoints.append(new DataPoint(1, 1));
  2. dataPoints.append(new DataPoint(2, 2));
  3.  
  4. DataPoint *colorPoint = new DataPoint(3, 3);
  5. colorPoint->setColor(QColor(qRgb(255, 0, 0)));
  6. dataPoints.append(colorPoint);
  7.  
  8. dataPoints.append(new DataPoint(4, 4));
  9.  
  10. DataPoint *widthPoint = new DataPoint(5, 5);
  11. widthPoint->setWidth(20);
  12. dataPoints.append(widthPoint);
  13.  
  14. dataPoints.append(new DataPoint(6, 6));
  15.  
  16. barChart = new BarChart;
  17.  
  18. //...
  19.  
  20. barChart->setDataPointList(dataPoints);
To copy to clipboard, switch view to plain text mode