Skip to content Skip to sidebar Skip to footer

Subclassing Of Qpushbutton

I've been looking for a way of subclassing QPushButton, so I can connect 'clicked' signal when constructing new button, like: Btn = CustomButtonClass('Text', clicked='lambda: self.

Solution 1:

You don't need to create a sub-class, because both PyQt and PySide already have this feature (see Connecting Signals Using Keyword Arguments in the PyQt docs).

Demo:

>>>from PyQt4 import QtGui>>>app = QtGui.QApplication([])>>>btn = QtGui.QPushButton('Test', clicked=lambda: print('Hello World!'))>>>btn.click()
Hello World!

If you still need to subclass, then of course you can simply do:

classCustomButtonClass(QtGui.QPushButton):def__init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

Post a Comment for "Subclassing Of Qpushbutton"