①继承QwtPlotItem类
class myplot:public QwtPlotItem
{
public:
myplot();
~myplot();
void SetString(QString);
void SetPen(QPen);
void SetPosition(float x,float y);
QPen pen;
float x,y;
QString text;
virtual void draw( QPainter* painter,const QwtScaleMap& xMap, const QwtScaleMap& yMap,const QRectF& canvasRect ) const QWT_OVERRIDE;
virtual int rtti() const;
};
②实现继承类的各个成员方法,最主要的是实现draw方法。
void myplot::draw(QPainter *painter, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect) const
{
//painter->drawText(xMap->);
QFont font;
font.setPointSize(16);
painter->setFont(font);
painter->setPen(pen);
painter->drawText(x,y,text);
}int myplot::rtti() const
{
return 99;
}
myplot::myplot()
{
}
myplot::~myplot()
{
}
void myplot::SetString(QString value)
{
text=value;
}
void myplot::SetPen(QPen p)
{
pen=p;
}
void myplot::SetPosition(float x, float y)
{
this->x=x;
this->y=y;
}