There are five main steps to create a simple PyQT application, which are:
1. Importing the required Packages.
2. Creating QApplication object.
3. Adding the required Widgets.
4. Setting up the Signal-Slot connection for the Widgets.
5. Starting the event loop.
Of these, the first, second and fifth are mandatory for even a simple application devoid of any Widgets and events. The whys and wherefores of all these steps are as follows:
Importing the required Packages
Though this is the preliminary step in any application creation, in this case it merits a special mention. The reason is dependency. To create any PyQT application the qt library has to be imported thus:
from qt import *
Apart from this, one more library is required; that is:
The qt library is dependent on the sys library because the command line parameters are read by the QApplication for which the sys library is required. The command line can contain options such as “-style=platinum” to have the Mac OS 8.5 look and feel. The point to be kept in mind is that advanced functionality such as the canvas have been divided into separate modules, like qtcanvas for QCanvas.
Creating QApplication object
Next is the QApplication object. It handles the dispatching of events generated by the keyboard and the mouse to the various widgets. In essence it is the QApplication object that controls the GUI’s control flow and main settings. It contains the main event loop. It is in the main event loop that all events from the window system and other sources are processed and dispatched. The initialization, finalization and session management for an application are managed by the QApplication object. Whatever be the number of windows, there will only be one QApplication object. For example, to create a QApplication object named app the statement would be:
In the above code the command line parameters are passed to the QApplication by using sys.argv.
Adding the required Widgets
This step varies according to the requirements. It can be as simple as a single button with no window to an MDI style widget placement. Whatever the case may be, there are three things to be done while adding the widgets. The first thing is the creation of the object. The second thing is adding the widgets to the containers. Sometimes the widgets themselves can act as containers. Finally, the third task is to make the widget visible. Taking the simplest case, to add a simple button named button the code would be:
button=QPushButton("Hello World", None)
Here, the button has no parent. Hence "None" is passed as the second parameter. Then the button object is set as the main widget by calling setMainWidget() on QApplication object with the button object. This tells app that button is the main widget. Next line makes the widget visible.
Setting up the Signal-Slot connection for the widgets
Once the widgets are added, next step is setting up the Signal-Slot connections for the added widgets. This is done using the connect() method of QApplication. The connect() method takes four parameters: first the object that sends the signal; second the Signal that is being emitted by the object passed as the first parameter; third the object that would be receiving the signal; and the final parameter is the Slot in which the Signal would be received. To put it in code:
app.connect(button, SIGNAL("clicked()"), app,
Let's analyze this. The first parameter is the button object. Its "clicked" signal is being connected to the app’s quit Slot. In more general terms, when the button is clicked, the "clicked" Signal would be emitted and would be "sent" to the "quit" Slot, and the function defined for the Slot would be executed.
Setting up the event loop
Everything is set up. However, until the event loop is started, nothing will take place. As stated earlier, it is the event loop where the processing and dispatch of events takes place. To start the event loop, the exec_loop() of QApplication object is to be called. In code:
These steps are the common steps taken when creating a PyQT application. Also these steps detail how QT is mapped with PyQT. However, unless a real world application is shown, theory is just theory. In the next section, I will be developing an application in which a slider's movement will be translated into display of a text box. Though it is not a standard application, it will become a component in the future application that I will be developing as part of this series on PyQT.
blog comments powered by Disqus