p
y
q
t
5
在
子
控
件
上
无
法
检
测
到
鼠
标
移
动
行
为
pyqt5在子控件上无法检测到鼠标移动行为
pyqt5在子控件上无法检测到鼠标移动行为
解决方案:子控件也设置setMouseTracking(True)
self.setMouseTracking(True)
self.label.setMouseTracking(True)
from PyQt5.Qt import *
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Major")
self.resize(500, 500)
self.setMouseTracking(True)
self.label = QLabel(self)
self.label.resize(200,200)
self.label.setText("测试")
self.label.setStyleSheet("background-color:blue;")
self.label.setMouseTracking(True)
def mouseMoveEvent(self,event):
x = event.x()
y = event.y()
text = "x:{0},y:{1}".format(x,y)
self.label.setText(text)
print(text)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())