Tab Widgets and Stacked Widgets

Some dialogs require so many widgets to present all the options that they make available that they become difficult for the user to understand. The most obvious way to deal with this is to create two or more dialogs and to divide the options between them. This is a good approach when it is possible since it minimizes the demands made on the user, and may also be easier from a maintenance point of view than a single complex dialog. But often we need to use a single dialog because the options we...

Model View Programming

Model view programming is a technique that involves separating data from its visual representation. It was first popularized as the MVC model view controller paradigm used in the Smalltalk programming language. A model is a class that provides a uniform interface through which data items can be accessed. A view is a class that can present the data items from a model to the user on-screen. A controller is a class that mediates between the user interface e.g., mouse events and key presses to...

Multiple Document Interface MDI

Qworkspace Widget

MDI offers many benefits compared with SDI or with running multiple application instances. MDI applications are less resource-hungry, and they make it much easier to offer the user the ability to lay out their document windows in relation to each other. One drawback, however, is that you cannot switch between MDI windows using Alt Tab Command Tab on Mac OS X , although this is rarely a problem in practice since for MDI applications, programmers invariably implement a Window menu for navigating...

The Event Handling Mechanism

PyQt provides two mechanisms for dealing with events the high-level signals and slots mechanism, and low-level event handlers. The signals and slots approach is ideal when we are concerned with what actions the user wants to perform, without getting bogged down in the details of how specifically they asked. Signals and slots can also be used to customize some aspects of how widgets behave. But when we need to go deeper, particularly when creating custom widgets, we need to use low-level event...

Creating Composite Widgets

A composite widget is a widget that is composed of two or more other widgets. We are already experienced composite widget creators For example, every dialog we have created is a composite widget. We are dedicating some space to a topic we have already covered because unlike the dialogs we created earlier which were QDialog subclasses , we want to create composite widgets that are not dialogs, and that instead can be used inside dialogs or as a main window's central widget . The kind of...

Creating Custom Models

In this section, we will create a custom model to hold the ship data, and display the same model in two different table views. An application that makes use of the model is shown in Figure 14.3. The user can scroll the tables independently, and can edit the data in either of them, safe in the knowledge that any changes will be automatically reflected in both views. We will begin by showing extracts from the application's main form. This will show us some of the model view API in use. Then we...

Custom and Interactive Graphics Items

Pyside Move Qgraphicsitem

The predefined graphics items can be made movable, selectable, and focusable by calling setFlags on them with suitable constants. Users can drag movable items with the mouse, and they can select selectable items by clicking them, and by using Ctrl Click to select multiple items. Focusable items will receive key events, but will ignore them unless we create an item subclass with a key event handler. Similarly, we can make items responsive to mouse events by subclassing and implementing...

Using QSyntax Highlighter

Text Editor Pyqt

If we want a Python-savvy text editor with syntax highlighting, we need not create one ourselves. The Tkinter-based IDLE application provides both a sandbox in which we can experiment with Python code, and a perfectly good Python code editor. And for more power, we can use Eric4, itself written in PyQt and using QScintilla for its text editing. However, no off-the-shelf editor will necessarily work in just the way we want, and since creating our own is instructive and revealing of what is...

Executing SQL Queries

Now that we have a connection, we can execute some SQL statements. query QSqlQuery query.exec_ CREATE TABLE outcomes id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL, name VARCHAR 40 NOT NULL query.execj CREATE TABLE calls id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL, FOREIGN KEY outcomeid REFERENCES outcomes We have not specified a particular database connection to use, so PyQt will use the default unnamed connection that we established earlier. The tables created by the SQL calls...