我需要在QDialog
的不同类中打开QMainWindow
,关闭QDialog
后,必须断开所有信号。
我可以通过按下组合键来打开QDialog
,然后当它立即打开时,会连接到button_pressed
方法,该方法本身连接到self.spanSelector_press
和{{1 }}
这是到目前为止的代码:
self.spanSelector_
关闭class Window(QMainWindow): #This is a matplotlib figure
def __init__(self):
QMainWindow.__init__(self)
#A lot of stuff for the matplotlib figure
def button_pressed(self):
self.select_data = SelectData(self)
self.select_data.show()
self.cid_press = self.figure_canvas.mpl_connect('button_press_event',
self.spanSelector_press)
self.cid_release = self.figure_canvas.mpl_connect('button_release_event',
self.spanSelector_release)
def spanSelector_press(self, event):
if event.button ==1:
self.limite = "minimum"
self.clearMarker() #This is another method to erase the previous line drawn
self.marker = self.axes.axvline(Window.minimumCoords, linestyle='dashed',
linewidth = 2, color = "green")
self.figure_canvas.draw_idle()
Window.initial_marker = self.marker
self.xmin = event.xdata
def spanSelector_release(self, event):
pass
class SelectData(QDialog):
def __init__(self, parent):
QDialog.__init__(self, parent, Qt.WindowStaysOnTopHint)
def closeEvent(self, event):
#I need to disconnect all the signals when i close the QDialog
view = self.parent()
view.figure_canvas.mpl_disconnect(view.cid_press)
view.figure_canvas.mpl_disconnect(view.cid_release)
view.deselect()
event.accept()
后,如何断开button_pressed
方法中的所有信号?希望你能帮帮我。
答案 0 :(得分:0)
根据matplotlib docs,您可以使用连接ID断开连接,您需要通过在Window
中执行此类操作来保存:
def button_pressed(self):
self.select_data = SelectData(self)
self.select_data.show()
self.cid_press = self.figure_canvas.mpl_connect('button_press_event', self.spanSelector_press)
self.cid_release = self.figure_canvas.mpl_connect('button_release_event', self.spanSelector_release)
您的SelectData
类可以通过调用Window
来引用其父类(parent()
),因此您可以使用它来进行断开连接。
def closeEvent(self, event):
window = self.parent()
window.figure_canvas.mpl_disconnect(window.cid_press)
window.figure_canvas.mpl_disconnect(window.cid_release)
event.accept()
我还没有测试过,但它应该非常接近。
修改:请务必将parent
传递给QDialog
构造函数:
class SelectData(QDialog):
def __init__(self, parent):
QDialog.__init__(self, parent, Qt.WindowStaysOnTopHint)