PDA

View Full Version : setting needle size in qwtCompass



user
19th July 2007, 07:22
Hello,
This is my first post so I hope I give all the required info.
I use Qt 4.2.3 and Qwt 5.0.1

I'm trying to create a compass or a dial that will show wind direction.
I created a class that inherits QwtCompass and in it's constructor:

windNeedle = new QwtCompassWindArrow( Style1, red, black );
rose = new QwtSimpleCompassRose( 4, 1 );

I instanciate this class in a layout which is inside a QWidget. So the compass is only part of what is displayed on the screen.

I have 2 problems:

1. I would like to change the size and direction of the windNeedle. The function I found is drawStyle1Needle(...) or draw() which calls that function.
When I try to use that function in the constructor I get the error: QPainter::begin: Widget painting can only begin as a result of a paintEvent.
What is the right way to use those functions? How do I create the paintEvent and how and when do I use the draw() function?

2. I would also like to change the direction of the windNeedle (since it represents wind direction and it may change) - How do I get the value after the user moved the needle and how do I set the needle to a certain direction? - I guess the setting part would require redrawing of the compass which is my first problem.

Uwe
19th July 2007, 09:23
How do I get the value after the user moved the needle...
You have to look at the derived API of the base classes of QwtCompass:

1) QwtDoubleRange::value(), or
2) QwtAbstractSlider::valueChanged(double) signal.

0.0 means north, 270.0 is west.

Guess you want write something like this: "connect(compass, SIGNAL(valueChanged(double)), ...);"


... and how do I set the needle to a certain direction.
"QwtAbstractSlider::setValue(double)", you don't have to repaint anything.


I would like to change the size of the windNeedle



class YourCompass::public QwtCompass
{
...

virtual void YourCompass::drawNeedle(
QPainter *painter, const QPoint &pos,
int radius, double direction,
QPalette::ColorGroup colorGroup) const
{
radius = ...; // decrease the radius here
QwtCompass::drawNeedle(painter, pos, radius,
direction, colorGroup);
}
}

HTH,
Uwe

user
20th July 2007, 01:06
Thank you Uwe.

I can set and get the Needle value now, but I still have a problem with the needle size.

Can you please tell me what I'm missing?

I have a class that has an instance of the QwtCompass:


class Atmosphere: public QWidget
{
Atmosphere::Atmosphere
{
a=new myCompass();
connect(myCompass, SIGNAL(valueChanged(double)), ...);
}
...
}

class myCompass::public QwtCompass
{
...
myCompass::myCompass( parent ) : QwtCompass(parent)
{
windNeedle = new QwtCompassWindArrow( Style1, red, black );
rose = new QwtSimpleCompassRose( 4, 1 );
update();
}

// As in your replay, I've added:
virtual void YourCompass::drawNeedle( QPainter *painter, const QPoint &pos,
int radius, double direction,
QPalette::ColorGroup colorGroup) const
...
}

}


I've added a printout in my drawNeedle function. It looks as if the function is never executed (not even when I move the needle or resize the window) . Do I need to call it manually?

Uwe
20th July 2007, 08:13
Of course it has to look like this.


class myCompass: public QwtCompass
{
...

virtual void drawNeedle( QPainter *painter, const QPoint &pos,
int radius, double direction,
QPalette::ColorGroup colorGroup) const
{
...
}
};

If this doesn't help start the debugger, set a breakpoint in QwtDial::drawNeedle and check if it is called from myCompass::drawNeedle.

Uwe

user
23rd July 2007, 00:40
Thank you Uwe.

It works now. I didn't type it properly: const QPoint instead of const QPoint & :o