Issue
I'm trying to color a label whenever the mouse passes over the program or "MainWindow" and the program only changes the color if I click and drag, any other operation like just clicking or just dragging does not trigger the "mouseMoveEvent" function
import utils
from utils.INTERFACE_FUNCIONALITIES.FOOD_DEV import Ui_MainWindow
from utils import interface_func as i_fun
from utils import template
p = template.Product()
c = template.Client()
i_fun = i_fun.Interface()
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent=parent)
self.setupUi(self)
self.show()
self.setMouseTracking(True)
def mouseMoveEvent(self, event):
self.rmm_lb_logo.setStyleSheet("background-color: red;") <-- TRY COLOR
#self.rmm_lb_logo.setText('Mouse coords: ( %d : %d )' % (event.x(), event.y()))
import sys
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MainWindow()
p.view()
c.view()
i_fun.functionalties(w)
w.show()
sys.exit(app.exec_())
Solution
The mouse tracking won't be very useful for this specific situation, because the objective is to get mouse events within the boundaries of the window, not that of the widget. Also, the move event is only received when actively moving the mouse, but this will fail the result if the window becomes under the mouse for any reason (for instance, another window that was above it becomes hidden or closed). Finally, the mouse tracking doesn't allow to know when the mouse actually leaves the widget.
The simplest solution is to override the enterEvent()
and leaveEvent()
:
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent=parent)
self.setupUi(self)
def enterEvent(self, event):
super().enterEvent(event)
self.rmm_lb_logo.setStyleSheet("background-color: red;")
def leaveEvent(self, event):
super().leaveEvent(event)
self.rmm_lb_logo.setStyleSheet("")
Answered By - musicamante
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.