AVC, Application View Controller

News | What is | Features | Quick start | Download | Installation | Change log

Reference manual:   Common part | GTK | Qt3 | Qt4

Examples   GTK: Simple | Counter | Complex Qt3: Simple | Counter | Complex Qt4: Simple | Counter | Complex

News

30-Jan-2007 released AVC 0.2.0
Added new widgets: combo box, slider, status bar (GTK only), text view/edit.

What is

Graphical User Interfaces (GUIs) are the easy way to input data to an application sofware and to view data produced by the application. The management of data exchanges between GUI and application is a central problem in GUI programming, it absorbs a relevant part of the programming effort. AVC is a python module that makes the programming of this data exchanges very easy, it supports the principal widget toolkits.

AVC, the Application View Controller, is a fully transparent and automatic connector between the values displayed and entered by GUI widgets and the variables of an application using the GUI. The connection is bidirectional. If the application sets a new value into a connected variable, AVC copies the new value into all widgets connected to the variable. If a new value is entered by a widget, AVC copies the new value into all other widgets connected the variable, into the variable and optionally notifies the change to the application. The connections are autogenerated by looking for matching names between widget names and variable names.

The application is completely unaware of the presence of the connected variables, it reads and writes them as normal variables. Only if the application requires to be immediately notified when a connected variable changes value, a notify handler must be added to the application.

AVC is released under the GNU General Public License .

Current version is AVC 0.2.0, alpha status, released 30-Jan-2007.

Tested on Debian GNU/Linux Etch, FP 30-Jan-2007.

For any question, suggestion, contribution contact the author Fabrizio Pollastri, e-mail: pollastri (at) iriti.cnr.it.

Th AVC web site is hosted at http://avc.iriti.cnr.it/.

Features

Quick start

Essential instructions to get started with AVC. This instructions are for the GTK toolkit, the usage with the other supported toolkits is very similar. AVC module is supposed already installed. For full details, see the documentation pages.

Import the AVC module for GTK.

from avc.avcgtk import *

Derive the application class from AVC class. Let suppose that the application class name is "theApp".

class theApp(AVC):

Design the GUI with Glade or create it in the application, naming the widgets with the rule described below.

Define all variables to be connected in the application. Each variable must have a name equal to the matching name of the widgets that are to be connected to the variable. A widget matching name is the widget name itself, if it does not contain a double underscore '__', otherwise is the name part before the double underscore.

In the application, after the creation of GUI and all variables to be connected, call the instance method 'avc_init'. Let suppose that the application instance name is "the_app".

the_app.avc_init()

All is done for AVC. From this point, AVC takes full control over data exchange between the connected variables and widgets.

Download

Download AVC tarball from http://avc.iriti.cnr.it/dist/ or from http://cheeseshop.python.org/pypi.

Installation

To run AVC, Python 2.2 or later must already be installed. The latest release is recommended. Python is available from http://www.python.org/.

