[PyQt] Hover Event for a QGraphicsItem

Elvis Stansvik elvstone at gmail.com
Sat May 7 23:27:32 BST 2016


2016-05-07 23:05 GMT+02:00 sw33tz <nyavuz.nm20 at gmail.com>:
> Thanks for replying...I've updated my code but I still cant get it to work:
>
> class graphics_Object(QtGui.QGraphicsPixmapItem):
>     def __init__(self, parent=None):
>         super(switch_Object, self).__init__(parent)

Should be graphics_Object here, not switch_Object ^ (in the call to super).

Don't think that is the problem though, but it's late here and I have to sleep.

Elvis

>         pixmap = QtGui.QPixmap("item.png")
>         self.graphics_pixItem = QtGui.QGraphicsPixmapItem(pixmap.scaled(40,
> 40, QtCore.Qt.KeepAspectRatio))
>
> self.graphics_pixItem.setFlag(QtGui.QGraphicsPixmapItem.ItemIsSelectable)
>
> self.graphics_pixItem.setFlag(QtGui.QGraphicsPixmapItem.ItemIsMovable)
>         self.graphics_pixItem.setAcceptHoverEvents(True)
>
>
>     def hoverEnterEvent(self, event):
>         print 'hello'
>
> class graphicsScene(QtGui.QGraphicsScene):
>     def __init__(self, parent=None):
>         super(graphicsScene, self).__init__(parent)
>
>     def mousePressEvent(self, event):
>         self.graphics_item = graphics_Object()
>     def mouseReleaseEvent(self, event)
>         self.addItem(self.self.graphics_item.graphics_pixItem)
>         self.graphics_item.self.graphics_pixItem.setPos(event.scenePos())
>
> class Form(QtGui.QMainWindow):
>     def __init__(self):
>         super(Form, self).__init__()
>         self.ui = uic.loadUi('form.ui')
>
>         self.scene = graphicsScene()
>         self.ui.view.setScene(self.scene)
>
>         self.setMouseTracking(True)
>
>
> On Sat, May 7, 2016 at 10:55 PM, Elvis Stansvik [via Python] <[hidden
> email]> wrote:
>>
>> Hi Nesibe,
>>
>> 2016-05-07 19:46 GMT+02:00 sw33tz <[hidden email]>:
>> > I want some small text to pop up when I have my curser over a
>> > QGraphicsItem
>> > in my QGraphicsScene. I have a class that inherits from QGraphicsItem,
>> > and
>> > this represents my graphical items in the scene.
>> >
>> > I tried using the QGraphicsItem.hoverEnterEvent and I also set the
>> > setAcceptHoverEvents(True), but I still can't enable that hover event. I
>> > also came across an event filter method but I'm not sure where to
>> > implement
>> > it.
>>
>> This seems to work here:
>>
>>
>> test.py:
>>
>> from sys import argv, exit
>>
>> from PyQt5.QtCore import Qt
>> from PyQt5.QtWidgets import QApplication
>> from PyQt5.QtWidgets import QGraphicsEllipseItem
>> from PyQt5.QtWidgets import QGraphicsScene
>> from PyQt5.QtWidgets import QGraphicsView
>> from PyQt5.QtWidgets import QMainWindow
>>
>>
>> class MyItem(QGraphicsEllipseItem):
>>
>>     def __init__(self, parent=None):
>>         super(MyItem, self).__init__(parent)
>>
>>         self.setRect(50, 50, 50, 50)
>>         self.setBrush(Qt.red)
>>         self.setAcceptHoverEvents(True)
>>
>>     def hoverEnterEvent(self, event):
>>         print('hover enter')
>>
>>     def hoverLeaveEvent(self, event):
>>         print('hover leave')
>>
>>
>> app = None
>>
>>
>> def main():
>>     global app
>>
>>     app = QApplication(argv)
>>
>>     scene = QGraphicsScene()
>>     scene.addItem(MyItem())
>>
>>     view = QGraphicsView()
>>     view.setScene(scene)
>>
>>     window = QMainWindow()
>>     window.setCentralWidget(view)
>>     window.show()
>>
>>     exit(app.exec_())
>>
>>
>> if __name__ == '__main__':
>>     main()
>>
>>
>> Hope that helps.
>>
>> Best regards,
>> Elvis
>>
>> >
>> > Should I install the event filter in the QGraphicsItem class, or the
>> > scene?
>> > I tried both and I'm still not getting the desired result. I want to be
>> > able
>> > to hover over all the items in the scene.
>> >
>> >
>> > class HoverEventFilter(QtCore.QObject):
>> >     def eventFilter(self, receiver, event):
>> >         if (event.type() == QtCore.QEvent.HoverEnter):
>> >             # this is for test purposes
>> >             print 'hover event'
>> >             return True
>> >         else:
>> >             # Call Base Class Method to Continue Normal Event Processing
>> >             return super(HoverEventFilter, self).eventFilter(receiver,
>> > event)
>> >
>> >
>> >
>> > --
>> > View this message in context:
>> > http://python.6.x6.nabble.com/Hover-Event-for-a-QGraphicsItem-tp5190283.html
>> > Sent from the PyQt mailing list archive at Nabble.com.
>> > _______________________________________________
>> > PyQt mailing list    [hidden email]
>> > https://www.riverbankcomputing.com/mailman/listinfo/pyqt
>> _______________________________________________
>> PyQt mailing list    [hidden email]
>> https://www.riverbankcomputing.com/mailman/listinfo/pyqt
>>
>> ________________________________
>> If you reply to this email, your message will be added to the discussion
>> below:
>>
>> http://python.6.x6.nabble.com/Hover-Event-for-a-QGraphicsItem-tp5190283p5190286.html
>> To unsubscribe from Hover Event for a QGraphicsItem, click here.
>> NAML
>
>
>
> ________________________________
> View this message in context: Re: Hover Event for a QGraphicsItem
>
> Sent from the PyQt mailing list archive at Nabble.com.
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt


More information about the PyQt mailing list