<div dir="ltr"><p style="font-size:12.8px">So I rewrote what you suggested but I'm still not being able to detect the hover event...why is this working?</p><p style="font-size:12.8px"><span class="im"><b>class graphics</b><b>_Object</b>(QtGui.QGraphicsPixmapItem):<br>    <b>def </b>__init__(self, parent=None):<br></span>        super(graphics_Object, self).__init__(parent)<br>        pixmap = QtGui.QPixmap("switch.png").scaled(40, 40, 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>    <b>def </b><b>hoverEnterEvent</b>(self, event):<br>        <b>print </b>'hello'<br><br>    <b>def </b><b>hoverLeaveEvent</b>(self, event):<br>        <b>print </b>'goodbye'</p><p style="font-size:12.8px"></p><p style="font-size:12.8px"><span class="im"><b>class </b><b>graphicsScene</b>(QtGui.QGraphicsScene):<br>    <b>def </b>__init__(self, parent=None):<br>        super(graphicsScene, self).__init__(parent)<br><br>    <b>def </b><b>mousePressEvent</b>(self, event):<br>        self.graphics_item = graphics_Object()<br><br>    <b>def </b><b>mouseReleaseEvent</b>(self, event)<br></span>        self.addItem(self.graphics_item.graphics_pixItem)<br>        self.graphics_item.setPos(event.scenePos())<span class="im"><br><br><br><b>class </b><b>Form</b>(QtGui.QMainWindow):<br>    <b>def </b>__init__(self):<br>        super(Form, self).__init__()<br>        self.ui = uic.loadUi('form.ui')<br><br>        self.scene = graphicsScene()<br></span>        self.setMouseTracking(True)<br></p><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, May 8, 2016 at 4:38 PM, Nesibe Yavuz <span dir="ltr"><<a href="/user/SendEmail.jtp?type=node&node=5190333&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"><div dir="ltr">By the way I'm not posting the entire code so sometimes I might forget to change the name of a variable or class but in my original code the format is written correctly.</div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Sun, May 8, 2016 at 4:34 PM, Nesibe Yavuz <span dir="ltr"><<a href="/user/SendEmail.jtp?type=node&node=5190333&i=1" 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"><div dir="ltr">







<p><span>







</span></p><p>So I rewrote what you suggested but I'm still not being able to detect the hover event...why is this working?</p><p><span><span><b>class graphics</b></span><b>_Object</b>(QtGui.QGraphicsPixmapItem):<br>
    <span><b>def </b></span><span>__init__</span>(<span>self</span><span>, </span>parent=<span>None</span>):<br></span>
        <span>super</span>(graphics_Object<span>, </span><span>self</span>).<span>__init__</span>(parent)<br>
        pixmap = QtGui.QPixmap(<span>"switch.png"</span>).scaled(<span>40</span><span>, </span><span>40</span><span>, </span>QtCore.Qt.KeepAspectRatio)<br>
        <span>self</span>.setPixmap(pixmap)<br>
        <span>self</span>.setFlag(QtGui.QGraphicsPixmapItem.ItemIsSelectable)<br>
        <span>self</span>.setFlag(QtGui.QGraphicsPixmapItem.ItemIsMovable)<br>
        <span>self</span>.setAcceptHoverEvents(<span>True</span>)<br>
        <span>self</span>.switch_LinkedWithItems = []<br>
        <span>self</span>.switch_mininet_name = <span>''<br>
<br>
    </span><span><b>def </b></span><b>hoverEnterEvent</b>(<span>self</span><span>, </span>event):<br>
        <span><b>print </b></span><span>'hello'<br>
<br>
    </span><span><b>def </b></span><b>hoverLeaveEvent</b>(<span>self</span><span>, </span>event):<br>
        <span><b>print </b></span><span>'goodbye'</span></p><p><span>







</span></p><p><span><span><b>class </b></span><b>graphicsScene</b>(QtGui.QGraphicsScene):<br>
    <span><b>def </b></span><span>__init__</span>(<span>self</span><span>, </span>parent=<span>None</span>):<br>
        <span>super</span>(graphicsScene<span>, </span><span>self</span>).<span>__init__</span>(parent)<br>
<br>
    <span><b>def </b></span><b>mousePressEvent</b>(<span>self</span><span>, </span>event):<br>
        <span>self</span>.graphics_item = graphics_Object()<br>
