[PyKDE] QCanvasRectangle porblems

Dustin Vink d.vink at mech.gla.ac.uk
Wed Jun 11 17:01:01 BST 2003


A newbie question. I wish to creat a selection rectangle for
QCanvasView, that is, I wish to select QCanvasItems using the mouse by
dragging a rectangle such that certain Items are contained in it. Here
is a snippet of newbie code

class selectionRect(QCanvasRectangle):
	def __init__(self,pos,canvas):
		QCanvasRectangle.__init__(self,pos.x(),pos.y(),1,1,canvas)
		self.canvas = canvas
		self.setBrush(QBrush(Qt.NoBrush))
		self.x = pos.x()
		self.y = pos.y()

	def size(self,pos):
		self.width = pos.x() - self.x
		self.height = pos.y() - self.y
		self.setSize(self.width,self.height)

In my QCanvasView I have:

def contentsMousePressEvent(self,e):
		pos = self.inverseWorldMatrix().map(e.pos())
		colList = self.canvas.collisions(pos)
		if colList != []:
			self.moving = colList[0]
			self.moving_start = pos
		else:
			self.moving = 0
			self.rect = selectionRect(pos,self.canvas)
			self.rect.show()
			self.canvas.update()

def contentsMouseMoveEvent(self,e):
		pos = self.inverseWorldMatrix().map(e.pos())
		if self.moving:
			self.moving.moveBy(
				pos.x()-self.moving_start.x(),
				pos.y()-self.moving_start.y())
			self.moving_start = pos
			self.canvas.update()
		else:
			self.rect.size(pos)
			self.canvas.update()

If the QCursor does not intersect any objects, then I can drag the
rectangle. The rectangle is perfectly drawn in all direction, but when
I move it, the canvas does not correctly update rectangles that
have negative sizes. Only  rectangles that are dragged from
topleft to bottom right can be moved without leaving traces of lines
on the canvas. Anyone seen this kind of behaviour?



Dustin






More information about the PyQt mailing list