1. qt
    1. Introduction
    2. Installation
    3. Classes
    4. General
      1. qt:init
      2. qt:run
    5. Widgets
      1. qt:widget
      2. qt:delete
      3. qt:show
      4. qt:hide
      5. qt:find
      6. qt:pixmap
      7. qt:update
    6. Properties
      1. qt:property
    7. Signals and receivers
      1. qt:receiver
      2. qt:connect
    8. Timers
      1. qt:timer
      2. qt:start
      3. qt:stop
    9. Lists
      1. qt:clear
      2. qt:add
      3. qt:item
      4. qt:set-headers
    10. Dialogs
      1. qt:message
    11. Sound
      1. qt:sound
      2. qt:play
      3. qt:stop
    12. Miscellaneous
      1. qt:gl
      2. qt:classname
    13. Example
    14. History
    15. License

qt

Introduction

This extension provides a lightweight and relatively easy to use interface to Trolltech's Qt 4 GUI toolkit. It has currently only been tested on Linux and Mac OS X.

Memory management is completely manual (with the exception of child widgets, which are deleted when their parent is). To release the resources of a widget or other Qt objects, call qt:delete.

This Qt binding is necessarily incomplete (since it is intended to be leightweight). If you miss certain functionality like widget-specific operations, contact the author.

Installation

The QTDIR environment variable must be set to the installation directory of Qt, like this:

QTDIR=/usr/local/Trolltech/Qt-4.2.0 chicken-setup qt

Under Mac OS X, an application bundle named ChickenQt.app is generated (and no dynamically loadable extension as such). The bundle will load and evaluate a Scheme file named init.scm from it's Contents/MacOS directory that can be modified to contain the actual application, or further load compiled code. Use (qt:bundle-path) to obtain the bundle directory. An example initialization file is provided that just pops up a dialog and asks for a file to load and execute. You can modify it to load another file (e.g. "egg-browser.scm") that is placed in the same directory:

