QT创造一个新的类(柱状图的类),并关联属性和方法

本文详细介绍了如何在C++中创建一个名为Study_Histogram的类,用于UI上显示柱状图,包括添加柱形、设置最大值、清空数据等功能,以及关键方法如PaintEvent的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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	= 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值