PDA

View Full Version : setScaleDraw(QwtScaleDraw * scaleDraw) function in QwtPlotScaleItem class



lnk
24th June 2014, 22:47
Hi, everyone.
I am using qwt-6.1.0 library. I want to draw scales inside the plot canvas. I have read the example which is given in QwtPlotScaleItem class reference and created a subclass of QwtPlot where in constructor I wrote this:


QwtPlotScaleItem *scaleItemY =
new QwtPlotScaleItem(QwtScaleDraw::RightScale);
scaleItemY->setBorderDistance(0);
scaleItemY->attach(this);
this->enableAxis(QwtPlot::yLeft, false);

QwtPlotScaleItem *scaleItemX =
new QwtPlotScaleItem(QwtScaleDraw::TopScale);
scaleItemX->setBorderDistance(0);
scaleItemX->attach(this);
this->enableAxis(QwtPlot::xBottom, false);

Here everythig is ok. When I attach grid it cross labels, and now I want to shift labels, so that there would be no intersections of grids and labels. For that I have created a subclasses (XScaleDraw and YScaleDraw) of QwtScaleDraw class where i override virtual void drawLabel (QPainter* painter, double value) const function: instead of


lbl.draw ( painter, QRect( QPoint( 0, 0 ), labelSize.toSize() ) );

I wrote (for X shift) :


lbl.draw ( painter, QRect( QPoint( 0+(int)(labelSize.width()/2), 0 ), labelSize.toSize() ) );

and for Y shift:


lbl.draw ( painter, QRect( QPoint(0, 0+(int)(labelSize.height()/2) ), labelSize.toSize() ) );

Now I set scale draw and after that problems arise:


QwtPlotScaleItem *scaleItemY =
new QwtPlotScaleItem(QwtScaleDraw::RightScale);
scaleItemY->setScaleDraw(new YScaleDraw);
scaleItemY->setBorderDistance(0);
scaleItemY->attach(this);
this->enableAxis(QwtPlot::yLeft, false);

QwtPlotScaleItem *scaleItemX =
new QwtPlotScaleItem(QwtScaleDraw::TopScale);
scaleItemX->setScaleDraw(new XScaleDraw);
scaleItemX->setBorderDistance(0);
scaleItemX->attach(this);
this->enableAxis(QwtPlot::xBottom, false);

XScale is drawn at the top (instead of bottom) and YScale is disappeared. Actually it happens even if I don't override virtual void drawLabel (QPainter* painter, double value) const function and create empty subclass of QwtScaleDraw. Any idea what is going on here?

Uwe
25th June 2014, 18:46
You can't change the drawing code without doing the same for the layout code - unfortunatley those methods are not virtual.

But instead of changing the scale item you could also try to configure the grid not draw over the scale. F.e. in the qwtpolar package the grid calculates a clip region from the bounding rectangles of the scale tick labels before drawing its lines ( have a look at QwtPolarGrid::draw ).

You could do something similar in YourGrid::draw, but also somewhere in an overloaded QwtPlot::drawItems().

Uwe