Multiple Inheritance Example#
Using a form created with Qt Designer in an application.
The Multiple Inheritance Example shows how to use a form created with Qt Designer in an application by subclassing both
QWidget and the user interface class, which is
To subclass the
calculatorform.ui file and ensure that
qmake processes it with the
uic, we have to include
calculatorform.ui in the
.pro file, as shown below:
<Code snippet "multipleinheritance/multipleinheritance.pro:0" not found>
When the project is compiled, the
uic will generate a corresponding
CalculatorForm definition, we include the
ui_calculatorform.h that was generated earlier.
from ui_calculatorform import *
As mentioned earlier, the class is a subclass of both
class CalculatorForm(QWidget, Ui): Q_OBJECT # public CalculatorForm = explicit(QWidget parent = None) slots: = private() def on_inputSpinBox1_valueChanged(value): def on_inputSpinBox2_valueChanged(value):
Two slots are defined according to the automatic connection naming convention required by
uic. This is to ensure that
QMetaObject ‘s auto-connection facilities connect all the signals and slots involved automatically.
In the constructor, we call
setupUi() to load the user interface file. Note that setupUi is a method of
def __init__(self, parent): super().__init__(parent) setupUi(self)
We include two slots,
on_inputSpinBox2_valueChanged(). These slots respond to the
valueChanged() signal that both spin boxes emit. Whenever there is a change in one spin box’s value, we take that value and add it to whatever value the other spin box has.
def on_inputSpinBox1_valueChanged(self, value): outputWidget.setText(QString.number(value + inputSpinBox2.value())) def on_inputSpinBox2_valueChanged(self, value): outputWidget.setText(QString.number(value + inputSpinBox1.value()))
main() function instantiates
calculator object is displayed by invoking the
if __name__ == "__main__": app = QApplication() calculator = CalculatorForm() calculator.show() sys.exit(app.exec())
There are various approaches to include forms into applications. The Multiple Inheritance approach is just one of them. See Using a Designer UI File in Your Application for more information on the other approaches available.