Python
  Home arrow Python arrow PyQT: Input Widgets
Dev Shed Forums 
Administration  
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Developerworks
 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: Input Widgets
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2006-11-20

    Table of Contents:
  • PyQT: Input Widgets
  • QCheckBox
  • PyQT in the Real World
  • The Connections
  • The Main Part

  • 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

    Virtual Tradeshows by Ziff Davis Enterprise - A Unique Opportunity to Connect with IT Experts, Access Information, and Gain Insight on today's Technology

    PyQT: Input Widgets
    (Page 1 of 5 )

    You may have experienced some headaches in the past when creating GUIs to gather user input. Python makes this process easy with input widgets. Keep reading to learn more.

    The edge that GUI has over CLI is the varied ways it offers to gather user input.  In a CLI-based application, there are at most two ways of taking the input – as strings of alphanumeric or as a single character or number. With a GUI-based application, the input can be gathered using various widgets such as textboxes, checkboxes, radio buttons etc.

    In most GUI frameworks, input widgets are the most complex or difficult to create. But the simplicity of Python comes to the rescue with PyQT. There are many input widgets supplied with PyQT itself, and if the platform happens to be KDE, then widgets that are capable of handling multiple types of input strategies are available.

    Starting from this part, I will be focusing on the different input widgets provided by PyQT. In this discussion I will be introducing the line input widget along with the checkbox widget. The first two sections will focus on the slots and signals of the widgets. The following sections will test the widgets by using them in a form that will later become part of a larger application. That sets the agenda for this discussion.

    QLineEdit and QCheckbox: Understanding the Basic Widgets

    Every GUI toolkit provides some widgets that provide basic services for gathering user inputs. These widgets don’t fit the category of heavy duty GUI widgets of the likes of a rich text editor, tree view et al. However they are quite indispensable when a pretty good user interface must be created. The single line textbox and checkbox are two of such basic yet indispensable widgets. In the world of PyQT they are known as QLineEdit and QCheckbox. 

    The difference that these widgets have between their counterparts in other languages is the way events are handled. That’s right – signals and slots. So let's look at the most common signals and slots that are available for QLineEdit and QCheckbox.

    QLineEdit

    As the name suggests, QLineEdit is a one line text editor. In other words, it supports basic editing services but the text cannot span over multiple lines (read one line). In essence, it lets the user enter and edit a single line of plain text. The most common slots of this widget are:

    • setText()
    • selectAll()
    • setMaxLength()
    • insert()

    and the signals are:

    • textChanged()
    • textEdited()

    Let's look at the slots first.

    The setText() slot is used to set a new text into the QLineEdit. When a new text is set, it removes the text that is already present. It takes one parameter – QString. When text is set using setText() method, the textChanged() signal is emitted.

    The selectAll() slot is used to select all the entered text. That means this slot can be used to highlight the entered text. In some cases it becomes necessary to place default values for QLineEdit. But whenever the user tries to enter data, the default value has to be deleted. For this to take place, the default data present has to be completely highlighted so that it can be deleted when the user tries to enter data. Hence, to make the current data in the QLineEdit preselected, this slot can be called.

    The setMaxLength() slot takes care of the requirement to set the maximum length of the text that can be entered. It takes an integer value as argument.

    While the setText() slot replaces or appends the text (according to the logic used), there are times, when text needs to be inserted. The insert() slot comes handy in such situations. One thing to keep in mind is that while invoking this slot, if any  portion of the text is highlighted, it will be deleted and the value of the QString passed as argument will be pasted in place of that portion.

    That’s about it for the theory of QLineEdit's service slots. Now let's look at how they can be used. The following block of code creates an object of QLineEdit, sets its length to 10, sets the text to “123- BackwardSt” and then selects it all:

    Address = QLineEdit( )
    Address.setMaxLength(10)
    Address.setText(‘123- BackwardSt’)
    Address.selectAll()

    The next part of this puzzle is the signals emitted by QLineEdit.

    Whenever the text is changed programmatically using the setText() slot, the textChanged() signal is emitted. This signal takes a QString argument which is the new text that has been set in QLineEdit. This is emitted when the text changes in any other way (such as the user changes the text).

    The textEdited() signal also takes a QString as an argument and is emitted whenever text is edited (or changed). However if the text is changed via the setText() slot, then this signal is not emitted.

    The one question that still remains about QLineEdit is that of how to access the text contained in QLineEdit. For this, the property of QLineEdit known as text() can be used. It returns a QString object containing the current text of the QLineEdit. That brings us to the next widget – QCheckBox.

    More Python Articles
    More By A.P.Rajshekhar


       · In this article I have discussed about using textbox and checkbox for input...
       · It's the main pyqt version now, and the recommended one for beginners, that are are...
       · First let me thank you for your encouragement and appreciation. As for why not PyQT...
     

       

    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...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




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