This page contains links to some useful code snippets. Longer, more detailed code examples can be found via the Overviews_and_Guides page.
Contents
Put any code snippet for PyKDE and PyQt that you find useful here. If the code is longer than one screen at normal point size, it might be better if you provide a rationale and a link, or just put it on a separate page.
PyQt 5
QML callback function: How to pass and execute callback functions with QML.
Threading, Signals and Slots: How to perform work in one thread and use the result in another.
PyQt 4
Decoding Japanese Text: The response to a posting on comp.lang.python, asking about decoding text for display in a GUI.
Screenshots of various database and 3D graphics widgets, with code to follow soon. [dead link!]
Using GStreamer with PyQt: How to use the GStreamer bindings for Python with PyQt (see Multimedia Resources for more multimedia links).
Infrastructure
Threading, Signals and Slots: How to perform work in one thread and use the result in another. (Qt 4)
PyQt/AutoConnectingSlots: How to have the signals of widgets automatically connected to Python defined slots.
Handling Qt's internal item MIME type: How to implement a drop handler in a model to handle items dragged from standard models.
Handling an internal Qt MIME type: How to handle the internal MIME type used for drag and drop between item views.
Binding widget properties to Python variables: Using Python's property system to access Qt properties and bind them to variables.
Making non-clickable widgets clickable: How to misuse event filters to add clicked() signals to widgets.
Undo and redo with line edits: An incomplete example showing basic undo/redo handling.
Using a signal mapper: How to associate values with signals from many widgets and use one slot to handle them all.
Sending Python values with signals and slots: How to communicate Python values via Qt's signals and slots mechanism.
Using a translation of Qt: How to display standard dialogs and messages in your local language.
Getting the version numbers of Qt, SIP and PyQt: How to obtain the version information for the Qt-related modules you are using.
Writing a client for a zeromq service: How to access a zeromq service while keeping the GUI responsive.
Painting
Drawing highlighted rows in a calendar widget: How to highlight the current week in a QCalendarWidget subclass.
Windows with gradient backgrounds: How to change the background colour of a window to use a gradient instead of the standard colour in the palette.
Fading Between Widgets: How to create a stacked widget that fades between widgets on different pages.
Fading and unfading a widget with a delay: How to produce a simple fade-unfade animation.
Paint on an image: How to paint text and graphics on an image.
Painting and clipping demonstration: A demonstration showing how to clip what a painter draws on a widget.
Clipping SVG output: How to clip Scalable Vector Graphics (SVG) output when drawing with QPainter.
GraphicsView_-_SimpleAnimation: How to construct a very simple animation using QGraphicsView, QGraphicsItem, QGraphicsItemAnimation and QTimeLine. (Qt4)
Python syntax highlighting: How to add syntax highlighting to a QPlainTextEdit widget. (Qt4)
Painting an overlay on an image: How to paint one image onto another.
Movie splash screen: How to paint a movie on a splash screen instead of a static image.
Extend Two QPixmap: How to paint merge two QPixmaps into one QPixmap.
Widgets
Widgets in a layout: How to create widgets and put them in a layout.
Show an image using a label: How to display an image.
A full widget waiting indicator: How to draw a busy/waiting indicator over an entire widget.
Handling context menus: The different ways you can handle context menus.
Five minute steps in a QTimeEdit: Customise the behaviour of QTimeEdit by subclassing.
Customising tab bars: How to change the size allocated to tabs in tab bars and tab widgets.
Customising a tab to contain a menu: How to put a menu button in a tab bar.
Adding auto-completion to a QLineEdit: How to use a QStringListModel to provide data for auto-completion.
Adding tab-completion to a QLineEdit: How to handle key events to enable tab-completion.
Adding custom signals to a simple painted widget: How to declare and use custom signals.
Creating a widget with a fixed aspect ratio: How to ensure that a widget keeps a certain aspect ratio when it is resized.
Compass widget: A simple custom widget, showing how to handle paint events.
Adding a background image to an MDI area: How to display a non-tiled image in the background of an MDI area widget.
Selecting a region of a widget: How to use a QRubberBand to select part of a widget.
Distinguishing between click and double click: How to handle a double click without acting on the first click.
Item Views
Using a different view with QComboBox: How to replace the standard view used for a combo box's pop-up menu.
Sorting numbers in columns: How to sort a table by the numbers in a given column.
Animated items using delegates: How to animate items in a list view, using a custom delegate, timer events and a signal.
Animated items using delegates and movies: How to animate items in a list view - an improved version of the previous example.
Adding items to a list widget: How to add items to a QListWidget instance.
A custom Python class-based 1D model: A drag and drop-enabled model which holds Python objects.
Reading selections from a selection model: How to read selections and update an underlying model.
Creating a context menu for a tree view: How to determine the level of nesting in a tree view and create an appropriate menu.
Selecting items in unrelated views: How to use selection models to relate items in views with different models.
Showing a subset of a model in a view: How to show a model in one view while showing a subset of the same model in another view.
WebKit
Using a Custom Protocol with QtWebKit: How to add support for a custom protocol to an embedded Web browser.
Embedding Widgets in Web Pages: How to embed widgets written in Python into Web pages shown with QWebView.
Exposing Qt Classes to QtWebKit: How to wrap Qt classes and expose them to an embedded Web browser.
Augmented Web Browser: A Web browser with a side panel that shows a table of contents for the document being displayed.
Multimedia
Playing a sound with QtMultimedia: How to use QAudioOutput to play a sound.
Drag and Drop
Exporting a file to other applications: How to drag data as a file for other applications to load.
Databases
Removing a database: How to remove a database and optionally reload it again without getting warnings from Qt.
Qt Designer and uic
Previewing a form: How to load and display a form created in Qt Designer.
PyQt 3
Note: PyQt 3 examples were not transferred from the old PyQt Wiki.
CustomListViewItems: How to customize the painting of list view items. (Qt 3)
ListBoxAndListViewIterators: How to loop over all items in list boxes and list views. (Qt 3)
DragAndDropWithPyQt: How to make use of drag and drop without subclassing. (Qt 3)
CreatingMdiApplications: Some links and pointers for MDI development. (Qt 3)
Wrapper_For_QWidgetFactory: How to set up the slot connections automatically in PyQt. (Qt 3)
LoadingUIFilesAtRuntime: How to load classes (as opposed to instances) directly from UI files, at runtime. (Qt 3)
SimpleQScintillaExample: a simple demonstration of QScintilla use with PyQt. (Qt 3)
SignalDecorator: a decorator that provides the signal signature for a method. (Qt 3)
ScrollableGroupBox: A simple way to get a scrollable groupbox. (Qt 3)
Capturing_Output_from_a_Process: How to capture output from a process and show it in a text editor/browser. (Qt 3)
Printing_a_Worksheet: How to print the contents of a worksheet onto a single page of A4 paper. (Qt 3)