<br>
    <span><b>def </b></span><b>mouseReleaseEvent</b>(<span>self</span><span>, </span>event)<br></span>
        <span>self</span>.addItem(<span>self</span>.graphics_item.graphics_pixItem)<br>
        <span>self</span>.graphics_item.setPos(event.scenePos())<span><br>
<br>
<br>
<span><b>class </b></span><b>Form</b>(QtGui.QMainWindow):<br>
    <span><b>def </b></span><span>__init__</span>(<span>self</span>):<br>
        <span>super</span>(Form<span>, </span><span>self</span>).<span>__init__</span>()<br>
        <span>self</span>.ui = uic.loadUi(<span>'form.ui'</span>)<br>
<br>
        <span>self</span>.scene = graphicsScene()<br></span>
        <span>self</span>.setMouseTracking(<span>True</span>)<br>
</p><p><br></p><p><br></p></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, May 8, 2016 at 1:54 PM, Elvis Stansvik [via Python] <span dir="ltr"><<a href="/user/SendEmail.jtp?type=node&node=5190333&i=2" 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"><div><div><span>

        2016-05-07 23:05 GMT+02:00 sw33tz <<a href="http:///user/SendEmail.jtp?type=node&node=5190318&i=0" rel="nofollow" link="external" target="_blank">[hidden email]</a>>:
<br>> Thanks for replying...I've updated my code but I still cant get it to work:
<br><br></span><span>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></span><span>> class graphics_Object(QtGui.QGraphicsPixmapItem):
<br>>     def __init__(self, parent=None):
<br>>         super(switch_Object, self).__init__(parent)
<br><br></span><span>This, as I already mentioned, should be graphics_Object and not
<br>graphics_Object in the call to super(..).
<br></span><span><div><div class='shrinkable-quote'><br>>         pixmap = QtGui.QPixmap("item.png")
<br>>         self.graphics_pixItem = QtGui.QGraphicsPixmapItem(pixmap.scaled(40,
<br>> 40, QtCore.Qt.KeepAspectRatio))
<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'
</div></div></span><span>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></span><span>> 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></span><span>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 works.
<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></span><span>from sys import argv, exit
<br><br></span><span>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></span>        self.setAcceptHoverEvents(True)
<br><br>    def hoverEnterEvent(self, event):
<br><span>        print('hover enter')  # Do whatever you need to do here
<br><br></span>    def hoverLeaveEvent(self, event):
<br><span>        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></span><span>app = None
<br><br><br>def main():
<br>    global app
<br><br>    app = QApplication(argv)
<br><br></span>    window = Window()
<br><span>    window.show()
<br><br>    exit(app.exec_())
<br><br><br>if __name__ == '__main__':
<br>    main()
<br><br><br></span><div><div>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></div></div><div><br>>
<br><span>> class Form(QtGui.QMainWindow):
<div class='shrinkable-quote'><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>>
</div></span><div><div>> On Sat, May 7, 2016 at 10:55 PM, Elvis Stansvik [via Python] <[hidden
<div class='shrinkable-quote'><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 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 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 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 Processing
<br>>> >             return super(HoverEventFilter, self).eventFilter(receiver,
<br>>> > event)
<br>>> >
<br>>> >
<br>>> >
<br>>> > --
<br>>> > View this message in context:
<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 discussion
<br>>> below:
<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>>
</div></div></div>> ________________________________
<br><span>> View this message in context: Re: Hover Event for a QGraphicsItem
<br>>
<br></span><span>> 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=5190318&i=1" 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></span></div><span>_______________________________________________
<br>PyQt mailing list    <a href="http:///user/SendEmail.jtp?type=node&node=5190318&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>

        
        
        
        <br>
        <br>
        <hr noshade size="1" color="#cccccc">
        </span></div></div><div style="color:#444;font:12px tahoma,geneva,helvetica,arial,sans-serif"><div><div><span>
                <div style="font-weight:bold">If you reply to this email, your message will be added to the discussion below:</div>
                </span></div></div><a href="http://python.6.x6.nabble.com/Hover-Event-for-a-QGraphicsItem-tp5190283p5190318.html" target="_blank" rel="nofollow" link="external">http://python.6.x6.nabble.com/Hover-Event-for-a-QGraphicsItem-tp5190283p5190318.html</a>
        </div><span><div><div>
        <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></span></blockquote></div><br></div>
</blockquote></div><br></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-tp5190283p5190333.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/>