EuroPython 2006, CERN, Geneva. 2006 David Boddie and Torsten Marek * Qt and Trolltech are registered trademarks of Trolltech ASA



Similar documents
Introduction to GUI programming in Python. Alice Invernizzi

David Boddie. PyCon UK 2007, Birmingham

PyQt for Desktop and Embedded Devices

Qt/Embedded Whitepaper. Trolltech

Surface and Volumetric Data Rendering and Visualisation

Qt 3.3 Whitepaper. Trolltech. Abstract

Building a Python Plugin

BogDan Vatra and Andy Gryc. Qt on Android: Is it right for you?

[PRAKTISCHE ASPEKTE DER INFORMATIK SS 2014]

HBQT-Tutorial. Giovanni Di Maria May 9,

Cross-platform C++ development using Qt

The Most Popular UI/Apps Framework For IVI on Linux

GUI application set up using QT designer. Sana Siddique. Team 5

Qt in Education. The Qt object model and the signal slot concept

KIVY - A Framework for Natural User Interfaces

caqtdm Tutorial Jim Stevens APS Controls Group October 9, 2014

Rapid Application Development with GNOME and Python

Multiprocess System for Virtual Instruments in Python

Mouse and Pointer Settings. Technical Brief

Java 7 Recipes. Freddy Guime. vk» (,\['«** g!p#« Carl Dea. Josh Juneau. John O'Conner

JavaFX Session Agenda

Qt Tips and Tricks. Integrated Computer Solutions Incorporated. Justin Noel and the ICS Team Oct 2010

Building Applications With DUIM

Microsoft PowerPoint 2011

ECWM511 MOBILE APPLICATION DEVELOPMENT Lecture 1: Introduction to Android

ANDROID INTRODUCTION TO ANDROID

Table of Contents. Adding Build Targets to the SDK 8 The Android Developer Tools (ADT) Plug-in for Eclipse 9

BACHELOR THESIS. Peter Fabian Interactive Data Visualization Tool

How to Develop Accessible Linux Applications

NDA ISSUE 1 STOCK # CallCenterWorX-Enterprise IMX MAT Quick Reference Guide MAY, NEC America, Inc.

Lecture 1 Introduction to Android

The KPager Handbook. Dirk Doerflinger Antonio Larrosa Jiménez Pino Toscano Matthias Elter Matthias Ettrich

How To Use Gss Software In Trimble Business Center

Beginning Nokia Apps. Development. Qt and HTIVIL5 for Symbian and MeeGo. Ray Rischpater. Apress. Daniel Zucker

Eclipse with Mac OSX Getting Started Selecting Your Workspace. Creating a Project.

DEVELOPMENT OF AN ANALYSIS AND REPORTING TOOL FOR ORACLE FORMS SOURCE CODES

The MaXX Desktop. Workstation Environment. Revised Road Map Version 0.7. for Graphics Professionals

Microsoft Office System Tip Sheet

Professional. SlickEdif. John Hurst IC..T...L. i 1 8 О 7» \ WILEY \ Wiley Publishing, Inc.

Notes Client Tips & Tricks

AUTOMATED CONFERENCE CD-ROM BUILDER AN OPEN SOURCE APPROACH Stefan Karastanev

Guide to SAS/AF Applications Development

ITG Software Engineering

Business Insight Report Authoring Getting Started Guide

Managing Your Desktop with Exposé, Spaces, and Other Tools

Skills for Employment Investment Project (SEIP)

Module One: Getting Started Opening Outlook Setting Up Outlook for the First Time Understanding the Interface...

Mobility Introduction Android. Duration 16 Working days Start Date 1 st Oct 2013

Figure 1 responsive grid system for all view ports, from desktops, tablets and smart phones

Neutron Science Visualization Software

Design document Goal Technology Description

Qt Features for Hybrid Web/Native Application Development

Content Author's Reference and Cookbook

Handout: Word 2010 Tips and Shortcuts

Mobile Development with Qt

VisIt Visualization Tool

Enhancing the SAS Enhanced Editor with Toolbar Customizations Lynn Mullins, PPD, Cincinnati, Ohio

The Remote Desktop Connection Handbook. Brad Hards Urs Wolfer

Getting Started Guide. Chapter 14 Customizing LibreOffice

13 Managing Devices. Your computer is an assembly of many components from different manufacturers. LESSON OBJECTIVES

Android Developer Fundamental 1

Using the Remote Desktop Connection for Mac

CaptainCasa. CaptainCasa Enterprise Client. CaptainCasa Enterprise Client. Feature Overview

What's New in QuarkXPress 10

Microsoft Office System Tip Sheet

QUICK START. GO-Global Cloud 4.1 SETTING UP A LINUX CLOUD SERVER AND HOST INSTALL THE CLOUD SERVER ON LINUX

IOIO for Android Beginners Guide Introduction

PBA.pro Getting Started

MAS 500 Intelligence Tips and Tricks Booklet Vol. 1

Getting Started with Android Programming (5 days) with Android 4.3 Jelly Bean

Teradata SQL Assistant Version 13.0 (.Net) Enhancements and Differences. Mike Dempsey

PeopleTools 8.51 PeopleBook: PeopleSoft Application Designer Lifecycle Management Guide

Native-quality, cross-platform HTML5 apps. Peter Helm

MICROSOFT OUTLOOK 2010 READ, ORGANIZE, SEND AND RESPONSE S

Table of Contents. iii

Mobile application development with QML & PySide

Implementação. Interfaces Pessoa Máquina 2010/ Salvador Abreu baseado em material Alan Dix. Thursday, June 2, 2011

Microsoft PowerPoint 2008

14.1. bs^ir^qfkd=obcib`qflk= Ñçê=emI=rkfuI=~åÇ=léÉåsjp=eçëíë

Getting more out of Matplotlib with GR

DCA. Document Control & Archiving USER S GUIDE

User's Guide FairCom Performance Monitor

Microsoft Outlook Reference Guide for Lotus Notes Users

Developing Exceptional Mobile and Multi-Channel Applications using IBM Web Experience Factory IBM Corporation 1

Rapid GUI Application Development with Python

FileMaker Pro 11. Tutorial

MERLIN. The Quick Start Guide to professional project management ProjectWizards GmbH, Melle, Germany. All rights reserved.

ECWM511 MOBILE APPLICATION DEVELOPMENT Lecture 1: Introduction to Android

USE OF PYTHON AS A SATELLITE OPERATIONS AND TESTING AUTOMATION LANGUAGE

ADOBE MUSE. Building your first website

Avaya Network Configuration Manager User Guide

NVivo 8 Help. Using the Software

Communicator for Mac Help

Transcription:

Introducing PyQt4* for GUI Application Development David Boddie dboddie@trolltech.com Torsten Marek shlomme@gmx.net EuroPython 2006, CERN, Geneva 2006 David Boddie and Torsten Marek * Qt and Trolltech are registered trademarks of Trolltech ASA

What is Qt? Qt is a cross-platform C++ framework for the development of GUI applications. Developed by Trolltech in Norway and Germany Supported on Windows, Mac OS X, Linux and other Unix platforms Available under the GNU General Public License on all supported platforms Also available under a Commercial License for closed source applications Not just a widget toolkit other features for application developers

Features Widgets, layouts, styles (native appearance on each platform) Standard GUI features for applications (menus, toolbars, dock windows) Easy communication between application components (signals and slots) Unified painting system (with transparency, anti-aliasing and SVG support) Rich text processing, display and printing Database support (SQL) and model/view features Input/output and networking Other features Container classes Threading Resources XML processing

PyQt Qt 3: PyQt is a set of bindings for Qt 3 by Phil Thompson at Riverbank Computing. Uses SIP to generate bindings Comprehensive API coverage Dual licensed under the GPL and Commercial licenses Community mailing list with around 500 members PyQt3 1998: First release 2000: Windows release 2002: Mac OS X and Zaurus releases PyQt4 2006: PyQt4 release Wiki at http://www.diotavelli.net/pyqtwiki

PyKDE KDE 3: PyKDE is a set of bindings for KDE 3 by Jim Bublitz that supports these mainstream KDE libraries: DCOP interprocess communication kdecore application and configuration kdeui widgets, dialogs, user interface elements khtml HTML display (used by Konqueror and Safari) kio network transparent communications kparts high-level reusable GUI components kdeprint printing, dialogs, print jobs and management Others (kfile, kmdi, kspell, kdesu, kutils)

Library Structure Qt 3 Qt 4 QtOpenGL QtGui Main Qt classes plus Icon view Workspace Table QtCore QtXml QtSvg Canvas Network SQL OpenGL XML QtNetwork QtSql ActiveQt QMotif Netscape Plugin Qt3Support QtAssistant QtDesigner QtUiTools QtTest QAxContainer QAxServer

QtCore Object and meta-object system: QObject, QMetaObject Basic value types: QByteArray, QString, QDate, QTime, QPoint[F], QSize[F] File system abstraction: QFile, QDir, QIODevice, QTextStream, QDataStream Basic application support: QCoreApplication encapsulates an application QEvent communication (see also signals and slots) QTimer signal-based timed event handling

QtGui Widgets: QCheckBox, QComboBox, QDateTimeEdit, QLineEdit, QPushButton, QRadioButton, QSlider, QSpinBox, etc. Basic value types: QColor, QFont, QBrush, QPen Painting system and devices: QPainter, QPaintDevice, QPrinter, QImage, QPixmap, QWidget Basic application support: QApplication encapsulates a GUI application Rich text: QTextEdit, QTextDocument, QTextCursor

QtGui Display widgets Input widgets Text entry widgets Buttons Scrolling list and tree widgets Tab widgets QGroupBox QLabel QDateEdit QTimeEdit QComboBox QListWidget QGroupBox QRadioButton QCheckBox QTabWidget QLabel QLineEdit QTextEdit

QtGui Range controls Tables Scrolling views Database support Custom widgets QFrame QLabel QSpinBox QSlider QDial QTableWidget AnalogClock QSlider QLabel

Using Widgets Creating a top-level widget window = QWidget() window.resize(480, 360) window.show() Creates a widget Resizes and shows it Creating a child widget button = QPushButton("Press me", window) button.move(200, 200) button.show() Creates a button inside the window Positions and shows it Placing widgets in a layout okbutton = QPushButton("&OK") cancelbutton = QPushButton("&Cancel") layout = QVBoxLayout() layout.addwidget(okbutton) layout.addwidget(cancelbutton) window.setlayout(layout) Creates parent and child widgets Creates a layout to arrange widgets Adds the child widget to the layout

Using Layouts Layouts manage child widgets and are responsible for: Updating their sizes and positions Providing default and minimum sizes One One Two Two One Two Three Three Three Horizontal, vertical and grid layouts Four Five yesbutton = QPushButton("&Yes") nobutton = QPushButton("&No") layout = QHBoxLayout() layout.addstretch(1) layout.addwidget(yesbutton) layout.addwidget(nobutton) namelabel = QLabel("Name:") nameedit = QLineEdit() addresslabel = QLabel("Address:") addressedit = QTextEdit() layout = QGridLayout() layout.addwidget(namelabel, 0, 0) layout.addwidget(nameedit, 0, 1) layout.addwidget(addresslabel,0,0,qt.aligntop) layout.addwidget(addressedit, 0, 1)

Signals and Slots Signals and slots allow objects to communicate with each other via type-safe interfaces. QComboBox QTextEdit Sender and receiver do not need to know about each other Connections can be direct or queued Sender and receiver can be in different threads

Making connections Signals and Slots class MainWindow(QMainWindow): def init (self): QMainWindow. init (self) filemenu = self.menubar().addmenu(self.tr("&file")) exitaction = filemenu.addaction(self.tr("e&xit")) helpmenu = self.menubar().addmenu(self.tr("&help")) aboutaction = helpmenu.addaction(self.tr("&about This Example")) self.connect(exitaction, SIGNAL("triggered()"), qapp, SLOT("quit()")) self.connect(aboutaction, SIGNAL("triggered()"), self.showaboutbox) # Set up the rest of the window. def showaboutbox(self): QMessageBox.information(self, self.tr("about This Example"), self.tr("this example shows how signals and slots are used to\n" "communication between objects in Python and C++."))

Writing an Application Creating an application app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_()) Creates the application Creates and shows the main window (a QMainWindow subclass) Runs the event loop then exits Running an application in different styles myapplication -style plastique myapplication -style motif myapplication -style windows QApplication parses the command line arguments The -style option can be used to override the native style

Main windows usually have Main Window Classes Menus built using QMenu and populated with actions Toolbars built using QToolBar, these usually share actions with menus Dock windows provided by QDockWidget A status bar provided by QStatusBar A central widget containing the main GUI User actions are represented by the QAction class The action system synchronizes menus, toolbars, and keyboard shortcuts It also stores information about tooltips and interactive help

Actions To create an action, you can: Instantiate a QAction object directly Call addaction() on existing QMenu and QToolBar objects Then you can share it with other objects. Sharing actions self.saveaction = QAction(QIcon(":/images/save.png"), self.tr("&save..."), self) self.saveaction.setshortcut(self.tr("ctrl+s")) self.saveaction.setstatustip(self.tr("save the current form letter")) self.connect(self.saveact, QtCore.SIGNAL("triggered()"), self.save)... self.filemenu = self.menubar().addmenu(self.tr("&file")) self.filemenu.addaction(self.saveaction)... self.filetoolbar = self.addtoolbar(self.tr("file")) self.filetoolbar.addaction(self.saveact)

Multiple Document Interface Applications are designed with different user interfaces: Single Document Interface (SDI) applications use multiple main windows, each containing a suitable central widget Multiple Document Interface (MDI) applications use a QWorkspace as the central widget Multiple Document Interface workspace = QWorkspace() workspace.setwindowtitle("simple Workspace Example") for i in range(5): textedit = QTextEdit() textedit.setplaintext("pyqt4 "*100) textedit.setwindowtitle("document %i" % i) workspace.addwindow(textedit) workspace.cascade()

Item Views Item views are complex controls that handle collections of items, each representing a piece of data. QListView or QListWidget QTableView or QTableWidget QTreeView or QTreeWidget Qt 3's item views are populated with item objects Qt 4 has item-based view classes and a model/view framework

Item Views What's the difference between the item-based and model-based approaches? Item-based tree of items tree = QTreeWidget() tree.setcolumncount(2) tree.setheaderlabels(["name", "Address"]) for name, address in phonebook.items(): item = QTreeWidgetItem(tree) item.settext(0, name) item.settext(1, address) tree.show() Model-based version # Given an existing model... tree = QTreeView() tree.setmodel(model) tree.show() table = QTableView() table.setmodel(model) table.show() Items are easy to use You just create them and add them to parent widgets or items It all has to be done by you Models automatically populate views with items Views (of different kinds) can share models We left out the tricky part...

Models and Views Concepts Models hold data for views to display Views access data using indexes Delegates display individual items for views Roles describe the types of data # Reading index = model.index(row, column, parent) data = index.data(index, role) # Writing model.setdata(index, data, role) Models expose pieces of data as items in tables Items can expose tables of child items

Models and Views A simple model class ImageModel(QAbstractTableModel): def init (self, image, parent=none): QAbstractTableModel. init (self, parent) self.image = QImage(image) def rowcount(self, parent): return self.image.height() def columncount(self, parent): return self.image.width() def data(self, index, role): if not index.isvalid(): return QVariant() elif role!= QtCore.Qt.DisplayRole: return QVariant() return QVariant(qGray( self.image.pixel(index.column(), index.row())))

Models and Views With a suitable model, views can be used to display any kind of data: XML data can be displayed in a tree view There's already a Qt example of a DOM-based XML model Torsten decided to write an ElementTree model Torsten's ElementTree model Around 50 lines of code Read-only Copes with quite large files Fast, even compared to pure C++ models

Database Support Like Python, Qt 4 has classes for working with databases These are integrated with the model/view framework Accessing a database with a SQL table model db = QSqlDatabase.addDatabase("QSQLITE") db.setdatabasename(databasename) model = QSqlTableModel(self) model.settable("person") model.seteditstrategy(qsqltablemodel.onmanualsubmit) model.select() view1 = QTableView(self) view1.setmodel(model) view2 = QTableView(self) view2.setmodel(model)

Database Support Python has its own standard database API: Included with many Python database modules Many Python developers are familiar with it Torsten decided to write a model for that, too Accessing a database with a SQL table model

Scalable Vector Graphics (SVG) SVG support can be accessed in two ways: You can use QSvgWidget to load and display pictures in a widget You can use QSvgRenderer to load and render pictures to any paint device SVGs can also contain animations # Showing an SVG drawing: widget = QSvgWidget(parent) widget.load(filename) widget.show() # Rendering a drawing on an image: pixmap = QPixmap(200, 200) renderer = QSvgRenderer() painter = QPainter() painter.begin(pixmap) renderer.render(painter) painter.end()

OpenGL Integration Qt provides a QGLWidget (a QWidget subclass) to display OpenGL content: The OpenGL context is handled automatically Convenience functions to handle textures and colors class GLWidget(QGLWidget): def init (self, parent): QGLWidget. init (self, parent) def initializegl(self): # Set up display lists, OpenGL options. def paintgl(self): # Clear buffers, apply transformations, # paint. def resizegl(self): # Resize viewport, recalculate matrices.

Custom widgets Widgets can be combined to make composite widgets by subclassing an existing widget class. class AddressWidget(QWidget): def init (self, parent = None): QWidget. init (self, parent) namelabel = QLabel(self.tr("Name:")) nameedit = QLineEdit() addresslabel = QLabel(self.tr("Address:")) addressedit = QTextEdit() In the init method: Call the base class's init method Create child widgets Use layouts to make the contents resize nicely. layout = QGridLayout() layout.addwidget(namelabel, 0, 0) layout.addwidget(nameedit, 0, 1) layout.addwidget(addresslabel, 1, 0, Qt.AlignTop) layout.addwidget(addressedit, 1, 1) self.setlayout(layout) Lay out the child widgets

Custom widgets Custom widgets can also be built from scratch. To make new controls For decorative purposes class CustomWidget(QWidget): def init (self, parent = None): QWidget. init (self, parent) # Initialize the widget. def paintevent(self, event): painter = QPainter() painter.begin(self) # Do some painting. painter.end() def sizehint(self): return QSize(200, 200) In the init () method: Subclass an existing widget class Call the base class's init () method In the paintevent() method: Widgets are paint devices Just use a painter to draw on them In the sizehint() method: Return a preferred size to help the layout engine

Custom widgets They can also provide their own signals and slots... There are lots of examples of custom widgets:

OpenGL Integration Revisited You can also use QPainter to paint onto a QGLWidget: Painting operations are translated to OpenGL calls The result is accelerated 2D rendering Relies on extensions for anti-aliasing, so you may be trading looks for speed

Qt Designer Qt Designer is Trolltech's design tool for creating user interfaces. Forms created with Qt Designer are stored in XML (.ui) files These can be compiled to C++ with uic You can also use pyuic4 to convert them to Python Or you can use the Python uic module to generate the GUI at run-time This presentation was created with Qt Designer. The GUI is shown using PyQt4.

Thanks Trolltech for Qt Phil Thompson for SIP and PyQt4 Jim Bublitz for PyKDE Paul Boddie for suggestions The PyQt community (especially those on the mailing list) Links Trolltech: http://www.trolltech.com Riverbank Computing: http://www.riverbankcomputing.com/ PyQt Wiki at http://www.diotavelli.net/pyqtwiki