gpt4 book ai didi

python - 在pyqt中拖动 Canvas 时将 Canvas 绑定(bind)到窗口

转载 作者:太空宇宙 更新时间:2023-11-03 15:51:48 25 4
gpt4 key购买 nike

我有一个应用程序,它将 matplotlib 图形设置为 FigurCanvas,然后将 FigurCanvas 添加到我的 AppWindow,我已将其设置为可拖动图形。但是,当我拖动图形时,它所包含的窗口会保持在原来的位置,因此图形会被拖离窗口。有没有办法将两者绑定(bind)在一起,以便当图形移动时窗口保持不变?这是代码。

from PyQt4 import QtCore
from PyQt4 import QtGui
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas


class GraphCanvas(FigureCanvas):
def __init__(self):

# The window
self.fig = Figure(figsize=(5, 5), dpi=100)
self.ax1 = self.fig.add_subplot(111)
self.ax1.plot([1,2,3], [1,2,3], linewidth=2, color="#c6463d", label="line1")
FigureCanvas.__init__(self, self.fig)

# drag properties
self.draggable = True
self.dragging_threshold = 5
self.__mousePressPos = None
self.__mouseMovePos = None

def mousePressEvent(self, event):
if self.draggable and event.button() == QtCore.Qt.LeftButton:
self.__mousePressPos = event.globalPos() # global
self.__mouseMovePos = event.globalPos() - self.pos() # local
super(GraphCanvas, self).mousePressEvent(event)

def mouseMoveEvent(self, event):
if self.draggable and event.buttons() & QtCore.Qt.LeftButton:
globalPos = event.globalPos()
moved = globalPos - self.__mousePressPos
if moved.manhattanLength() > self.dragging_threshold:
# move when user drag window more than dragging_threshould
diff = globalPos - self.__mouseMovePos
self.move(diff)
self.__mouseMovePos = globalPos - self.pos()
super(GraphCanvas, self).mouseMoveEvent(event)

def mouseReleaseEvent(self, event):
if self.__mousePressPos is not None:
if event.button() == QtCore.Qt.LeftButton:
moved = event.globalPos() - self.__mousePressPos
if moved.manhattanLength() > self.dragging_threshold:
# do not call click event or so on
event.ignore()
self.__mousePressPos = None
super(GraphCanvas, self).mouseReleaseEvent(event)

''' End Class '''


class AppWindow(QtGui.QWidget):
def __init__(self, parent=None):
super(AppWindow, self).__init__(parent)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)

layout = QtGui.QVBoxLayout(self)
cpu_canvas = GraphCanvas()
layout.addWidget(cpu_canvas)

''' End Class'''

if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
main = AppWindow()
main.show()
sys.exit(app.exec_())

最佳答案

如果你想拖动AppWindow,你应该在这个类中注册拖动,而不是在图形 Canvas 中。然后,您可以在图形 Canvas 内将拖动事件路由到 AppWindow。

以下内容应该适合您,我没有更改太多代码,只是重新排列它,将父参数添加到 GraphCanvas并让拖动函数调用其父函数。

from PyQt4 import QtCore
from PyQt4 import QtGui
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas


class GraphCanvas(FigureCanvas):
def __init__(self, parent = None):
self.parent = parent
# The window
self.fig = Figure(figsize=(5, 5), dpi=100)
self.ax1 = self.fig.add_subplot(111)
self.ax1.plot([1,2,3], [1,2,3], linewidth=2, color="#c6463d", label="line1")
FigureCanvas.__init__(self, self.fig)


def mousePressEvent(self, event):
self.parent.mousePressEvent(event)

def mouseMoveEvent(self,event):
self.parent.mouseMoveEvent(event)

def mouseReleaseEvent(self, event):
self.parent.mouseReleaseEvent(event)


''' End Class '''


class AppWindow(QtGui.QWidget):
def __init__(self, parent=None):
super(AppWindow, self).__init__(parent)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)

layout = QtGui.QVBoxLayout(self)
cpu_canvas = GraphCanvas(self)
layout.addWidget(cpu_canvas)

self.draggable = True
self.dragging_threshold = 5
self.__mousePressPos = None
self.__mouseMovePos = None

def mousePressEvent(self, event):
if self.draggable and event.button() == QtCore.Qt.LeftButton:
self.__mousePressPos = event.globalPos() # global
self.__mouseMovePos = event.globalPos() - self.pos() # local
super(AppWindow, self).mousePressEvent(event)

def mouseMoveEvent(self, event):
if self.draggable and event.buttons() & QtCore.Qt.LeftButton:
globalPos = event.globalPos()
moved = globalPos - self.__mousePressPos
if moved.manhattanLength() > self.dragging_threshold:
# move when user drag window more than dragging_threshould
diff = globalPos - self.__mouseMovePos
self.move(diff)
self.__mouseMovePos = globalPos - self.pos()
super(AppWindow, self).mouseMoveEvent(event)

def mouseReleaseEvent(self, event):
if self.__mousePressPos is not None:
if event.button() == QtCore.Qt.LeftButton:
moved = event.globalPos() - self.__mousePressPos
if moved.manhattanLength() > self.dragging_threshold:
# do not call click event or so on
event.ignore()
self.__mousePressPos = None
super(AppWindow, self).mouseReleaseEvent(event)

''' End Class'''

if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
main = AppWindow()
main.show()
sys.exit(app.exec_())

关于python - 在pyqt中拖动 Canvas 时将 Canvas 绑定(bind)到窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41214022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com