The first step is to expand the .tgz archive in a temporary directory (not directly in Python's site-packages). It contains a distutils setup file "setup.py".

  1. Open a shell.

  2. Unpack the tarball in a temporary directory (not directly in Python's site-packages). Commands:

    tar zxf avc-X.Y.tar.gz

    X and Y are the major and minor version numbers of the tarball.

  1. Go to the directory created by expanding the tarball:

    cd avc-X.Y
  2. Get root privileges and install the package:

    su
    (enter root password)
    python setup.py install

    If the python executable isn't on your path, you'll have to specify the complete path, such as /usr/local/bin/python.

Change log

AVC 0.2.0 released 30-Jan-2007

AVC 0.1.0 released 10-Jan-2007


Reference manual

Common part

This is the part of the reference manual common to all supported widget toolkits: GTK, Qt3, Qt4.

Names matching

AVC connects widgets and variables using a names matching procedure with the following rules.

The matching name for a variable is the variable name itself.

Examples of matching names
widget namematching name
button_okbutton_ok
toggle__buttontoggle
check_button_1check_button_1
radio_button__2radio_button

The matching name for a widget is the widget name itself, if the name does not contain a double underscore ('__'), otherwise the matching name is the part of the widget name before the double underscore. This allow to differentiate widget names for widgets that are to be connected to the same variable.

Each widget having a matching name equal to a variable matching name is connected to that variable.

A widget can be connected to one variable. A variable can be connected to one or more widgets.

Application class

The application that uses AVC must be instantiated from an application class that is derived from the AVC class. Let suppose that the application class name is "theApp", the application class statement will be class theApp(AVC):.

The AVC class is derived from the builtin object class that is the base of all new style classes introduced with python 2.2. So, also the application becomes a new style class.

AVC initialization

AVC start its job just after it is initialized. AVC initialization can take place in the application after the creation of GUI and after the instantiation of all variables to be connected. AVC initialization is done by calling the instance method avc_init. Let suppose that the application instance name is "the_app", the AVC init statement will be the_app.avc_init():.

When the value of a connected variable is changed, the values displayed by the widgets connected to it are updated by AVC in one of two allowed modes: immediate or periodic. Mode selection is done at AVC initialization specifying the "view_period" argument. If the argument is omitted, like in the_app.avc_init():, it is assigned a default value of 0.1 seconds, selecting a periodic views update with that period. If the argument is assigned a value, like in the_app.avc_init(view_period=0.2):, views will be updated every "view_period" seconds. If the argument is assigned to zero or to "None" value, like in the_app.avc_init(view_period=0):, views will be updated immediately after each change of the variable value.

Button widget

The memoryless press button, its connected variable must be a boolean. In normal state (button not pressed) the variable is "False", in pressed state (mouse pointer over button and mouse button 1 pressed) the variable is "True". Names for button widget in supported toolkits: GTK "Button", Qt3 and Qt4 "PushButton" with toggle attribute off.

Check button widget

The behaviour of the check button widget is the same of the toggle button widget. See toggle button. Names for check button widget in supported toolkits: GTK "CheckButton", Qt3 and Qt4 "CheckBox".

Combo box widget

The combo box, an item selector. The connected variable must be of type integer, its value is the index of the selected item. When no item is selected index is -1. Names for combo box widget in supported toolkits: GTK, Qt3 and Qt4 "ComboBox".

Entry widget

The text entry, its connected variable can be integer, float or string. Text input must conform to the type of the connected variable. If the connected variable is of type string, its value is copied to the entry widget "as is", if type is integer or float, the value is converted to string before copy. Names for text entry widget in supported toolkits: GTK "Entry", Qt3 and Qt4 "LineEdit".

Label widget

The text label, its connected variable can be boolean, integer, float, string, list or tuple. If the connected variable is of type string, its value is copied to the label widget "as is", if type is boolean, integer or float, the value is converted to string before copy. If the label is created with a default text that is a valid python formatting string, this is saved by AVC and used to format the label text updates when the connected variable value changes. If the connected variable type is list or tuple, a valid python formatting string matching all the elements of list or tuple is mandatory in default lebel text.

Radio button widget

The radio buttons come always in groups of two or more radio buttons. Each radio button behaves like a check button, but only one radio button at a time can be checked in each group. A variable of type integer can be connected to each group of radio buttons, its value is the index of the checked radio button in the group.

Slider widget

The slider, its connected variable can be integer or float. The GTK "HScale" and "VScale" support both types. On the contrary, Qt3 and Qt4 support only integer with "Slider" widget. Remember that in python floats are always doubles.

Spin button widget

The spin button, its connected variable can be integer or float. The GTK "SpinButton" support both types. On the contrary, Qt3 and Qt4 differentiate integer or float support with two widgets: "SpinBox" and "DoubleSpinBox". Names for spin button widget in supported toolkits: GTK "SpinButton", Qt3 and Qt4 "SpinBox" for integer and "DoubleSpinBox" for float. Remember that in python floats are always doubles.

Status bar widget

The status bar, its connected variable is a string. Names for text view/edit widget in supported toolkits: GTK "StatusBar", Qt3 and Qt4 not supported.

Text view/edit widget

The text view/edit, its connected variable is a string. Names for text view/edit widget in supported toolkits: GTK "TextView", Qt3 and Qt4 "TextEdit".

Toggle button widget

The toggle button, a button with memory, its connected variable must be a boolean. Each time the button is pressed, it changes its state: from on to off or viceversa. In off state the variable is "False", in on state the variable is "True". Names for toggle button widget in supported toolkits: GTK "ToggleButton", Qt3 and Qt4 "PushButton" with toggle attribute on.

GTK

This is the part of the reference manual specific to GTK widgets toolkit.

Module dependencies

AVC GTK depends on PyGTK the python wrapper for GTK libraries. AVC GTK imports the following modules from PyGTK.

import gtk
import gobject

Widget namimg

Glade a GTK allow duplicated naming of widgets.

Status bar widget

AVC uses the GTK status bar widget as a simple output label. Only context #1 with one or none message on status bar stack is used.

Qt3

This is the part of the reference manual specific to Qt3 widgets toolkit.

Module dependencies

AVC Qt3 depends on PyQt v3 the python bindings for Qt v3 application framework. AVC Qt3 imports the following modules from PyQt.

import qt

Widget namimg

Qt3 Designer and Qt3 do not allow duplicated naming of widgets. So use the 'double underscore' mechanism to differentiate widgets names.

Application class

The application that uses AVC must be instantiated from an application class that is derived from the QApplication class and from the AVC class. Let suppose that the application class name is "theApp", the application class statement will be class theApp(QApplication,AVC):.

Qt4

This is the part of the reference manual specific to Qt4 widgets toolkit.

Module dependencies

AVC Qt4 depends on PyQt v4 the python bindings for Qt v4 application framework. AVC Qt4 imports the following modules from PyQt.

import PyQt4.Qt as qt

Widget namimg

Qt4 Designer and Qt4 do not allow duplicated naming of widgets. So use the 'double underscore' mechanism to differentiate widgets names.

Application class

The application that uses AVC must be instantiated from an application class that is derived from the QApplication class and from the AVC class. Let suppose that the application class name is "theApp", the application class statement will be class theApp(QApplication,AVC):.


Examples

A simple example for GTK

example1_gtk.png This simple example shows how AVC can manage data exchange from widget to widget without any specific code in the application. The program creates a window with two widgets: a spin button and a label. When the value in the spin button is changed by clicking on up or down arrows or by entering it with the keyboard, the new value is displayed into the label.

Python source of 'AVC GTK example 1'
#!/usr/bin/python

import gtk				# gimp tool kit bindings
import gtk.glade			# glade bindings

from avc.avcgtk import *		# AVC for GTK

GLADE_XML = 'example1_gtk.glade'	# GUI glade descriptor


class Example(AVC):
  "A spin button whose value is replicated into a label"
  
  def __init__(self):
  
    # create GUI
    self.glade = gtk.glade.XML(GLADE_XML)
    
    # autoconnect GUI signal handlers
    self.glade.signal_autoconnect(self)
    
    # the variable holding the spin button value
    self.spin_value = 0


  def on_destroy(self,window):
    "Terminate program at window destroy"
    gtk.main_quit()


#### MAIN

example = Example()			# instantiate the application
example.avc_init()			# connect widgets with variables
gtk.main()				# run GTK event loop until quit

The GUI layout was previously edited with Glade and saved to the file 'example1_gtk.glade'.

The key points of the example regarding AVC are the following.

Download the sample program example1_gtk.py

and the Glade descriptor example1_gtk.glade

A counter example for GTK

example2_gtk.png This example shows how AVC can manage data input from a check button widget to the application and from the application to a label widget without any specific code in the application. The program creates a window with two widgets: a check button and a label. The label displays the value of an integer counter. The check button controls the increment speed of the counter. Initially, it is unchecked meaning that the increment speed of the counter is 2 units per second. When the user checks the check button the increment speed grows to 10 units per seconds and returns to the initial value (2) when the check button is unchecked again.

Python source of 'AVC GTK example 2'
#!/usr/bin/python

import gobject				#--
import gtk				#- gimp tool kit bindings
import gtk.glade			# glade bindings

from avc.avcgtk import *		# AVC for GTK

GLADE_XML = 'example2_gtk.glade'	# GUI glade descriptor
LOW_SPEED = 500				#--
HIGH_SPEED = 100			#- low and high speed period (ms)


class Example(AVC):
  """
  A counter displayed in a label widget whose count speed can be
  accelerated by checking a check button.
  """

  def __init__(self):

    # create GUI
    self.glade = gtk.glade.XML(GLADE_XML)

    # autoconnect GUI signal handlers
    self.glade.signal_autoconnect(self)

    # the counter variable and its speed status
    self.counter = 0
    self.high_speed = False

    # start counter incrementer at low speed
    gobject.timeout_add(LOW_SPEED,self.incrementer) 


  def incrementer(self):
    """
    Counter incrementer: increment period = LOW_SPEED, if high speed
    is False, increment period = HIGH_SPEED otherwise. Return False to
    destroy previous timer.
    """
    self.counter += 1
    if self.high_speed:
      period = HIGH_SPEED
    else:
      period = LOW_SPEED
    gobject.timeout_add(period,self.incrementer) 
    return False


  def on_destroy(self,window):
    "Terminate program at window destroy"
    gtk.main_quit()


#### MAIN

example = Example()			# instantiate the application
example.avc_init()			# connect widgets with variables
gtk.main()			 	# run GTK event loop until quit

The GUI layout was previously edited with Glade and saved to the file 'example2_gtk.glade'.

The key points of the example regarding AVC are the following.

Download the sample program example2_gtk.py

and the Glade descriptor example2_gtk.glade

A complex example for GTK

example3_gtk.png This example shows a table of all widget/variable type combinations supported by AVC. The program creates a window with three columns: the first shows the type of the connected variable, the second shows all the widgets that can be connected to that type of variable, the third shows the current value of each variable. Each row of the window represent a widgets/variable combination. Row 1: memoryless button with boolean variable, pressed = True, unpressed = False. Row 2: buttons with memory, toggle and check buttons, pressed = True, unpressed = False. Row 3: mutually exclusive choices widgets, radiobuttons numbered from 0 to 2 and a combo box with 3 items, index variable = number of checked radiobutton and selected item of combo box. Row 4: integer input/output widgets, spin button, entry and slider. Row 5: float input/output widgets, spin button, entry and slider. Row 6: string input/output widget, entry. Row 7: string input/output widget, text view/edit. Row 8: status messages, status bar. The text label widget is used in all output modes for the column of the connected variable values. The program increment the value of each connected variable looping top-bottom at three rows per seconds. The user can also change the connected variables interacting with the widgets.

Python source of 'AVC GTK example 3'
#!/usr/bin/python

import gobject				#--
import gtk				#- gimp tool kit bindings
import gtk.glade			# glade bindings

from avc.avcgtk import *		# AVC for GTK

GLADE_XML = 'example3_gtk.glade'	# GUI glade descriptor
INCREMENTER_PERIOD = 333		# ms


class Example(AVC):
  "A table of all supported widget/control type combinations"

  def __init__(self):

    # create GUI
    self.glade = gtk.glade.XML(GLADE_XML)

    # autoconnect GUI signal handlers
    self.glade.signal_autoconnect(self)

    # the control variables
    self.boolean1 = False
    self.boolean2 = False
    self.radio = 0
    self.integer = 0
    self.float = 0.0
    self.string = ''
    self.textview = ''
    self.status = ''

    # start variables incrementer
    increment = self.incrementer()
    gobject.timeout_add(INCREMENTER_PERIOD,increment.next)


  def incrementer(self):
    """
    Booleans are toggled, radio button index is rotated from first to last,
    integer is incremented by 1, float by 0.5, string is appended a char
    untill maxlen when string is cleared, text view/edit is appended a line
    of text untill maxlen when it is cleared. Status bar message is toggled.
    Return True to keep timer alive.
    """
    while True:

      self.boolean1 = not self.boolean1
      yield True

      self.boolean2 = not self.boolean2
      yield True

      if self.radio >= 2:
        self.radio = 0
      else:
        self.radio += 1
      yield True

      self.integer += 1
      yield True

      self.float += 0.5
      yield True

      if len(self.string) >= 10:
        self.string = 'A'
      else:
        self.string += 'A'
      yield True

      if len(self.textview) >= 200:
        self.textview = ''
      else:
        self.textview += 'line of text, line of text, line of text\n'
      yield True

      if not self.status:
        self.status = 'status message'
      else:
        self.status = ''
      yield True


  def on_destroy(self,window):
    "Terminate program at window destroy"
    gtk.main_quit()


#### MAIN

example = Example()			# instantiate the application
example.avc_init()			# connect widgets with variables
gtk.main()			 	# run GTK event loop until quit

The GUI layout was previously edited with Glade and saved to the file 'example3_gtk.glade'.

The key points of the example regarding AVC are the following.

Download the sample program example3_gtk.py

and the Glade descriptor example3_gtk.glade

A simple example for Qt3

example1_qt3.png This simple example shows how AVC can manage data exchange from widget to widget without any specific code in the application. The program creates a window with two widgets: a spin button and a label. When the value in the spin button is changed by clicking on up or down arrows or by entering it with the keyboard, the new value is displayed into the label.

Python source of 'AVC Qt3 example 1'
#!/usr/bin/python

from qt import * 			# Qt interface
from qtui import *			# ui files realizer
import sys				# system support

from avc.avcqt3 import *		# AVC for Qt3

UI_FILE = 'example1_qt3.ui'


class Example(QApplication,AVC):
  "A spin box whose value is replicated into a text label"

  def __init__(self,sys_argv):

    # create GUI
    QApplication.__init__(self,sys_argv)
    self.root = QWidgetFactory.create(UI_FILE)
    self.setMainWidget(self.root)
    self.root.show()
    
    # the variable holding the spinbox value
    self.spin_value = 0


#### MAIN

example = Example(sys.argv)		# instantiate the application
example.avc_init()			# connect widgets with variables
example.exec_loop()			# run Qt event loop until quit

The GUI layout was previously edited with Qt3 Designer and saved to the file 'example1_qt3.ui'.

The key points of the example regarding AVC are the following.

Download the sample program example1_qt3.py

and the UI descriptor example1_qt3.ui

A counter example for Qt3

example2_qt3.png This example shows how AVC can manage data input from a check button widget to the application and from the application to a label widget without any specific code in the application. The program creates a window with two widgets: a check button and a label. The label displays the value of an integer counter. The check button controls the increment speed of the counter. Initially, it is unchecked meaning that the increment speed of the counter is 2 units per second. When the user checks the check button the increment speed grows to 10 units per seconds and returns to the initial value (2) when the check button is unchecked again.

Python source of 'AVC Qt3 example 2'
#!/usr/bin/python

from qt import * 			# Qt interface
from qtui import *			# ui files realizer
import sys				# system support

from avc.avcqt3 import *		# AVC for Qt3

UI_FILE = 'example2_qt3.ui'
LOW_SPEED = 500				#--
HIGH_SPEED = 100			#- low and high speed period (ms)


class Example(QApplication,AVC):
  """
  A counter displayed in a Label widget whose count speed can be
  accelerated by checking a check box.
  """

  def __init__(self,sys_argv):

    # create GUI
    QApplication.__init__(self,sys_argv)
    self.root = QWidgetFactory.create(UI_FILE)
    self.setMainWidget(self.root)
    self.root.show()
    
    # the counter variable and its speed status
    self.counter = 0
    self.high_speed = False

    # start counter incrementer at low speed
    self.timer = qt.QTimer(self)
    self.connect(self.timer,qt.SIGNAL("timeout()"),self.incrementer)
    self.timer.start(LOW_SPEED)


  def incrementer(self):
    """
    Counter incrementer: increment period = LOW_SPEED, if high speed
    is False, increment period = HIGH_SPEED otherwise.
    """
    self.counter += 1
    if self.high_speed:
      period = HIGH_SPEED
    else:
      period = LOW_SPEED
    self.timer.stop()
    self.timer.start(period) 


#### MAIN

example = Example(sys.argv)		# instantiate the application
example.avc_init()			# connect widgets with variables
example.exec_loop()			# run Qt event loop until quit

The GUI layout was previously edited with Qt3 Designer and saved to the file 'example2_qt3.ui'.

The key points of the example regarding AVC are the following.

Download the sample program example2_qt3.py

and the UI descriptor example2_qt3.ui

A complex example for Qt3

example3_qt3.png This example shows a table of all widget/variable type combinations supported by AVC. The program creates a window with three columns: the first shows the type of the connected variable, the second shows all the widgets that can be connected to that type of variable, the third shows the current value of each variable. Each row of the window represent a widgets/variable combination. Row 1: memoryless button with boolean variable, pressed = True, unpressed = False. Row 2: buttons with memory, toggle and check buttons, pressed = True, unpressed = False. Row 3: mutually exclusive choices widgets, radiobuttons numbered from 0 to 2 and a combo box with 3 items, index variable = number of checked radiobutton and selected item of combo box. Row 4: integer input/output widgets, spin button, entry and slider. Row 5: float input/output widgets, spin button and entry. Row 6: string input/output widget, entry. Row 7: string input/output widget, text view/edit. The text label widget is used in all output modes for the column of the connected variable values. The program increment the value of each connected variable looping top-bottom at three rows per seconds. The user can also change the connected variables interacting with the widgets.

Python source of 'AVC Qt3 example 3'
#!/usr/bin/python

from qt import * 			# Qt interface
from qtui import *			# ui files realizer
import sys				# system support

from avc.avcqt3 import *		# AVC for Qt3


UI_FILE = 'example3_qt3.ui'
INCREMENTER_PERIOD = 333		# ms


class Example(QApplication,AVC):
  "A table of all supported widget/control type combinations"

  def __init__(self,sys_argv):

    # create GUI
    QApplication.__init__(self,sys_argv)
    self.root = QWidgetFactory.create(UI_FILE)
    self.setMainWidget(self.root)
    self.root.show()
    
    # the control variables
    self.boolean1 = False
    self.boolean2 = False
    self.radio = 0
    self.integer = 0
    self.float = 0.0
    self.string = ''
    self.textview = ''
    
    # start variables incrementer
    self.increment = self.incrementer()
    self.timer = qt.QTimer(self)
    self.connect(self.timer,qt.SIGNAL("timeout()"),self.timer_function)
    self.timer.start(INCREMENTER_PERIOD)


  def timer_function(self):
    self.increment.next()


  def incrementer(self):
    """
    Booleans are toggled, radio button index is rotated from first to last,
    integer is incremented by 1, float by 0.5, string is appended a char
    untill maxlen when string is cleared, text view/edit is appended a line
    of text untill maxlen when it is cleared.
    """
    while True:

      self.boolean1 = not self.boolean1
      yield True

      self.boolean2 = not self.boolean2
      yield True

      if self.radio == 2:
        self.radio = 0
      else:
        self.radio += 1
      yield True

      self.integer += 1
      yield True

      self.float += 0.5
      yield True

      if len(self.string) >= 10:
        self.string = 'A'
      else:
        self.string += 'A'
      yield True

      if len(self.textview) >= 200:
        self.textview = ''
      else:
        self.textview += 'line of text, line of text, line of text\n'
      yield True


#### MAIN

example = Example(sys.argv)		# instantiate the application
example.avc_init()			# connect widgets with variables
example.exec_loop()			# run Qt event loop until quit

The GUI layout was previously edited with Qt3 Designer and saved to the file 'example3_qt3.ui'.

The key points of the example regarding AVC are the following.

Download the sample program example3_qt3.py

and the UI descriptor example3_qt3.ui

A simple example for Qt4

example1_qt4.png This simple example shows how AVC can manage data exchange from widget to widget without any specific code in the application. The program creates a window with two widgets: a spin button and a label. When the value in the spin button is changed by clicking on up or down arrows or by entering it with the keyboard, the new value is displayed into the label.

Python source of 'AVC Qt4 example 1'
#!/usr/bin/python

from PyQt4.QtCore import *		# Qt core
from PyQt4.QtGui import *		# Qt GUI interface
from PyQt4.uic import *			# ui files realizer
import sys				# system support

from avc.avcqt4 import *		# AVC for Qt4

UI_FILE = 'example1_qt4.ui'


class Example(QApplication,AVC):
  "A spin box whose value is replicated into a text label"

  def __init__(self,sys_argv):

    # create GUI
    QApplication.__init__(self,sys_argv)
    self.root = loadUi(UI_FILE)
    self.root.show()
    
    # the variable holding the spin box value
    self.spin_value = 0
    

#### MAIN

example = Example(sys.argv)		# instantiate the application
example.avc_init()			# connect widgets with variables
example.exec_()				# run Qt event loop until quit

The GUI layout was previously edited with Qt4 Designer and saved to the file 'example1_qt4.ui'.

The key points of the example regarding AVC are the following.

Download the sample program example1_qt4.py

and the UI descriptor example1_qt4.ui

A counter example for Qt4

example2_qt4.png This example shows how AVC can manage data input from a check button widget to the application and from the application to a label widget without any specific code in the application. The program creates a window with two widgets: a check button and a label. The label displays the value of an integer counter. The check button controls the increment speed of the counter. Initially, it is unchecked meaning that the increment speed of the counter is 2 units per second. When the user checks the check button the increment speed grows to 10 units per seconds and returns to the initial value (2) when the check button is unchecked again.

Python source of 'AVC Qt4 example 2'
#!/usr/bin/python

from PyQt4.QtCore import *		# Qt core
from PyQt4.QtGui import *		# Qt GUI interface
from PyQt4.uic import *			# ui files realizer
import sys				# system support

from avc.avcqt4 import *		# AVC for Qt4


UI_FILE = 'example2_qt4.ui'
LOW_SPEED = 500				#--
HIGH_SPEED = 100			#- low and high speed period (ms)


class Example(QApplication,AVC):
  """
  A counter displayed in a Label widget whose count speed can be
  accelerated by checking a check box.
  """

  def __init__(self,sys_argv):

    # create GUI
    QApplication.__init__(self,sys_argv)
    self.root = loadUi(UI_FILE)
    self.root.show()
    
    # the counter variable and its speed status
    self.counter = 0
    self.high_speed = False

    # start counter incrementer at low speed
    self.timer = qt.QTimer(self)
    self.connect(self.timer,qt.SIGNAL("timeout()"),self.incrementer)
    self.timer.start(LOW_SPEED)


  def incrementer(self):
    """
    Counter incrementer: increment period = LOW_SPEED, if high speed
    is False, increment period = HIGH_SPEED otherwise.
    """
    self.counter += 1
    if self.high_speed:
      period = HIGH_SPEED
    else:
      period = LOW_SPEED
    self.timer.stop()
    self.timer.start(period) 


#### MAIN

example = Example(sys.argv)		# instantiate the application
example.avc_init()			# connect widgets with variables
example.exec_loop()			# run Qt event loop until quit

The GUI layout was previously edited with Qt4 Designer and saved to the file 'example2_qt4.ui'.

The key points of the example regarding AVC are the following.

Download the sample program example2_qt4.py

and the UI descriptor example2_qt4.ui

A complex example for Qt4

example3_qt4.png This example shows a table of all widget/variable type combinations supported by AVC. The program creates a window with three columns: the first shows the type of the connected variable, the second shows all the widgets that can be connected to that type of variable, the third shows the current value of each variable. Each row of the window represent a widgets/variable combination. Row 1: memoryless button with boolean variable, pressed = True, unpressed = False. Row 2: buttons with memory, toggle and check buttons, pressed = True, unpressed = False. Row 3: mutually exclusive choices widgets, radiobuttons numbered from 0 to 2 and a combo box with 3 items, index variable = number of checked radiobutton and selected item of combo box. Row 4: integer input/output widgets, spin button, entry and slider. Row 5: float input/output widgets, spin button and entry. Row 6: string input/output widget, entry. Row 7: string input/output widget, text view/edit. The text label widget is used in all output modes for the column of the connected variable values. The program increment the value of each connected variable looping top-bottom at three rows per seconds. The user can also change the connected variables interacting with the widgets.

Python source of 'AVC Qt4 example 3'
#!/usr/bin/python

from PyQt4.QtCore import *		# Qt core
from PyQt4.QtGui import *		# Qt GUI interface
from PyQt4.uic import *			# ui files realizer
import sys				# system support

from avc.avcqt4 import *		# AVC for Qt4

UI_FILE = 'example3_qt4.ui'
INCREMENTER_PERIOD = 333		# ms


class Example(QApplication,AVC):
  "A table of all supported widget/control type combinations"

  def __init__(self,sys_argv):

    # create GUI
    QApplication.__init__(self,sys_argv)
    self.root = loadUi(UI_FILE)
    self.root.show()

    # group all radio buttons into a button group. Button group not
    # managed by Qt4 Designer ?!
    self.radio_button0 = self.root.findChild(QWidget,'radio__button0')
    self.radio_button1 = self.root.findChild(QWidget,'radio__button1')
    self.radio_button2 = self.root.findChild(QWidget,'radio__button2')
    self.radio_button_group = QButtonGroup()
    self.radio_button_group.addButton(self.radio_button0,0)
    self.radio_button_group.addButton(self.radio_button1,1)
    self.radio_button_group.addButton(self.radio_button2,2)
    
    # the control variables
    self.boolean1 = False
    self.boolean2 = False
    self.radio = 0
    self.integer = 0
    self.float = 0.0
    self.string = ''
    self.textview = ''
    
    # start variables incrementer
    self.increment = self.incrementer()
    self.timer = QTimer(self)
    self.connect(self.timer,SIGNAL("timeout()"),self.timer_function)
    self.timer.start(int(INCREMENTER_PERIOD))


  def timer_function(self):
    self.increment.next()


  def incrementer(self):
    """
    Booleans are toggled, radio button index is rotated from first to last,
    integer is incremented by 1, float by 0.5, string is appended a char
    untill maxlen when string is cleared, text view/edit is appended a line
    of text untill maxlen when it is cleared.
    """
    while True:

      self.boolean1 = not self.boolean1
      yield True

      self.boolean2 = not self.boolean2
      yield True

      if self.radio == 2:
        self.radio = 0
      else:
        self.radio += 1
      yield True

      self.integer += 1
      yield True

      self.float += 0.5
      yield True

      if len(self.string) >= 20:
        self.string = 'A'
      else:
        self.string += 'A'
      yield True

      if len(self.textview) >= 200:
        self.textview = ''
      else:
        self.textview += 'line of text, line of text, line of text\n'
      yield True


#### MAIN

example = Example(sys.argv)		# instantiate the application
example.avc_init()			# connect widgets with variables
example.exec_()				# run Qt event loop until quit

The GUI layout was previously edited with Qt4 Designer and saved to the file 'example3_qt4.ui'.

The key points of the example regarding AVC are the following.

Download the sample program example3_qt4.py

and the UI descriptor example3_qt4.ui


© Copyright 2006-2007 by Fabrizio Pollastri