0

I am a bit new to QT. I have a separate Crosshair class that simply renders a crosshair using the QPainter and QPen. I used the paint() function and it does display the crosshairs at some position in the window. How can make the crosshairs follow the current mouse position?

This is my approach but I can't get it to work. I was following the VoidRealms tutorial.

void Crosshair::mouseMoveEvent(QGraphicsSceneMouseEvent *event){

   // i want to update the x and y position when the mouse moves
   //x = mouse.x
   //y = mouse.y
   QGraphicsItem::mouseMoveEvent(event);
   update();
 }
lambda
  • 1,225
  • 2
  • 14
  • 40

1 Answers1

2

This should do it for you:

this->setPos(event->x(), event->y());

There are also other helper functions available if you are doing a mapping to your scene when you are outside of QGraphicsSceneMouseEvent.

I described it here:

How to draw a point (on mouseclick) on a QGraphicsScene?

Hope that helps.

Community
  • 1
  • 1
phyatt
  • 18,472
  • 5
  • 61
  • 80
  • is it possible to do this without mouse click? I want the crosshair to follow the cursor at all times – lambda Jul 12 '13 at 01:22