【问题标题】:Implement dragMoveEvent on QWidget in pyqt5?在pyqt5中的QWidget上实现dragMoveEvent?
【发布时间】:2016-08-17 21:42:50
【问题描述】:

有谁知道如何在我的 QWidget 上实现 dragMove 事件?所以基本上我想要的是将鼠标移到小部件上,按住鼠标按钮并拖动它。拖动时,不应移动小部件,它应仅在按下鼠标时捕获鼠标坐标。

我已经用谷歌搜索了,只是找到了一些拖放教程,他们将一些东西拖到了小部件等中,比如文本。这没什么用。

【问题讨论】:

    标签: python user-interface pyqt pyqt5


    【解决方案1】:

    这与拖动无关。您真正需要做的是启用鼠标跟踪,然后监控鼠标移动事件。

    这是一个简单的演示:

    from PyQt5 import QtCore, QtGui, QtWidgets
    
    class Window(QtWidgets.QWidget):
        def __init__(self):
            super(Window, self).__init__()
            self.setMouseTracking(True)
    
        def mouseMoveEvent(self, event):
            if event.buttons() & QtCore.Qt.LeftButton:
                print(event.globalPos().x(), event.globalPos().y())
    
    if __name__ == '__main__':
    
        import sys
        app = QtWidgets.QApplication(sys.argv)
        window = Window()
        window.setGeometry(500, 150, 100, 100)
        window.show()
        sys.exit(app.exec_())
    

    【讨论】:

      【解决方案2】:

      我认为您正在寻找mousePressEvent 而不是dragMoveEvent。您需要继承 QWidget 并实现 mousePressEvent 方法来提供您的实现:

      from PyQt5.QtWidgets import QWidget
      
      class MyWidget(QWidget):
      
          def mousePressEvent(self, event):
              print(event.pos())
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-25
        • 2020-06-07
        • 2019-04-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多