PDA

View Full Version : rotating a custom made symbol for QwtPlotCurve



babu198649
16th March 2008, 15:20
hi
i have created a customized symbol by subclassing the QwtSymbol class and reimplementing the function,


class feather : public QwtSymbol
{
public:
void draw(QPainter *painter, const QRect &r) const
{
//shape of symbol( |___ )
QwtPainter::drawLine(painter, r.bottomLeft(), r.topLeft());
QwtPainter::drawLine(painter, r.bottomLeft(), r.bottomRight());
}
}
i want to rotate the symbols at some angle. but,i can not use the painter argument for doing this because its co-ordinates are not the co-ordinates of r(argument QRect).

how can i do this:confused:.

Uwe
17th March 2008, 09:37
i have created a customized symbol by subclassing the QwtSymbol class and reimplementing the function, ...

Note, that you can't customize the symbol of a marker/curve by subclassing, because it gets copied as QwtSymbol. Instead you have to subclass QwtPlotCurve and reimplement QwtPlotCurve::drawSymbols(). In Qwt 5.1-SVN ( svn co https://qwt.svn.sourceforge.net/svnroot/qwt/branches/qwt-5.1 ) you can subclass if you reimplement a QwtSymbol::copy() method.


i want to rotate the symbols at some angle. but,i can not use the painter argument for doing this because its co-ordinates are not the co-ordinates of r(argument QRect).

The QRect is the rectangle ( position + size), where you have to paint your symbol. I'm not sure , what you mean by painter coordinates - maybe its translation ?

It depends on your type of symbol, if QPainter::rotate is the best way to paint it. If you want to use it you might have to set QPainter::translate to the position of your rotation.

HTH,
Uwe

babu198649
17th March 2008, 10:51
hi
i have subclassed the QwtPlotCurve like this .



#include <QtGui>
#include "qwt_plot.h"
#include "qwt_plot_curve.h"
#include "qwt_valuelist.h"
#include "qwt_scale_div.h"
#include "qwt_symbol.h"
#include "qwt_painter.h"
class feather : public QwtSymbol
{
public:

void draw(QPainter *painter, const QRect &r) const
{
//painter->translate(r.topLeft());

//shape |_|__
QwtPainter::drawLine(painter, r.bottomLeft(), r.topLeft());
QwtPainter::drawLine(painter, r.bottomLeft(), r.bottomRight());
QwtPainter::drawLine(painter, r.right()-r.width()/2, r.bottom(),
r.right()-r.width()/2, r.top());
}
};

class curve_data : public QwtPlotCurve
{
public:
void drawSymbols(QPainter *painter, const QwtSymbol &symbol,
const QwtScaleMap &xMap, const QwtScaleMap &yMap, int from, int to) const
{
QRect rect;
rect.setSize(QwtPainter::metricsMap().screenToLayo ut(symbol.size()));

for (int i = from; i <= to; i++)
{
const int xi = xMap.transform(x(i));
const int yi = yMap.transform(y(i));
rect.moveCenter(QPoint(xi, yi));
painter->setPen(QPen(Qt::blue));

feather f;
f.draw(painter, rect);
}
}
};

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QwtPlot *qwt =new QwtPlot;
qwt->setCanvasBackground(QColor(Qt::white));

//set scale for x-axis
qwt->setAxisScale(QwtPlot::xBottom, 13, 0);
qwt->setAxisMaxMajor(QwtPlot::xBottom, 24);
qwt->setAxisMaxMinor(QwtPlot::xBottom, 0);

//set scale for y-axis
qwt->setAxisScale(QwtPlot::yLeft, 0, 31);

//create data for curve
int MAX=12*30;
double x[MAX], y[MAX];
int k=1;
int l=1;

for (int i=0; i<MAX; i++)
{
x[i]=k;
y[i]=l;
k++;
if (k>12)
{
k=1;
l++;
}
}

//create symbol for curve
feather sym;
sym.setStyle(QwtSymbol::Cross);
sym.setSize(30, 10);

//create curve , set symbol and data to curve
curve_data *cur=new curve_data;
cur->setStyle(QwtPlotCurve::NoCurve);
cur->setSymbol(sym);
cur->setData(x, y, MAX);
cur->attach(qwt);

qwt->resize(800, 600);
qwt->show();

return a.exec();
}


i get the symbols , but i need to rotate individual symbols at different angles.

Uwe
17th March 2008, 17:08
i get the symbols , but i need to rotate individual symbols at different angles.
You know the position and the size of your symbol and you have control over the painting code.
What is missing ( maybe, that you don't know how to paint something rotated at all ) ?

Uwe

babu198649
18th March 2008, 10:24
i have messed with the co-oridnates and now i have corrected it .
thank u.

also i have noticed that the subclassed function of QwtSymbol (draw ) is called twice ,
i have 360 symbols but the draw function is called 720 times.which i have found through global variable.

why is it called twice.

Uwe
18th March 2008, 10:44
why is it called twice.
It is called, whenever the content of your plot canvas has to be recreated ( QwtPlot::replot ). Use your debugger set a breakpoint in QwtPlotCurve::drawSymbols() and look at the stack, where you are.

Uwe