PyQT: Getting Started - PyQT in the Real World
(Page 4 of 4 )
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.
class Form1(QDialog):
def __init__(self,parent = None,name = None,modal =
0,fl = 0):
QDialog.__init__(self,parent,name,modal,fl)
:
:
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:
class Form1(QDialog):
def __init__(self,parent = None,name = None,modal = 0,fl = 0):
QDialog.__init__(self,parent,name,modal,fl)
self.lCDNumber1 = QLCDNumber(self,"lCDNumber1")
self.lCDNumber1.setGeometry(QRect(110,140,301,30))
self.slider1 = QSlider(self,"slider1")
self.slider1.setGeometry(QRect(130,220,261,31))
self.slider1.setOrientation(QSlider.Horizontal)
:
:
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.
class Form1(QDialog):
def __init__(self,parent = None,name = None,modal = 0,fl = 0):
QDialog.__init__(self,parent,name,modal,fl)
self.lCDNumber1 = QLCDNumber(self,"lCDNumber1")
self.lCDNumber1.setGeometry(QRect(110,140,301,30))
self.slider1 = QSlider(self,"slider1")
self.slider1.setGeometry(QRect(130,220,261,31))
self.slider1.setOrientation(QSlider.Horizontal)
self.connect(self.slider1,SIGNAL("valueChanged
(int)"),
self.lCDNumber1.display)
:
:
Lastly, we have the main section where QApplication will be set up:
class Form1(QDialog):
def __init__(self,parent = None,name = None,modal = 0,fl = 0):
QDialog.__init__(self,parent,name,modal,fl)
self.lCDNumber1 = QLCDNumber(self,"lCDNumber1")
self.lCDNumber1.setGeometry(QRect(110,140,301,30))
self.slider1 = QSlider(self,"slider1")
self.slider1.setGeometry(QRect(130,220,261,31))
self.slider1.setOrientation(QSlider.Horizontal)
self.connect(self.slider1,SIGNAL("valueChanged(int)"),
self.lCDNumber1.display)
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.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |