Pablo Flores
Pablo Flores

Reputation: 717

Disconnecting a PyQt Signal in a conditional

I am having some troubles with a mouse event in PyQt. This is the code:

class A(QMainWindow):

  var = None

  def __init__(self):
    QMainWindow.__init__(self)
    #Here I draw a matplotlib figure

    self.figure_canvas = FigureCanvas(Figure())
    layout.addWidget(self.figure_canvas, 10)
    self.axes = self.figure_canvas.figure.add_subplot(211)

    #I created a toolbar for the figure and I added a QPushButton

    self.btn_selection_tool = QPushButton()
    self.navigation_toolbar.addWidget(self.btn_selection_tool)
    self.connect(self.btn_selection_tool, SIGNAL("clicked()"), self.B)

  def B(self):
    if self.var == 1:
      cid = self.figure_canvas.mpl_connect("press_button_event", self.C)

  def C(self, event):
    x = xdata.event
    #I draw a line every time I click in the canvas

  def D(self):
    #Here I tried to call this method and disconnect the signal
    self.figure_canvas.mpl_disconnect(cid)

The problem is that I can not disconnect the signal of the mouse event using:

self.figure_canvas.mpl_disconnect(cid)

Nothing happens, I keep drawing a line with every click I make. The mouse event is still connected.

How can I disconnect the signal? maybe using another QPushButton?

Upvotes: 0

Views: 657

Answers (1)

mfessenden
mfessenden

Reputation: 608

Are you storing the connection somewhere? You might need to store the in a variable to disconnect it properly:

class A(QMainWindow):

    def __init__(self):
        QMainWindow.__init__(self)

        self.cid = None

    def B(self):
        if self.var == 1:
            self.cid = self.figure_canvas.mpl_connect("press_button_event", self.C)

    def D(self):
        if self.cid is not None:
             self.figure_canvas.mpl_disconnect(self.cid)

Upvotes: 1

Related Questions