Mouseover event for a PyQT5 Label
Asked Answered
T

3

13

I want that if I move my mouse over the label with text stop on it then it should change the value of a variable Stop to True so that I may pause/stop my program.

I have looked the code at Mouseover event filter for a PyQT Label and tried to run it, but nothing is being shown up.

The code is:

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5 import *
import sys

class mouseoverEvent(QtCore.QObject):
    def __init__(self, parent):
        super(mouseoverEvent, self).__init__(parent)
        self.initUI()
    def eventFilter(self, object, event):
        if event.type() == QtCore.QEvent.MouseMove:
            print( "mousemove!")
            return True
        else:
            return False

    def initUI(self):
        self.filter = mouseoverEvent(self)
        self.label.installEventFilter(self.filter)
        self.lbl=QLabel(self)
        self.lbl.setText(self,"hellojjj")
        self.setGeometry(1000, 30, 300, 100)
        self.setWindowTitle('QLineEdit')

        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = mouseoverEvent()
    sys.exit(app.exec_())
Tombolo answered 21/7, 2018 at 13:8 Comment(0)
H
9
  1. If you've already imported

    from PyQt5.QtWidgets import *
    from PyQt5.QtGui import *
    from PyQt5.QtCore import *
    

    there is no need to

    from PyQt5 import *
    
  2. Once you've imported QtCore, you no longer need to call its functions/classes with 'QtCore.QEvent', Just using QEvent is fine

  3. I believe the question you linked to used PyQt4. In PyQt5, the initialization procedure for the class changed

The code below should work.

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys

class mouseoverEvent(QWidget):

    def __init__(self):
        super().__init__()
        self.stop = False # your 'stop' variable
        self.initUI()

    def initUI(self):
        self.lbl=QLabel(self)
        self.lbl.setText("Hover over me to stop the program")
        self.lbl.installEventFilter(self)
        self.setGeometry(1000, 30, 300, 100)
        self.setWindowTitle('QLineEdit')

        self.show()

    def eventFilter(self, object, event):
        if event.type() == QEvent.Enter:
            print("Mouse is over the label")
            self.stop = True
            print('program stop is', self.stop)
            return True
        elif event.type() == QEvent.Leave:
            print("Mouse is not over the label")
            self.stop = False
            print('program stop is', self.stop)
        return False


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = mouseoverEvent()
    sys.exit(app.exec_())

if you only want the stop to activate over a label with certain text change your eventFilter function to:

def eventFilter(self, object, event):
    if hasattr(object, 'text'): #check to see if the object has text, otherwise if you hover over something without text, PyQt will return an error
        if object.text() == "Hover over me to stop the program":
            if event.type() == QEvent.Enter:
                print("Mouse is over the label")
                self.stop = True
                print('program stop is', self.stop)
                return True
            elif event.type() == QEvent.Leave:
                print("Mouse is not over the label")
                self.stop = False
                print('program stop is', self.stop)
    return False
Heracles answered 21/7, 2018 at 19:57 Comment(0)
M
9

Please find the reference:

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *


class Label(QLabel):
    def __init__(self, *args, **kwargs):
        QLabel.__init__(self, *args, **kwargs)

    def enterEvent(self, event):
        print("hovered")

    def leaveEvent(self, event):
        print("left")
Mika answered 18/7, 2019 at 6:29 Comment(0)
P
6

Remember that Python is very flexible, so there is no need to subclass QLabel.

For example:

def enter(event):
    print("Enter")

def leave(label):
    print("Leave")

label = QLabel("Hello")
label.leaveEvent = leave
label.enterEvent = enter

Or you can use lambdas:

label = QLabel("Hello")
label.leaveEvent = lambda e: print("Leave")
label.enterEvent = lambda e: print("Enter")
Phyllys answered 23/2, 2021 at 10:30 Comment(1)
While this generally works for simple cases and basic widgets, it's also usually discouraged, and especially for event handlers: 1. it can cause bugs that are difficult to debug (especially if the overwritten function is used on more instances); 2. doesn't allow direct calls to the base implementation, which many widgets do require in order to work as expected. The proper and safe way is always to subclass the widget class or use an event filter.Truthful

© 2022 - 2024 — McMap. All rights reserved.