PDA

View Full Version : QPainter::rotate and Qwt problems



alex_sh
19th October 2011, 20:14
Hello,

I'm porting a QPainter-based code (written in absolute coordinates) to Qwt plot item. Normally the porting consists of transforming the coordinates using QwtScaleMap::transform() and using QwtPainter's static draw*() functions. But the original code uses QPainter::rotate() which really screws it all up and gives some really strange (and inexplicable) results.

For example,


for(int i=0; i<50; i++) {
painter->save();
painter->rotate(-90 - i*7.2);
QwtPainter::drawText(painter, QwtScaleMap::transform(xMap, yMap, QRectF(-14,-595,100,100)),
Qt::AlignLeft | Qt::AlignBottom, QString::number);
painter->restore();
}

Basically, what it does (in the original) is that it draw the numbers radially around a circle (think numbers around a dial).

I can use a separate QTransform object, rotate it and map that rectangle to it, giving me the text approximately where I want it. But, it's still off enough to be bad, and the text itself is not rotated (and I really need that).

Is there a way to make the painter rotation work with Qwt? Or maybe draw the rotated text some other way?

Thanks in advance!
Alexander

Uwe
20th October 2011, 06:45
You have to take care about the origin of your rotation. Your code rotates around the top left position of the plot cancas.

Rotating the text around the center of a rectangle would work like this ( untested, out of my memory ):


QRectF textRect QwtScaleMap::transform(xMap, yMap, QRectF(-14,-595,100,100) );
painter->save();
painter->translate( textRect.center() );
painter->rotate( -90 - i * 7.2);

textRect.moveCenter( QPointF( 0.0, 0.0 ) );
QwtPainter::drawText(painter, textRect, Qt::AlignLeft | Qt::AlignBottom, QString::number);

painter->restore();But in your case I guess you want to align the rotated text to a specific position not to the center of some pointless rectangle.

HTH,
Uwe

alex_sh
20th October 2011, 17:44
Thanks!

Using your hint I was able to fix the problem like this:


QPointF zero_point = QwtScaleMap::transform(xMap, yMap, QPointF(0, 0));
for(int i=0; i< 50; i++) {
painter->save();
painter->translate(zero_point);
painter->rotate(-90 - i * 7.2);
painter->translate(QPointF(-zero_point.x(), -zero_point.y()));
QwtPainter::drawText(painter, QwtScaleMap::transform(xMap, yMap, QRectF(-14,595-100,100,100)),
Qt::AlignLeft | Qt::AlignBottom, QString::number(i));
painter->restore();
}


The original code had (0, 0) coordinates in the center of the painter (using window / viewport translation), so that was the rotation center. By translating to it, rotating and then translating back the problem fixed itself.
Another small problem was that the Y axis was inverted (QwtScaleMap::transform() inverts it), hence 595-100 instead of -595.

Thanks again!