(require-extension chicken-more-macros)
(require 'match)
(define basedir (pathname-directory (qt:bundle-path)))
(load (make-pathname basedir "egg-browser.scm"))

If you find that when chicken-setup runs qmake, an XCode project is generated rather than a Makefile, you need to modify QMAKESPEC like this:

export QMAKESPEC=macx-g++

Classes

The following TinyCLOS classes are exposed:

<qt>
 <qt-object>
  <at-application>
  <qt-widget>
  <qt-receiver>
  <qt-timer>
  <qt-sound>
 <qt-pixmap>

General

qt:init

[procedure] (qt:init) 

Initializes Qt (including any command-line processing) and returns the application object, an instance of <qt-application>. Performs an implicit

(qt:connect <application> "lastWindowClosed()" <application> "quit()")

qt:run

[procedure] (qt:run [ONCE])

Runs the Qt event loop. If ONCE is given and true, then the procedure returns once all pending events have been processed (use (qt:run #t) in a loop when you want to do some custom idle processing, for example).

Widgets

qt:widget

[procedure] (qt:widget UIXML [PARENT])

Parses the UI description in the string UIXML, which should be the XML representation of a user interface created by the Qt designer application. Returns an instance of <qt-widget>, the toplevel widget. If PARENT is given, the newly created widget will be a child of this.

qt:delete

[generic] (qt:delete OBJECT)

Deletes OBJECT, which should be an instance of <qt-object> or <qt-pixmap>.

qt:show

[procedure] (qt:show WIDGET)

Shows the given widget.

qt:hide

[procedure] (qt:hide WIDGET)

Hides the widget from view.

qt:find

[procedure] (qt:find WIDGET NAME)

Returns the direct or indirect child widget of WIDGET named NAME (a string) or #f if no such child widget could be found.

qt:pixmap

[procedure] (qt:pixmap FILENAME)

Loads an image file and returns an instance of <qt-image>.

qt:update

[procedure] (qt:update WIDGET)

Schedules a repaint event for the given widget.

Properties

qt:property

[procedure] (qt:property WIDGET PROP)
[setter] (set! (qt:property WIDGET PROP) VALUE)

Get or set a widget property with the name PROP (a string or symbol). See the Qt documentation for more information about which widget supports which properties. Value conversion is automatically, the following value types are supported:

Property (C++) type    Scheme type
QString                string
int                    integer
double                 number
bool                   boolean
char                   char
QPixmap                <qt-image>
Point                  s32vector
Size                   s32vector
Rect                   s32vector
PointF                 f64vector
SizeF                  f64vector
RectF                  f64vector

Signals and receivers

qt:receiver

[procedure] (qt:receiver THUNK)

Returns an instance of <qt-receiver> that when connected to a Qt signal will invoke PROC once the signal is emitted.

qt:connect

[procedure] (qt:connect SOURCE SIGNAL DESTINATION [SLOT])

Connects the signal SIGNAL from the <qt-object> SOURCE to the slot SLOT from DESTINATION. If no slot is given, then "slot()" is assumed (which is the only slot implemented in <qt-receiver> instances). Signals and slots should be strings and follow the normal syntax used by Qt.

Timers

qt:timer

[procedure] (qt:timer SECONDS)

Creates and returns a timer object which can be connected to a receiver and which will emit "timeout()" signals every SECONDS.

qt:start

[procedure] (qt:start TIMER)

Starts the given timer.

qt:stop

[generic] (qt:stop TIMER)

Stops the given timer.

Lists

qt:clear

[procedure] (qt:clear WIDGET)

Clears all entries from WIDGET which should be a QListWidget.

qt:add

[procedure] (qt:add WIDGET STRING)

Adds a new entry to a QListWidget, QComboBox or QTreeWidget. In the latter case, the columns should be separated by the | character (vertical bar).

qt:item

[procedure] (qt:item WIDGET INDEX)

Returns the text of the QListWidget item with the given index.

qt:set-headers

[procedure] (qt:set-headers WIDGET STRING)

Sets the column headers in a QTreeWidget. Columns should be separated by the | character (vertical bar).

Dialogs

qt:message

[procedure] (qt:message TEXT #!key caption parent button1 button2 button3)

Opens a QMessageBox with the given properties and returns the index of the pressed button.

[procedure] (qt:get-open-filename CAPTION DIRECTORY #!key parent options filter)

Shows a modal file-selection dialog and returns the selected filename (or "", if the dialog was canceled). options should be a list with zero or more of the following keywords:

show-dirs-only:
dont-resolve-symlinks:
dont-confirm-overwrites:
dont-use-sheet:
dont-use-native-dialog:
[procedure] (qt:get-save-filename CAPTION DIRECTORY #!key parent options filter)

Shows a modal file-selection for saving.

[procedure] (qt:get-directory CAPTION DIRECTORY #!key parent options filter)

Shows a modal directory-selection dialog.

Sound

qt:sound

[procedure] (qt:sound FILENAME)

Loads a sound-file and returns an instance of <qt-sound>.

qt:play

[procedure] (qt:play SOUND)

Plays the sound asynchronously.

qt:stop

[generic] (qt:stop SOUND)

Stops a currently playing sound.

Miscellaneous

qt:gl

[procedure] (qt:gl NAME PARENT INIT RESIZE PAINT)

Creates and returns a QGLWidget. INIT should be zero-argument procedure called to initialuze the OpenGL context. RESIZE should be a two-argument procedure called when the widget is resized and receives the new width and height. PAINT is a zero-argument procedure called when the widget should repaint itself. GL output will be automatically flushed.

qt:classname

[procedure] (qt:classname OBJECT)

Returns the name of the Qt class of which OBJECT is an instance.

Example

Given the file hello.ui:

<ui version="4.0" >
 <class>Form</class>
 <widget class="QWidget" name="Form" >
  <property name="geometry" >
   <rect>
    <x>0</x>
    <y>0</y>
    <width>295</width>
    <height>144</height>
   </rect>
  </property>
  <property name="windowTitle" >
   <string/>
  </property>
  <widget class="QLabel" name="label" >
   <property name="geometry" >
    <rect>
     <x>40</x>
     <y>30</y>
     <width>121</width>
     <height>31</height>
    </rect>
   </property>
   <property name="font" >
    <font>
     <pointsize>15</pointsize>
     <weight>75</weight>
     <bold>true</bold>
    </font>
   </property>
   <property name="text" >
    <string>Hello, world!</string>
   </property>
   <property name="alignment" >
    <set>Qt::AlignCenter</set>
   </property>
  </widget>
  <widget class="QPushButton" name="quitButton" >
   <property name="geometry" >
    <rect>
     <x>180</x>
     <y>90</y>
     <width>75</width>
     <height>31</height>
    </rect>
   </property>
   <property name="text" >
    <string>Quit</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

Run this to display the Window:

(use qt utils)
(define a (qt:init))
(define w (qt:widget (read-all "hello.ui")))
(qt:connect (qt:find w "quitButton") "clicked()" a "quit()")
(qt:show w)
(qt:run)

A more interesting example can be found here: egg-browser.zip. On Mac OS X change the reference in egg-browser.scm to "egg-browser.ui" to (make-pathname basedir "egg-browser.ui").

History

0.3
generated application bundle on Mac OS X
0.2
added file dialogs and QSound support, fixed bug by making qt:show "safe"
0.1
initial release

License

Copyright (c) 2006, Felix L. Winkelmann
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
conditions are met:
  Redistributions of source code must retain the above copyright notice, this list of conditions and the following
    disclaimer. 
  Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
    disclaimer in the documentation and/or other materials provided with the distribution. 
  Neither the name of the author nor the names of its contributors may be used to endorse or promote
    products derived from this software without specific prior written permission. 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
Send bugs, suggestions and ideas to: 
felix@call-with-current-continuation.org
Felix L. Winkelmann
Unter den Gleichen 1
37130 Gleichen
Germany