Python
  Home arrow Python arrow Page 3 - PyQT: Getting Started
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
Moblin 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

PyQT: Getting Started
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 10
    2006-03-29

    Table of Contents:
  • PyQT: Getting Started
  • PyQT: Understanding the Terminology
  • Steps for PyQT Application Creation
  • PyQT in the Real World

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!

    PyQT: Getting Started - Steps for PyQT Application Creation


    (Page 3 of 4 )

    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:

    import sys

    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:

    app=QApplication(sys.argv)

    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)   

    app.setMainWidget(button)
    button.show()                 

    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,   
                SLOT("quit()"))

    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:

    App.exec_loop()

    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.

    More Python Articles
    More By A.P.Rajshekhar


       · PyQT is one of the best toolkits available for Python. In this article I have tried...
       · This tutorial was pretty easy to follow, until the end. Turns out the "w.show()"...
       · But this is maybe the only chance I have to install pyqt in windows...Please tell me...
     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...

    Click Here




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway