0

I'm using PyQt and Matplotlib libraries to create an application. I've created a sub-class of my QMainWindow and I do not know how to connect the method of the new class to the methods in the first class.

At first, I created a QPushButton in the QMainWindow like this:

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

    self.mainWidget = QWidget()
    self.setCentralWidget(self.mainWidget)

    layout = QVBoxLayout()
    self.mainWidget.setLayout(layout)

    self.figure_canvas = FigureCanvas(Figure(facecolor="lightblue"))
    layout.addWidget(self.figure_canvas, 10)

    self.axes = self.figure_canvas.figure.add_subplot(111)

    #Here is the first button creatd and added to the navigation toolbar
    #using addWidget
    self.btn_selection_tool = QPushButton(QIcon("select.png"), "")
    self.navigation_toolbar.addWidget(self.btn_selection_tool)

    #And here is how i connected this button to two methods
    self.connect(self.btn_selection_tool, SIGNAL("clicked()"), self.method1)
    self.connect(self.btn_selection_tool, SIGNAL("clicked()"), self.method2)

But now, I had to create a new class (class B) and I need to connect a method from this one to the methods called before by the QPushButton in class A. But, in this case, I do not have a button to do it. I just need to "trigger" this methods in class A , by a method in class B.

My problem is that I can not call the methods in class A (self.method1 and self.method2)the same way that I did when I created the QPushbutton. This is before creating the subclass.

I've tried doing:

def method_in_B_class(self):
  self.a  = A()
  self.a.method1()
  self.a.method2()

How can I do this? Hope you can help me.

------ EDIT -----

These are the two methods:

def method1(self):
    self.figure_canvas.setCursor(Qt.IBeamCursor)
    self.selection_mode = None
    #self.selectionmode is a variable that I change when method1 is called
    #and I can go through the "if" in method2

def method2(self):
    #I define some conditions with another variables
    if len(self.axes.lines) >= 1 and self.selection_mode == None and A.send_msg == None:

        self.cid_press = self.figure_canvas.mpl_connect("button_press_event", self.method3)
        self.cid_release = self.figure_canvas.mpl_connect("button_release_event", self.method4)

    elif self.selection_mode == "disable":
        self.disconnect(self)

So, when they are called using:

def method_in_B_class(self):
  self.a  = A()
  self.a.method1()
  self.a.method2()

they are instantiated, but then they are "finished" we might say. This is that the QtCursor does not change, and the method3 and method4 are not called.

9
  • Should probably pass through parent=parent. Commented May 29, 2016 at 20:13
  • NavigationToolbar2.__init__(self, figure_canvas, parent= None) -> NavigationToolbar2.__init__(self, figure_canvas, parent=parent) but I think that is unrelated to your problem. Commented May 29, 2016 at 20:27
  • and I would look at the code matplotlib.backends.backend_qt5.py to see how the toolbar gets built. Is the problem that 'bye' is not being called or something else? Commented May 29, 2016 at 20:29
  • I've tried that, an it is not the problem. Sorry, I did not understand. The problem is that when the method1 and method2 are called, they do not "act" like when I call them using the QPushButton. Commented May 29, 2016 at 20:35
  • 1
    Everytime you call bye you are creating a new instance of A (your whole window) and operating on that instance. Commented May 29, 2016 at 20:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.