0

I'm just wondering, is it possible to drag points on a QCanvas. I'd like to draw random points on a canvas for example, and I want each point to be dragable everywhere in the canvas.

erbal
  • 727
  • 3
  • 12
  • 32

1 Answers1

0

I am just getting into pyqt although I have done a fair bit of canvas based GUI dev using wxPython and WPF, so I can give you the overall approach but not the details for pyqt.

You should be able to

  • capture the mouse left click event of the canvas,
  • find the point that is closest to it,
  • change the cursor so user is aware that they have "taken" the point,
  • then move the point as mouse move signal handler, and finally
  • in mouse release signal handler, "drop" the point (change its coordinates and change cursor back to normal).

You should probably indicate that a point can be grabbed when mouse hovers near a point, by drawing say a circle around that point; then when you drag the point you actually drag that circle with point in middle, so it is easy to see where point is.

You should also probably respond to ESC key press: if user presses ESC during a move, the point goes back to where it started and the operation is aborted.

For pyqt, look at How to draw a line with animation in PyQt4: it uses QPainter in a class derived from QWidget, this seems to be common pattern, but there is also QGraphicsView which may provide higher level API. The QCanvas class seems very old and seemed to require QCanvasView from which your code could capture mouse events.

Community
  • 1
  • 1
Oliver
  • 27,510
  • 9
  • 72
  • 103
  • How could I connect my canvas to the mouse click? I don't really find any slot to do that. – erbal Feb 17 '14 at 19:41