Hi,

I have a strange effect with hover events and QGraphicsItem.

The example below demonstrate the problem, the hover events are emitted
only on the border of the item but not on the item himsef.

If I don't draw a border, events are never emitted.

Platform    Linux-2.6.32-33-generic-i686-with-Ubuntu-10.04-lucid
Qt              4.6.2  (probably, the problem is here)
Python       2.6.5
PyQt          4.7.2
Sip             4.10.1

Regards


# -*- coding: utf-8 -*-

from PyQt4 import QtCore, QtGui

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.resize(400, 400)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.gridLayout = QtGui.QGridLayout(self.centralwidget)
        self.verticalLayout = QtGui.QVBoxLayout()
        self.viewer = QtGui.QGraphicsView(self.centralwidget)
        self.verticalLayout.addWidget(self.viewer)
        self.scene = QtGui.QGraphicsScene()
        self.scene.setSceneRect(0, 0, 300, 300)
        self.viewer.setScene(self.scene)
        self.item = PixmapItem(self.viewer, None, self.scene)
        self.gridLayout.addLayout(self.verticalLayout, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        MainWindow.show()

class PixmapItem(QtGui.QGraphicsPixmapItem):
    def __init__(self, viewer, parent=None, scene=None):
        super(PixmapItem, self).__init__(parent, scene)
        self.viewer = viewer
        self.setFlags(QtGui.QGraphicsItem.ItemIsMovable |
                QtGui.QGraphicsItem.ItemSendsScenePositionChanges)
        self.setAcceptHoverEvents(True)
        self.old_cursor = self.viewer.cursor()

        color = QtGui.QColor()
        color.setRgb(150, 220, 250, 40)
        border = QtGui.QColor()
        border.setRgb(30, 40, 220, 255)
        painter = QtGui.QPainter()
        pix = QtGui.QPixmap(QtCore.QSize(200, 200))
        pix.fill(color)
        painter.begin(pix)
        painter.setPen(QtGui.QPen(border, 2,
                              QtCore.Qt.SolidLine, QtCore.Qt.RoundCap,
                              QtCore.Qt.MiterJoin))
        painter.drawRect(0.0, 0.0, 200.0, 200.0)
        painter.end()
        self.setPixmap(pix)
        self.setPos(50, 50)
        self.show()

    def hoverEnterEvent(self, event):
        print "Enter event in pixmap item"
        self.setCursor(QtCore.Qt.CrossCursor)
        event.accept()

    def hoverLeaveEvent(self, event):
        print "Leave event from pixmap item"
        self.viewer.setCursor(self.old_cursor)
        event.accept()

    def hoverMoveEvent(self, event):
        print "hover move event on pixmap item"
        event.accept()

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
   
    sys.exit(app.exec_())
--
Vincent V.V.
Oqapy . Qarte+7 . PaQager
_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to