1.以在UI上添加柱状图的类为例(Histogram)
#ifndef STUDY_HISTOGRAM_H
#define STUDY_HISTOGRAM_H
#include <QVector>
#include <QWidget>
// 前向声明
QT_BEGIN_NAMESPACE
class QColor;
class QRect;
class QString;
class QPaintDevice;
QT_END_NAMESPACE
class Study_HistogramItem
{
public:
QString m_Name;
qreal m_Value;
QColor m_PillarColor;
QRect m_PillarRect;
};
class Study_Histogram : public QWidget
{
public:
Study_Histogram( QWidget* parent = 0 );
//方法
void Study_AddItem( QString name, qreal value,
QColor pillarColor );
void Study_SetMaxValue( quint32 maxValue ); // 设置最大值,以便绘图
void Study_Clear(); // 清空所有已添加的Item
void Study_Paint( QPaintDevice* pDevice ); // 绘图
protected:
//刷新页面
void paintEvent(QPaintEvent *);
private:
void Study_DrawAxis( QPaintDevice* pDevice, QPainter* pPainter ); // 绘制坐标轴
void Study_DrawPillars( QPaintDevice* pDevice, QPainter* pPainter ); // 绘制柱子
void Study_DrawText( QPainter *pPainter ); // 绘制文字
void Study_DrawScale( QPaintDevice* pDevice, QPainter* pPainter ); // 绘制刻度
//类属性
enum Study_HistogramOptions
{
blankWidth = 64, // 两个柱子间的空格大小
pillarIndent = 0, // 首个柱子缩进的大小
xAxisOffset = 16, // X轴的偏移(相对于左边界)
yAxisOffset = 16, // Y轴的偏移(相对于下边界)
textRectHeight =