PyQt5鼠标移动事件之进入子控件

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QLabel, QLineEdit, QToolTip, QWidget
from PyQt5.QtGui import QPainter, QColor, QPen
from PyQt5.QtCore import Qt, QEvent

class MyLineEdit(QLineEdit):

    def __init__(self, parent=None):
        super().__init__(parent)
        # 启用鼠标跟踪
        self.setMouseTracking(True)


    def mouseMoveEvent(self, event):
        print("dddddddddddddddddddddddd")
        if self.rect().contains(event.pos()):
            print(f"鼠标进入了 {self.objectName()} 控件")
        pos = self.mapFromGlobal(self.cursor().pos())
        print(pos)
        widget = self.childAt(pos)
        print(event.pos(),"=======",widget)
        if widget is not None or widget =='':
            print("6666")
            class_name = widget.metaObject().className()
            print(class_name)

class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):

        self.setWindowTitle('Mouse Hover Event')
        self.setGeometry(300, 300, 400, 200)

        # 创建三个不同类型的控件
        self.button = QPushButton('Button', self)
        self.button.move(20, 20)

        self.label = QLabel('Label', self)
        self.label.move(20, 60)

        self.line_edit = MyLineEdit(self)
        self.line_edit.move(20, 100)
        self.line_edit.setFixedHeight(80)
        self.show()
        self.button.setMouseTracking(True)
        self.label.setMouseTracking(True)
        # self.line_edit. QLineEdit setMouseTracking(True)
        self.setMouseTracking(True)  # 开启鼠标跟踪功能



    def mouseMoveEvent(self, event):

        # widget = self.childAt(event.pos())
        pos = self.mapFromGlobal(self.cursor().pos())
        print(pos)
        widget = self.childAt(pos)
        print(event.pos(),"=======",widget)
        if widget is not None:
            print("6666")
            class_name = widget.metaObject().className()
            print(class_name)

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PyQt中的graphicsview控件可以通过实现mousePressEvent()、mouseReleaseEvent()、mouseMoveEvent()方法来实现鼠标漫游功能。具体实现步骤如下: 1. 创建GraphicsView对象和Scene对象,将Scene对象设置给GraphicsView对象。 2. 在GraphicsView对象中重写鼠标事件的三个方法: (1)mousePressEvent(): 获取鼠标按下的坐标点,并将其设置为起始点; (2)mouseMoveEvent(): 获取鼠标移动坐标点,计算出鼠标在视图中移动的距离,并以此来调用setSceneRect()设置场景的滚动区域以实现鼠标漫游功能; (3)mouseReleaseEvent(): 释放鼠标。 3. 在程序中实例化GraphicsView对象并将其加入主窗口的布局中,即可实现鼠标漫游功能。 具体的代码实现可以参考如下: from PyQt5.QtWidgets import QGraphicsView, QGraphicsScene, QApplication from PyQt5.QtCore import Qt class MyView(QGraphicsView): def __init__(self, parent=None): super(MyView, self).__init__(parent) self.setScene(QGraphicsScene(self)) self.setRenderHint(QPainter.Antialiasing) def mousePressEvent(self, event): self.startingPoint = event.pos() self.setDragMode(QGraphicsView.ScrollHandDrag) super(MyView, self).mousePressEvent(event) def mouseMoveEvent(self, event): currentPoint = event.pos() difference = currentPoint - self.startingPoint self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse) self.translate(difference.x(), difference.y()) self.startingPoint = currentPoint super(MyView, self).mouseMoveEvent(event) def mouseReleaseEvent(self, event): self.setDragMode(QGraphicsView.NoDrag) super(MyView, self).mouseReleaseEvent(event) if __name__ == '__main__': import sys app = QApplication(sys.argv) view = MyView() view.setGeometry(100,100,300,200) view.show() sys.exit(app.exec_())

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值