<div dir="ltr">
<p class=""><span class="">So i have a toggle bar and you add the switch object from there and also I can connect switch objects with lines but because of the <span class="" style="color:rgb(153,0,255)">self</span><span style="color:rgb(153,0,255)">.setAcceptHoverEvents(</span><span class="" style="color:rgb(153,0,255)">True</span><span style="color:rgb(153,0,255)">) </span><font color="#000000">command when I click the switch object and try to connect it to another one and then release my mouse the line does not land on that object. When I erase this command everything goes back to normal, but I still cant get the hoverEnterEvent function to be triggered.</font></span></p><p class=""><span class=""><font color="#000000"><br></font>
</span><span class=""># ---------------SWITCH OBJECT CLASS---------------#<br>
</span><font color="#9900ff"><span class=""><b>class </b></span><b>switch_Object</b>(QtGui.QGraphicsPixmapItem):<br>
<span class=""><b>def </b></span><span class="">__init__</span>(<span class="">self</span><span class="">, </span>parent=<span class="">None</span>):<br>
<span class="">super</span>(switch_Object<span class="">, </span><span class="">self</span>).<span class="">__init__</span>(parent)<br>
pixmap = QtGui.QPixmap(<span class="">"switch.png"</span>).scaled(<span class="">40</span><span class="">, </span><span class="">40</span><span class="">, </span>QtCore.Qt.KeepAspectRatio)<br>
<span class="">self</span>.setPixmap(pixmap)<br>
<span class="">self</span>.setFlag(QtGui.QGraphicsPixmapItem.ItemIsSelectable)<br>
<span class="">self</span>.setFlag(QtGui.QGraphicsPixmapItem.ItemIsMovable)<br>
<span class="">self</span>.setAcceptHoverEvents(<span class="">True</span>)<br>
<span class="">self</span>.switch_LinkedWithItems = []</font></p><p class=""><span class=""><br>
<font color="#9900ff"> </font></span><font color="#9900ff"><span class=""><b>def </b></span><b>hoverEnterEvent</b>(<span class="">self</span><span class="">, </span>event):<br>
<span class=""><b>print </b></span></font><span class=""><font color="#9900ff">'hello'</font><br>
</span><span class=""># ---------------SCENE CLASS---------------#<br>
</span><span class=""><b>class </b></span><b>graphicsScene</b>(QtGui.QGraphicsScene):<br>
<span class=""><b>def </b></span><span class="">__init__</span>(<span class="">self</span><span class="">, </span>parent=<span class="">None</span>):<br>
<span class="">super</span>(graphicsScene<span class="">, </span><span class="">self</span>).<span class="">__init__</span>(parent)<br>
<span class="">self</span>.i = <span class="">0</span><span class=""><br>
</span><span class="">self</span>.setSceneRect(-<span class="">180</span><span class="">, </span>-<span class="">90</span><span class="">, </span><span class="">360</span><span class="">, </span><span class="">180</span>)<br>
<span class="">self</span>.pen = QtGui.QPen(QtCore.Qt.black<span class="">, </span><span class="">3</span><span class="">, </span>QtCore.Qt.SolidLine)<br>
<span class="">self</span>.selected_Object = <span class="">None<br>
<br>
<br>
</span><span class=""><b>global </b></span>selected<br>
selected = <span class="">0<br>
<br>
</span><span class=""># ---------------MOUSE CLICK EVENT---------------#<br>
</span><span class=""><b>def </b></span><b>mousePressEvent</b>(<span class="">self</span><span class="">, </span>event):<br>
<span class=""><b>global </b></span>switch_cs<br>
<span class=""><b>global </b></span>selected<br>
<br>
<span class=""># Unselect the previous selected item when a new item is being selected<br>
<br>
</span><span class=""><b>for </b></span>selected_items <span class=""><b>in </b></span>selected_items_list:<br>
selected_items.setSelected(<span class="">False</span>)<br>
selected_items_list.remove(selected_items)<br>
<br>
<span class=""># Get position of the mouse click and set a rectangle for the collision test<br>
</span><span class="">self</span>.cursorPosition = event.scenePos()<br>
rect = QtCore.QRectF(<span class="">self</span>.cursorPosition.x()<span class="">,</span><span class="">self</span>.cursorPosition.y()<span class="">, </span><span class="">40</span><span class="">, </span><span class="">40</span>)<br>
intersection_list = <span class="">self</span>.items(rect)<br><br>
<br>
<span class=""># ---------------CREATE SWITCH ITEM---------------#<br>
</span><span class=""><b>if </b></span>switch_cs == <span class="">1 </span><span class=""><b>or </b></span>connectLine_cs == <span class="">1</span>:<br>
<span class=""># Test if there is a switch in the scene<br>
</span><span class=""><b>if </b></span><span class="">len</span>(switchObject_list) == <span class="">0</span>:<br>
<font color="#9900ff"> <span class="">self</span>.switch_item = switch_Object()</font><br>
switchObject_list.append(<span class="">self</span>.switch_item)<br>
<br>
<span class=""># Test if there is a collision between graphic items when selecting item<br>
</span><span class=""><b>elif </b></span><span class="">len</span>(intersection_list):<br>
<span class=""><b>for </b></span>switch <span class=""><b>in </b></span>switchObject_list:<br>
<span class=""><b>if </b></span>switch == intersection_list[<span class="">len</span>(intersection_list) - <span class="">1</span>]:<br>
<span class="">self</span>.selected_Object = switch<br>
selected_items_list.append(intersection_list[<span class="">len</span>(intersection_list) - <span class="">1</span>])<br>
<span class=""><b>if </b></span>connectLine_cs == <span class="">1 </span><span class=""><b>and </b></span><span class="">self</span>.selected_Object <span class=""><b>is not </b></span><span class="">None</span>:<br>
<span class="">self</span>.cursorStartPosition = event.scenePos()<br>
<span class="">self</span>.start = QtCore.QPoint(<span class="">self</span>.cursorStartPosition.x()<span class="">,</span><span class="">self</span>.cursorStartPosition.y())<br>
intersection_list[<span class="">len</span>(intersection_list) - <span class="">1</span>].setSelected(<span class="">True</span>)<br>
<br>
selected = <span class="">1<br>
<br>
</span><span class=""># If there is no collision then create new switch item<br>
</span><span class=""><b>else</b></span>:<br>
<span class="">self</span>.switch_item = switch_Object()<br>
selected = <span class="">0<br>
</span>switchObject_list.append(<span class="">self</span>.switch_item)<br>
<br>
<br>
<span class=""># ---------------MOUSE DRAG EVENT---------------#<br>
</span><span class=""><b>def </b></span><b>mouseMoveEvent</b>(<span class="">self</span><span class="">, </span>event):<br>
<br>
<span class=""><b>if </b></span>connectLine_cs == <span class="">1</span>:<br>
<span class=""><b>if </b></span><span class="">self</span>.start:<br>
<br>
<span class="">self</span>.cursorCurrentPosition = event.scenePos()<br>
<span class="">self</span>.current = QtCore.QPointF(<span class="">self</span>.cursorCurrentPosition.x()<span class="">,</span><span class="">self</span>.cursorCurrentPosition.y())<br>
<span class="">self</span>.draw_line(<span class="">self</span>.current)<br>
rect_for_link = QtCore.QRectF(<span class="">self</span>.cursorCurrentPosition.x()<span class="">,</span><span class="">self</span>.cursorCurrentPosition.y()<span class="">, </span><span class="">30</span><span class="">, </span><span class="">30</span>)<br>
<span class=""># Check for any collision when drawing the line<br>
</span><span class="">self</span>.intersection_forLink_list = <span class="">self</span>.items(rect_for_link)<br>
<br>
<br>
<span class=""># ---------------Draw line for animated effect---------------#<br>
</span><span class=""><b>def </b></span><b>draw_line</b>(<span class="">self</span><span class="">, </span>pos):<br>
<span class=""><b>try</b></span>:<br>
<span class=""># remove the old line if exists<br>
</span><span class="">self</span>.removeItem(<span class="">self</span>.line)<br>
<span class=""><b>except</b></span>:<br>
<span class=""><b>pass<br>
</b></span><span class="">self</span>.line = QtGui.QGraphicsLineItem(QtCore.QLineF(<span class="">self</span>.start<span class="">, </span>pos))<br>
<span class="">self</span>.line.setPen(<span class="">self</span>.pen)<br>
<span class="">self</span>.addItem(<span class="">self</span>.line)<br>
<br>
<span class=""><b>def </b></span><b>mouseReleaseEvent</b>(<span class="">self</span><span class="">, </span>event):<br>
<br>
<span class=""># ---------------ADD SWITCH ITEM TO SCENE IF SWITCH TOOLBAR HAS BEEN SELECTED---------------#<br>
</span><span class=""><b>if </b></span>switch_cs == <span class="">1</span>:<br>
<span class=""><b>if </b></span>selected == <span class="">0</span>:<br>
<font color="#9900ff"><span class="">self</span>.addItem(<span class="">self</span>.switch_item)</font><br>
<span class="">self</span>.switch_item.setPos(event.scenePos())<br>
<br>
<span class=""># ---------------ADD LINE ITEM TO SCENE IF LINE TOOLBAR HAS BEEN SELECTED---------------#<br>
</span><span class=""><b>if </b></span>connectLine_cs == <span class="">1</span>:<br>
lineItem_list.append(<span class="">self</span>.line)<br>
<span class=""><b>for </b></span>link <span class=""><b>in </b></span>lineItem_list:<br>
<span class="">self</span>.addItem(link)<br>
<span class=""># Check if there has been a collision with another item when drawing a line<br>
</span><span class=""><b>if </b></span><span class="">len</span>(<span class="">self</span>.intersection_forLink_list) > <span class="">0</span>:<br>
<span class=""># iterate in switchObject_list and check to see if the objects graphical item is the collided item</span><br>
<span class=""><b>for </b></span>switch_objects <span class=""><b>in </b></span>switchObject_list:<br>
<span class=""><b>if </b></span>switch_objects == <span class="">self</span>.intersection_forLink_list[<span class="">len</span>(<span class="">self</span>.intersection_forLink_list)-<span class="">1</span>]:<br>
<span class=""># Add the item that has been linked with the switch<br>
</span><span class="">self</span>.selected_Object.switch_LinkedWithItems.append(switch_objects)<br>
<span class="">self</span>.intersection_forLink_list = []</p></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, May 8, 2016 at 5:04 PM, Elvis Stansvik [via Python] <span dir="ltr"><<a href="/user/SendEmail.jtp?type=node&node=5190340&i=0" target="_top" rel="nofollow" link="external">[hidden email]</a>></span> wrote:<br><blockquote style='border-left:2px solid #CCCCCC;padding:0 1em' class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
2016-05-08 16:06 GMT+02:00 Elvis Stansvik <<a href="http:///user/SendEmail.jtp?type=node&node=5190337&i=0" rel="nofollow" link="external" target="_blank">[hidden email]</a>>:
<span class=""><div><div class='shrinkable-quote'><br>> 2016-05-08 15:24 GMT+02:00 sw33tz <<a href="http:///user/SendEmail.jtp?type=node&node=5190337&i=1" rel="nofollow" link="external" target="_blank">[hidden email]</a>>:
<br>>> By the way I'm not posting the entire code so sometimes I might forget to
<br>>> change the name of a variable or class but in my original code the format is
<br>>> written correctly.
<br>>
<br>> In general, <a href="http://sscce.org/" rel="nofollow" link="external" target="_blank">http://sscce.org/</a> is very good advice.
<br>>
<br>> When you send an example which the reader you seek help from cannot
<br>> run, it puts an extra burden on that person, because he/she will often
<br>> have to fix up the example and run it in order to help.
<br>>
<br>> I'm not going to borther doing that this time, but see my comments
<br>> inline below, which are at best educated guesses since the code you
<br>> have given won't run.
</div></div></span><span class="">That sounded a bit harsch, I'm sorry for that.
<br><br>But it's so much easier for us to help if you send the code exactly as
<br>it is, even if it's only parts of it.
<br><br>The code you sent in your last email could not possibly have been the
<br>one you were trying to run, since in your mouseReleaseEvent you try to
<br>access self.graphics_item.graphics_pixItem, which doesn't even exist
<br>anymore in the graphics_Object class, so you would have gotten an
<br>error.
<br><br>I know it's a bit of work cut your code down a minimal working example
<br>that shows the problem you're having, but doing so makes it much
<br>easier to help, and the chances are great that you find the problem on
<br>your own when doing so.
<br><br>Elvis
<br></span><div><br>>
<br>>>
<br><div><div class="h5">>> On Sun, May 8, 2016 at 4:34 PM, Nesibe Yavuz <[hidden email]> wrote:
<div class='shrinkable-quote'><br>>>>
<br>>>> So I rewrote what you suggested but I'm still not being able to detect the
<br>>>> hover event...why is this working?
<br>>>>
<br>>>> class graphics_Object(QtGui.QGraphicsPixmapItem):
<br>>>> def __init__(self, parent=None):
<br>>>> super(graphics_Object, self).__init__(parent)
<br>>>> pixmap = QtGui.QPixmap("switch.png").scaled(40, 40,
<br>>>> QtCore.Qt.KeepAspectRatio)
<br>>>> self.setPixmap(pixmap)
<br>>>> self.setFlag(QtGui.QGraphicsPixmapItem.ItemIsSelectable)
<br>>>> self.setFlag(QtGui.QGraphicsPixmapItem.ItemIsMovable)
<br>>>> self.setAcceptHoverEvents(True)
<br>>>> self.switch_LinkedWithItems = []
<br>>>> self.switch_mininet_name = ''
<br>>>>
<br>>>> def hoverEnterEvent(self, event):
<br>>>> print 'hello'
<br>>>>
<br>>>> def hoverLeaveEvent(self, event):
<br>>>> print 'goodbye'
<br>>>>
<br>>>> class graphicsScene(QtGui.QGraphicsScene):
<br>>>> def __init__(self, parent=None):
<br>>>> super(graphicsScene, self).__init__(parent)
<br>>>>
<br>>>> def mousePressEvent(self, event):
<br>>>> self.graphics_item = graphics_Object()
<br>>>>
<br>>>> def mouseReleaseEvent(self, event)
<br>>>> self.addItem(self.graphics_item.graphics_pixItem)
<br>>
<br>> You're still doing something strange here if this is indeed your
<br>> current code. Why are you (seemingly) keeping one graphics item as an
<br>> attribute of another? That doesn't make sense, and it's probably still
<br>> the case that the item you're adding is not of type graphics_Object,
<br>> which is the class where you are overriding hoverEnterEvent /
<br>> hoverLeaveEvent.
<br>>
<br>> From the looks of it, you've fixed everything but the issue I pointed
<br>> out as the main one.
<br>>
<br>> Elvis
<br>>
<br>>>> self.graphics_item.setPos(event.scenePos())
<br>>>>
<br>>>>
<br>>>> class Form(QtGui.QMainWindow):
<br>>>> def __init__(self):
<br>>>> super(Form, self).__init__()
<br>>>> self.ui = uic.loadUi('form.ui')
<br>>>>
<br>>>> self.scene = graphicsScene()
<br>>>> self.setMouseTracking(True)
<br>>>>
<br>>>>
<br>>>>
<br>>>>
<br>>>> On Sun, May 8, 2016 at 1:54 PM, Elvis Stansvik [via Python] <[hidden
<br>>>> email]> wrote:
<br>>>>>
<br>>>>> 2016-05-07 23:05 GMT+02:00 sw33tz <[hidden email]>:
<br>>>>> > Thanks for replying...I've updated my code but I still cant get it to
<br>>>>> > work:
<br>>>>>
<br>>>>> I've now had a closer look at your code. There are several problems
<br>>>>> with which I point out inline below.
<br>>>>>
<br>>>>> >
<br>>>>> > class graphics_Object(QtGui.QGraphicsPixmapItem):
<br>>>>> > def __init__(self, parent=None):
<br>>>>> > super(switch_Object, self).__init__(parent)
<br>>>>>
<br>>>>> This, as I already mentioned, should be graphics_Object and not
<br>>>>> graphics_Object in the call to super(..).
<br>>>>>
<br>>>>> > pixmap = QtGui.QPixmap("item.png")
<br>>>>> > self.graphics_pixItem =
<br>>>>> > QtGui.QGraphicsPixmapItem(pixmap.scaled(40,
<br>>>>> > 40, QtCore.Qt.KeepAspectRatio))
<br>>>>> >
<br>>>>> >
<br>>>>> > self.graphics_pixItem.setFlag(QtGui.QGraphicsPixmapItem.ItemIsSelectable)
<br>>>>> >
<br>>>>> > self.graphics_pixItem.setFlag(QtGui.QGraphicsPixmapItem.ItemIsMovable)
<br>>>>> > self.graphics_pixItem.setAcceptHoverEvents(True)
<br>>>>> >
<br>>>>> >
<br>>>>> > def hoverEnterEvent(self, event):
<br>>>>> > print 'hello'
<br>>>>> Here's your real problem, you are overriding hoverEnterEvent in the
<br>>>>> graphics_Object class, but the item that you are later adding to the
<br>>>>> scene is self.graphics_pixItem, which is of type
<br>>>>> QtGui.QGraphicsPixmapItem.
<br>>>>>
<br>>>>> >
<br>>>>> > class graphicsScene(QtGui.QGraphicsScene):
<br>>>>> > def __init__(self, parent=None):
<br>>>>> > super(graphicsScene, self).__init__(parent)
<br>>>>> >
<br>>>>> > def mousePressEvent(self, event):
<br>>>>> > self.graphics_item = graphics_Object()
<br>>>>> > def mouseReleaseEvent(self, event)
<br>>>>>
<br>>>>> There's a missing ':' above.
<br>>>>>
<br>>>>> > self.addItem(self.self.graphics_item.graphics_pixItem)
<br>>>>>
<br>>>>> Apart from "self.self" which makes no sense (it should be just
<br>>>>> "self.graphics_item"), here you are adding graphics_pixItem to the
<br>>>>> scene, which is of type QtGui.QGraphicsPixmapItem, not of type
<br>>>>> graphics_Object.
<br>>>>>
<br>>>>> You seem to have a misunderstanding of how inheritance vs composition
<br>>>>> works.
<br>>>>>
<br>>>>> >
<br>>>>> > self.graphics_item.self.graphics_pixItem.setPos(event.scenePos())
<br>>>>>
<br>>>>> Here's again a strange use of "self" which will give an error. `self`
<br>>>>> is only available from within the class itself, the
<br>>>>> "self.graphics_item.self" here should be just "self.graphics_item".
<br>>>>>
<br>>>>> Here's roughly how I would structure the code, given what I've seen in
<br>>>>> your example (Python 2, PyQt 4, since I'm guessing that's what you
<br>>>>> use):
<br>>>>>
<br>>>>>
<br>>>>> main.py:
<br>>>>>
<br>>>>> from sys import argv, exit
<br>>>>>
<br>>>>> from PyQt4.QtGui import QApplication
<br>>>>> from PyQt4.QtGui import QGraphicsItem
<br>>>>> from PyQt4.QtGui import QGraphicsPixmapItem
<br>>>>> from PyQt4.QtGui import QGraphicsScene
<br>>>>> from PyQt4.QtGui import QMainWindow
<br>>>>> from PyQt4.QtGui import QPixmap
<br>>>>> from PyQt4.QtCore import Qt
<br>>>>> from PyQt4.uic import loadUi
<br>>>>>
<br>>>>>
<br>>>>> class Item(QGraphicsPixmapItem):
<br>>>>>
<br>>>>> def __init__(self, parent=None):
<br>>>>> super(Item, self).__init__(parent)
<br>>>>>
<br>>>>> pixmap = QPixmap('item.png').scaled(40, 40, Qt.KeepAspectRatio)
<br>>>>>
<br>>>>> self.setPixmap(pixmap)
<br>>>>> self.setFlag(QGraphicsItem.ItemIsSelectable)
<br>>>>> self.setFlag(QGraphicsItem.ItemIsMovable)
<br>>>>> self.setAcceptHoverEvents(True)
<br>>>>>
<br>>>>> def hoverEnterEvent(self, event):
<br>>>>> print('hover enter') # Do whatever you need to do here
<br>>>>>
<br>>>>> def hoverLeaveEvent(self, event):
<br>>>>> print('hover leave') # Do whatever you need to do here
<br>>>>>
<br>>>>>
<br>>>>> class Scene(QGraphicsScene):
<br>>>>>
<br>>>>> def __init__(self, parent=None):
<br>>>>> super(Scene, self).__init__(parent)
<br>>>>>
<br>>>>> def mouseReleaseEvent(self, event):
<br>>>>> item = Item()
<br>>>>> item.setPos(event.scenePos())
<br>>>>> self.addItem(item)
<br>>>>>
<br>>>>>
<br>>>>> class Window(QMainWindow):
<br>>>>>
<br>>>>> def __init__(self, parent=None):
<br>>>>> super(Window, self).__init__(parent)
<br>>>>>
<br>>>>> loadUi('window.ui'), self)
<br>>>>>
<br>>>>> self.scene = Scene(self)
<br>>>>> self.view.setScene(self.scene)
<br>>>>>
<br>>>>>
<br>>>>> app = None
<br>>>>>
<br>>>>>
<br>>>>> def main():
<br>>>>> global app
<br>>>>>
<br>>>>> app = QApplication(argv)
<br>>>>>
<br>>>>> window = Window()
<br>>>>> window.show()
<br>>>>>
<br>>>>> exit(app.exec_())
<br>>>>>
<br>>>>>
<br>>>>> if __name__ == '__main__':
<br>>>>> main()
<br>>>>>
<br>>>>>
<br>>>>> window.ui:
<br>>>>>
<br>>>>> <?xml version="1.0" encoding="UTF-8"?>
<br>>>>> <ui version="4.0">
<br>>>>> <class>Window</class>
<br>>>>> <widget class="QMainWindow" name="Window">
<br>>>>> <property name="geometry">
<br>>>>> <rect>
<br>>>>> <x>0</x>
<br>>>>> <y>0</y>
<br>>>>> <width>800</width>
<br>>>>> <height>600</height>
<br>>>>> </rect>
<br>>>>> </property>
<br>>>>> <property name="windowTitle">
<br>>>>> <string>MainWindow</string>
<br>>>>> </property>
<br>>>>> <widget class="QWidget" name="centralwidget">
<br>>>>> <layout class="QVBoxLayout" name="verticalLayout">
<br>>>>> <item>
<br>>>>> <widget class="QGraphicsView" name="view"/>
<br>>>>> </item>
<br>>>>> </layout>
<br>>>>> </widget>
<br>>>>> </widget>
<br>>>>> <resources/>
<br>>>>> <connections/>
<br>>>>> </ui>
<br>>>>>
<br>>>>>
<br>>>>> Hope you can use that as starting point / inspiration and figure out
<br>>>>> things from there.
<br>>>>>
<br>>>>> Elvis
<br>>>>>
<br>>>>> >
<br>>>>> > class Form(QtGui.QMainWindow):
<br>>>>>
<br>>>>> > def __init__(self):
<br>>>>> > super(Form, self).__init__()
<br>>>>> > self.ui = uic.loadUi('form.ui')
<br>>>>> >
<br>>>>> > self.scene = graphicsScene()
<br>>>>> > self.ui.view.setScene(self.scene)
<br>>>>> >
<br>>>>> > self.setMouseTracking(True)
<br>>>>> >
<br>>>>> >
<br>>>>> > On Sat, May 7, 2016 at 10:55 PM, Elvis Stansvik [via Python] <[hidden
<br>>>>>
<br>>>>> > email]> wrote:
<br>>>>> >>
<br>>>>> >> Hi Nesibe,
<br>>>>> >>
<br>>>>> >> 2016-05-07 19:46 GMT+02:00 sw33tz <[hidden email]>:
<br>>>>> >> > I want some small text to pop up when I have my curser over a
<br>>>>> >> > QGraphicsItem
<br>>>>> >> > in my QGraphicsScene. I have a class that inherits from
<br>>>>> >> > QGraphicsItem,
<br>>>>> >> > and
<br>>>>> >> > this represents my graphical items in the scene.
<br>>>>> >> >
<br>>>>> >> > I tried using the QGraphicsItem.hoverEnterEvent and I also set the
<br>>>>> >> > setAcceptHoverEvents(True), but I still can't enable that hover
<br>>>>> >> > event. I
<br>>>>> >> > also came across an event filter method but I'm not sure where to
<br>>>>> >> > implement
<br>>>>> >> > it.
<br>>>>> >>
<br>>>>> >> This seems to work here:
<br>>>>> >>
<br>>>>> >>
<br>>>>> >> test.py:
<br>>>>> >>
<br>>>>> >> from sys import argv, exit
<br>>>>> >>
<br>>>>> >> from PyQt5.QtCore import Qt
<br>>>>> >> from PyQt5.QtWidgets import QApplication
<br>>>>> >> from PyQt5.QtWidgets import QGraphicsEllipseItem
<br>>>>> >> from PyQt5.QtWidgets import QGraphicsScene
<br>>>>> >> from PyQt5.QtWidgets import QGraphicsView
<br>>>>> >> from PyQt5.QtWidgets import QMainWindow
<br>>>>> >>
<br>>>>> >>
<br>>>>> >> class MyItem(QGraphicsEllipseItem):
<br>>>>> >>
<br>>>>> >> def __init__(self, parent=None):
<br>>>>> >> super(MyItem, self).__init__(parent)
<br>>>>> >>
<br>>>>> >> self.setRect(50, 50, 50, 50)
<br>>>>> >> self.setBrush(Qt.red)
<br>>>>> >> self.setAcceptHoverEvents(True)
<br>>>>> >>
<br>>>>> >> def hoverEnterEvent(self, event):
<br>>>>> >> print('hover enter')
<br>>>>> >>
<br>>>>> >> def hoverLeaveEvent(self, event):
<br>>>>> >> print('hover leave')
<br>>>>> >>
<br>>>>> >>
<br>>>>> >> app = None
<br>>>>> >>
<br>>>>> >>
<br>>>>> >> def main():
<br>>>>> >> global app
<br>>>>> >>
<br>>>>> >> app = QApplication(argv)
<br>>>>> >>
<br>>>>> >> scene = QGraphicsScene()
<br>>>>> >> scene.addItem(MyItem())
<br>>>>> >>
<br>>>>> >> view = QGraphicsView()
<br>>>>> >> view.setScene(scene)
<br>>>>> >>
<br>>>>> >> window = QMainWindow()
<br>>>>> >> window.setCentralWidget(view)
<br>>>>> >> window.show()
<br>>>>> >>
<br>>>>> >> exit(app.exec_())
<br>>>>> >>
<br>>>>> >>
<br>>>>> >> if __name__ == '__main__':
<br>>>>> >> main()
<br>>>>> >>
<br>>>>> >>
<br>>>>> >> Hope that helps.
<br>>>>> >>
<br>>>>> >> Best regards,
<br>>>>> >> Elvis
<br>>>>> >>
<br>>>>> >> >
<br>>>>> >> > Should I install the event filter in the QGraphicsItem class, or the
<br>>>>> >> > scene?
<br>>>>> >> > I tried both and I'm still not getting the desired result. I want to
<br>>>>> >> > be
<br>>>>> >> > able
<br>>>>> >> > to hover over all the items in the scene.
<br>>>>> >> >
<br>>>>> >> >
<br>>>>> >> > class HoverEventFilter(QtCore.QObject):
<br>>>>> >> > def eventFilter(self, receiver, event):
<br>>>>> >> > if (event.type() == QtCore.QEvent.HoverEnter):
<br>>>>> >> > # this is for test purposes
<br>>>>> >> > print 'hover event'
<br>>>>> >> > return True
<br>>>>> >> > else:
<br>>>>> >> > # Call Base Class Method to Continue Normal Event
<br>>>>> >> > Processing
<br>>>>> >> > return super(HoverEventFilter,
<br>>>>> >> > self).eventFilter(receiver,
<br>>>>> >> > event)
<br>>>>> >> >
<br>>>>> >> >
<br>>>>> >> >
<br>>>>> >> > --
<br>>>>> >> > View this message in context:
<br>>>>> >> >
<br>>>>> >> > <a href="http://python.6.x6.nabble.com/Hover-Event-for-a-QGraphicsItem-tp5190283.html" rel="nofollow" link="external" target="_blank">http://python.6.x6.nabble.com/Hover-Event-for-a-QGraphicsItem-tp5190283.html</a><br>>>>> >> > Sent from the PyQt mailing list archive at Nabble.com.
<br>>>>> >> > _______________________________________________
<br>>>>> >> > PyQt mailing list [hidden email]
<br>>>>> >> > <a href="https://www.riverbankcomputing.com/mailman/listinfo/pyqt" rel="nofollow" link="external" target="_blank">https://www.riverbankcomputing.com/mailman/listinfo/pyqt</a><br>>>>> >> _______________________________________________
<br>>>>> >> PyQt mailing list [hidden email]
<br>>>>> >> <a href="https://www.riverbankcomputing.com/mailman/listinfo/pyqt" rel="nofollow" link="external" target="_blank">https://www.riverbankcomputing.com/mailman/listinfo/pyqt</a><br>>>>> >>
<br>>>>> >> ________________________________
<br>>>>> >> If you reply to this email, your message will be added to the
<br>>>>> >> discussion
<br>>>>> >> below:
<br>>>>> >>
<br>>>>> >>
<br>>>>> >> <a href="http://python.6.x6.nabble.com/Hover-Event-for-a-QGraphicsItem-tp5190283p5190286.html" rel="nofollow" link="external" target="_blank">http://python.6.x6.nabble.com/Hover-Event-for-a-QGraphicsItem-tp5190283p5190286.html</a><br>>>>> >> To unsubscribe from Hover Event for a QGraphicsItem, click here.
<br>>>>> >> NAML
<br>>>>> >
<br>>>>> >
<br>>>>> >
<br>>>>> > ________________________________
<br>>>>> > View this message in context: Re: Hover Event for a QGraphicsItem
<br>>>>> >
<br>>>>> > Sent from the PyQt mailing list archive at Nabble.com.
<br>>>>> >
<br>>>>> > _______________________________________________
<br>>>>> > PyQt mailing list [hidden email]
<br>>>>> > <a href="https://www.riverbankcomputing.com/mailman/listinfo/pyqt" rel="nofollow" link="external" target="_blank">https://www.riverbankcomputing.com/mailman/listinfo/pyqt</a><br>>>>> _______________________________________________
<br>>>>> PyQt mailing list [hidden email]
<br>>>>> <a href="https://www.riverbankcomputing.com/mailman/listinfo/pyqt" rel="nofollow" link="external" target="_blank">https://www.riverbankcomputing.com/mailman/listinfo/pyqt</a><br>>>>>
<br>>>>> ________________________________
<br>>>>> If you reply to this email, your message will be added to the discussion
<br>>>>> below:
<br>>>>>
<br>>>>> <a href="http://python.6.x6.nabble.com/Hover-Event-for-a-QGraphicsItem-tp5190283p5190318.html" rel="nofollow" link="external" target="_blank">http://python.6.x6.nabble.com/Hover-Event-for-a-QGraphicsItem-tp5190283p5190318.html</a><br>>>>> To unsubscribe from Hover Event for a QGraphicsItem, click here.
<br>>>>> NAML
<br>>>>
<br>>>>
<br>>>
<br>>>
<br>>> ________________________________
<br>>> View this message in context: Re: Hover Event for a QGraphicsItem
<br>>> Sent from the PyQt mailing list archive at Nabble.com.
<br>>>
<br>>> _______________________________________________
<br>>> PyQt mailing list <a href="http:///user/SendEmail.jtp?type=node&node=5190337&i=2" rel="nofollow" link="external" target="_blank">[hidden email]</a>
<br>>> <a href="https://www.riverbankcomputing.com/mailman/listinfo/pyqt" rel="nofollow" link="external" target="_blank">https://www.riverbankcomputing.com/mailman/listinfo/pyqt</a></div></div></div><div><div class="h5">_______________________________________________
</div>PyQt mailing list <a href="http:///user/SendEmail.jtp?type=node&node=5190337&i=3" rel="nofollow" link="external" target="_blank">[hidden email]</a>
<br><a href="https://www.riverbankcomputing.com/mailman/listinfo/pyqt" rel="nofollow" link="external" target="_blank">https://www.riverbankcomputing.com/mailman/listinfo/pyqt</a>
<br>
<br>
<hr noshade size="1" color="#cccccc">
</div></div><div style="color:#444;font:12px tahoma,geneva,helvetica,arial,sans-serif"><div><div class="h5">
<div style="font-weight:bold">If you reply to this email, your message will be added to the discussion below:</div>
</div></div><a href="http://python.6.x6.nabble.com/Hover-Event-for-a-QGraphicsItem-tp5190283p5190337.html" target="_blank" rel="nofollow" link="external">http://python.6.x6.nabble.com/Hover-Event-for-a-QGraphicsItem-tp5190283p5190337.html</a>
</div><div class="HOEnZb"><div class="h5">
<div style="color:#666;font:11px tahoma,geneva,helvetica,arial,sans-serif;margin-top:.4em;line-height:1.5em">
To unsubscribe from Hover Event for a QGraphicsItem, <a href="" target="_blank" rel="nofollow" link="external">click here</a>.<br>
<a href="http://python.6.x6.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml" rel="nofollow" style="font:9px serif" target="_blank" link="external">NAML</a>
</div></div></div></blockquote></div><br></div>
<br/><hr align="left" width="300" />
View this message in context: <a href="http://python.6.x6.nabble.com/Hover-Event-for-a-QGraphicsItem-tp5190283p5190340.html">Re: Hover Event for a QGraphicsItem</a><br/>
Sent from the <a href="http://python.6.x6.nabble.com/PyQt-f1792048.html">PyQt mailing list archive</a> at Nabble.com.<br/>