<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
</head>
<body bgcolor="#ffffff" text="#000000">
<font face="Courier New, Courier, monospace">This is a reduced part of
a project I am working on (Win7, Python 3.1, PyQt 4.7.3, Qt 4.6.2). <br>
<br>
I can set different "modes" via keyPressEvent (F1, F2, F3) in the
MyView class. There I set different cursors according to the mode in
MyView.updateItems().<br>
<br>
When I run the code and the mouse is outside of the test rectangles the
switching of cursors works fine. As soon as I move the mouse over one
of the rectangles, the cursors switching works fine in/over the
rectangles but as soon as I move the mouse outside of the rectangles it
does not switch anymore.<br>
<br>
I expected the cursor switching to still work outside of the rectangles
when the mouse is "over" the view are not covered by rectangles.<br>
<br>
Is this expectation wrong? Did I do something wrong? <br>
<br>
Thanks in advance<br>
Chris<br>
<br>
<br>
from PyQt4.QtCore import *<br>
from PyQt4.QtGui import *<br>
<br>
class MyRect(QGraphicsRectItem):<br>
<br>
def __init__(self, parent=None, scene=None):<br>
# init parent<br>
super().__init__(parent, scene)<br>
<br>
# set flags<br>
self.setFlag(QGraphicsItem.ItemIsSelectable, True)<br>
self.setFlag(QGraphicsItem.ItemIsMovable, True)<br>
self.setFlag(self.ItemSendsGeometryChanges, True)<br>
self.setAcceptHoverEvents(True)<br>
<br>
def paint(self, painter, option, widget):<br>
if self.isSelected():<br>
painter.setPen(QPen(Qt.black, 1, Qt.DotLine))<br>
else: <br>
painter.setPen(QPen(Qt.black, 1, Qt.SolidLine))<br>
<br>
painter.setBrush(QBrush(Qt.white, Qt.SolidPattern))<br>
painter.drawRect(self.rect())<br>
<br>
class MyView(QGraphicsView):<br>
<br>
def __init__(self, parent=None):<br>
super().__init__(parent)<br>
self.setMouseTracking(True)<br>
self.scale(1,1)<br>
self.startPos = None<br>
<br>
def setSelectMode(self):<br>
print("view setting select mode")<br>
self.scene().setViewMode(MyScene.SELECTMODE)<br>
<br>
def setEditMode(self):<br>
print("view setting edit mode")<br>
self.scene().setViewMode(MyScene.EDITMODE)<br>
if len(self.scene().selectedItems()) > 1:<br>
self.scene().clearSelection()<br>
<br>
def setDrawMode(self):<br>
print("view setting draw mode")<br>
self.scene().setViewMode(MyScene.DRAWMODE)<br>
self.scene().clearSelection()<br>
<br>
def updateItems(self):<br>
print("view updateItems")<br>
m = self.scene().viewMode()<br>
<br>
if m == MyScene.SELECTMODE:<br>
print("is select mode")<br>
self.setCursor(Qt.ArrowCursor)<br>
itemCursor = Qt.OpenHandCursor<br>
<br>
elif m == MyScene.EDITMODE:<br>
print("is edit mode")<br>
self.setCursor(Qt.ForbiddenCursor)<br>
itemCursor = Qt.ForbiddenCursor<br>
<br>
elif m == MyScene.DRAWMODE:<br>
print("is draw mode")<br>
self.setCursor(Qt.CrossCursor)<br>
itemCursor = Qt.CrossCursor<br>
<br>
items = self.scene().items()<br>
<br>
for item in items:<br>
item.setCursor(itemCursor)<br>
item.update()<br>
<br>
def drawBackground(self, painter, rect):<br>
# draw a rect in size of sceneRect<br>
<br>
painter.setPen(QPen(Qt.red, 0, Qt.NoPen))<br>
painter.setBrush(QBrush(Qt.lightGray,
Qt.SolidPattern)) <br>
painter.drawRect(self.scene().sceneRect()) <br>
<br>
def mousePressEvent(self, mouseEvent):<br>
print("view mousePress")<br>
curPos = mouseEvent.pos()<br>
self.startPos = self.mapToScene(curPos)<br>
<br>
if self.scene().viewMode() == MyScene.DRAWMODE:<br>
self.scene().newItem = MyRect(scene=self.scene())<br>
self.scene().newItem.setRect(QRectF(self.startPos,
QSizeF(0, 0)))<br>
self.scene().newItem.setSelected(True)<br>
<br>
else:<br>
super().mousePressEvent(mouseEvent)<br>
<br>
def mouseMoveEvent(self, mouseEvent):<br>
#print("view mouseMove")<br>
curPos = self.mapToScene(mouseEvent.pos())<br>
<br>
if self.scene().viewMode() == MyScene.DRAWMODE:<br>
if self.scene().newItem:<br>
newRectF = QRectF(self.startPos, curPos)<br>
if newRectF != self.scene().newItem.rect():<br>
self.scene().newItem.setRect(newRectF.normalized())<br>
else:<br>
super().mouseMoveEvent(mouseEvent)<br>
<br>
def mouseReleaseEvent(self, mouseEvent):<br>
print("view mouseRelease")<br>
<br>
if self.scene().newItem:<br>
# delete item if zero height or width<br>
if (self.scene().newItem.rect().width() == 0 <br>
or self.scene().newItem.rect().height() == 0):<br>
self.scene().removeItem(self.scene().newItem)<br>
del self.scene().newItem<br>
<br>
self.startPos = None<br>
self.scene().newItem = None<br>
super().mouseReleaseEvent(mouseEvent)<br>
<br>
def keyPressEvent(self, keyEvent):<br>
if keyEvent.key() == Qt.Key_F1:<br>
self.setSelectMode()<br>
self.updateItems()<br>
<br>
elif keyEvent.key() == Qt.Key_F2:<br>
self.setEditMode()<br>
self.updateItems()<br>
<br>
elif keyEvent.key() == Qt.Key_F3:<br>
self.setDrawMode()<br>
self.updateItems()<br>
<br>
elif keyEvent.key() == Qt.Key_Delete:<br>
if self.scene().viewMode() == MyScene.SELECTMODE:<br>
items = self.scene().selectedItems()<br>
if len(items):<br>
for item in items:<br>
self.scene().removeItem(item)<br>
del item <br>
<br>
class MyScene(QGraphicsScene):<br>
<br>
SELECTMODE, EDITMODE, DRAWMODE = (0, 1, 2)<br>
validViewModes = [SELECTMODE, EDITMODE, DRAWMODE]<br>
<br>
def __init__(self, parent=None):<br>
super().__init__(parent)<br>
<br>
self._viewMode = MyScene.SELECTMODE<br>
self.newItem = None<br>
<br>
self.setSceneRect(-300, -200, 600, 400)<br>
<br>
# add some item<br>
someRect = MyRect(scene=self)<br>
someRect.setRect(QRectF(0, 0, 160, 80))<br>
<br>
# add another item<br>
anotherRect = MyRect(scene=self)<br>
anotherRect.setRect(QRectF(-80, -40, 80, 160))<br>
<br>
def setViewMode(self, value):<br>
if value != self._viewMode:<br>
if value in MyScene.validViewModes:<br>
self._viewMode = value<br>
else:<br>
raise ValueError("invalid view mode")<br>
<br>
def viewMode(self):<br>
return self._viewMode<br>
<br>
class MainWindow(QMainWindow):<br>
<br>
def __init__(self, parent=None):<br>
# call parent init<br>
super().__init__(parent)<br>
<br>
# setup scene object<br>
self.scene = MyScene()<br>
<br>
# setup view object<br>
self.view = MyView()<br>
<br>
# connect scene to view<br>
self.view.setScene(self.scene)<br>
<br>
# create layout<br>
layout = QVBoxLayout()<br>
<br>
# add view to layout<br>
layout.addWidget(self.view)<br>
<br>
# set the margin of the object in the layout<br>
layout.setContentsMargins(0, 0, 0, 0)<br>
<br>
# create the central widget<br>
self.widget = QWidget()<br>
<br>
# lay it out<br>
self.widget.setLayout(layout)<br>
<br>
# set it to central<br>
self.setCentralWidget(self.widget) <br>
<br>
if __name__ == "__main__":<br>
<br>
import sys<br>
<br>
# setup application object<br>
app = QApplication(sys.argv)<br>
<br>
# create (parent) main window<br>
mainWindow = MainWindow()<br>
mainWindow.setWindowTitle("testScene")<br>
mainWindow.show()<br>
<br>
# run application object<br>
sys.exit(app.exec_())</font>
</body>
</html>