Python's PyQt Toolkit
by Boudewijn Rempt


Listing One
from constants import TRUE, FALSE
from qt import *
class guiComboBox(QComboBox):
 def __init__(self, parent):
  QComboBox.__init__(self, FALSE, parent)
  self.setAutoCompletion(TRUE)
  self.data2key = {}
  self.key2data = {}
  self.connect(self, SIGNAL("activated(const char *)"), self.slotItemSelected)
 def insertItem(self, text, key, index=-1):
  QComboBox.insertItem(self, text, index)
  self.data2key [self.count() - 1] = key
  self.key2data [key]=self.count() - 1
 def currentKey(self):
  return self.data2key[self.currentItem()]
 def setCurrentItem(self, key):
  if self.key2data.has_key(key):
    QComboBox.setCurrentItem(self, self.key2data[key])
 def slotItemSelected(self, key):
  item=self.currentKey()
  self.emit( PYSIGNAL("itemSelected"),(item,key) )

Listing Two
class Q_EXPORT QComboBox : public QWidget
 ...
public:
 ...
QComboBox( QWidget *parent=0, const char *name=0 );
QComboBox( bool rw, QWidget *parent=0, const char *name=0 );
 ...
void   insertItem( const QString &text, int index=-1 );
void   insertItem( const QPixmap &pixmap, int index=-1 );
void   insertItem( const QPixmap &pixmap, const QString &text, int index=-1 );
 ...
virtual void        setCurrentItem( int index );
 ...

Listing Three
from qt import *
import sys
class ApplicationWindow(QMainWindow):
  def __init__(self):
    QMainWindow.__init__(self, None, 'main window',
                         Qt.WidgetFlags.WDestructiveClose)
    self.view=QMultiLineEdit(self)
    self.setCentralWidget(self.view)
def main(args):
  app = QApplication(args)
  mainwin = ApplicationWindow()
  mainwin.show()
  app.connect(app, SIGNAL('lastWindowClosed()'), app, SLOT('quit()'))
  app.exec_loop()
if __name__=="__main__":
  main(sys.argv)

Listing Four
class myKnob(QWidget):
  def __init__(self,*args):
  self.value=1
    ...
  def twiddle(self):
    self.value=self.value + 1
    self.emit(PYSIGNAL("sigTwiddled"),(self.value,))
class myDisplay(QLabel):
  def __init__(self,*args):
    ...
  def slotValueC`hanged(self, value):
    if value<>self.value:
    self.value=value
    self.setText(value)
class myView(QWidget):
  def __init__(self, *args):
    ...
    self.display=myDisplay(self)
    self.knob=myKnob(self)
    self.connect(self.knob, PYSIGNAL("sigTwiddled"), 
self.display.slotValueChanged)

Listing Five
# Copyright (C) 1997, 1998, 2000 by Bernhard Herzog
 ...
 class Connector:
 ...
     def Connect(self, object, channel, function, args):
         idx = id(object)
         if self.connections.has_key(idx):
             channels = self.connections[idx]
         else:
             channels = self.connections[idx] = {}
         if channels.has_key(channel):
             receivers = channels[channel]
         else:
             receivers = channels[channel] = []
         info = (function, args)
         try:
             receivers.remove(info)
         except ValueError:
             pass
         receivers.append(info)
 ...





2

