p y q t 5 在 子 控 件 上 无 法 检 测 到 鼠 标 移 动 行 为 pyqt5在子控件上无法检测到鼠标移动行为pyqt5在子控件上无法检测到鼠标移动行为
解决方案:子控件也设置setMouseTracking(True)
self.setMouseTracking(True)
self.label.setMouseTracking(True)
# 0.导入需要的包和模块
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__':
# 1.创建一个应用程序对象
app = QApplication(sys.argv)
# 2.控件的操作
# 2.1创建控件
window = Window()
# 2.2设置控件
# 2.3展示控件
window.show()
# 3.应用程序的执行,进入到信息循环
sys.exit(app.exec_())
版权声明:本文为qq_41375318原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。