In real world applications, there would be at least two classes, one that sub-classes a container class such as QDialog and a second that would inherit the first one and set up the QApplication object. For the sake of brevity, in this discussion only one class will be used, that will be derived from the QDialog class. So let's get started.
First the imports:
import sys from qt import *
Next the class that derives from QDialog. It is better to derive from QDialog if you require a lightweight application:
class Form1(QDialog): : :
Now the constructor for this class. The main parameters to look out for are the parent indicating the parent of the dialog, the name of the dialog and the flag indicating whether the dialog is modal or not.
Next are the two components to be used, QLCDNumber and QSlider. Their objects are instantiated by passing their names and the equivalent of the "this" pointer of C++ - self. Then the geometry is set to set the boundaries:
Then connect the QLCDNumber and QSlider. The Signal is "valueChanged" and the slot is "display." Since the display of the QLCDNumber is the receiver and the slot, they are not separately used.
if __name__ == "__main__": a = QApplication(sys.argv) QObject.connect(a,SIGNAL("lastWindowClosed()"),a,SLOT ("quit()")) w = Form1() a.setMainWidget(w) a.exec_loop()
That’s all. Whenever the value of the slider changes, the new value is displayed in the QLCDNumber.
That brings us to the end of this discussion. It should be evident from the above application how easy it is to develop applications using PyQT. But this is just the beginning. In the future, I will cover other aspects, including complex widgets such as qtcanvas. Till next time.