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.