Python
  Home arrow Python arrow PyQT: Input Widgets
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: Input Widgets
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    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
     

       

    PYTHON ARTICLES

    - Tuples and Other Python Object Types
    - The Dictionary Python Object Type
    - String and List Python Object Types
    - Introducing Python Object Types
    - Mobile Programming using PyS60: Advanced UI ...
    - Nested Functions in Python
    - Python Parameters, Functions and Arguments
    - Python Statements and Functions
    - Statements and Iterators in Python
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT