When I pass in a method of the class I’m currently in to the clicked.connect
method of a QAbstractButton
, everything works as expected:
import layout.layoutbutton as layoutbutton
class LayoutWindow(QWidget):
def load(self):
self.button = layoutbutton.LayoutButton()
self.button.load() # Method specific to `LayoutButton`
self.button.clicked.connect()
self.layout.addWidget(self.button)
def pressed(self, data):
print(data)
However, if I try to define pressed
in the layoutbutton.LayoutButton
class I used above, pressed
is no longer ran whenever I press the button:
import layout.layoutbutton as layoutbutton
class LayoutWindow(QWidget):
def load(self):
self.button = layoutbutton.LayoutButton()
self.button.load()
self.layout.addWidget(self.button)
class LayoutButton(QPushButton):
def load(self):
self.setText('Checkable layout button')
self.setCheckable(True)
self.clicked.connect(self.pressed)
def pressed(self, data):
print(data)
My question is, when the button is clicked and some internal Qt class is notified of the event, shouldn’t it execute the function passed in with no regard to what class it belongs to? It seems like an issue of scope, but if that were the case, how does my first example work, when the method is inside of the window class? That method is contained in a class as well, so why would Qt be able to access that method but not the other?