Overview

The dtlibs.qt module contains the following extensions to the PyQt4 framework.

  • All objects in the QtCore and QtGui modules can be imported from the dtlibs.qt, e.g.

    >>> from dtlibs.qt import Qt, QRect, QWidget
    
    note:Because of the size of PyQt4, it is recommended that from dtlibs.qt import * is not used.
  • The tango icon set is included as a compiled recource. It is also separately available and as a qrc file for use in Designer.

  • The Qt namespace is extended with dtlibs.qt.Qx.

  • New frameworks

  • New classes and functions

New frameworks

Decorator Actions

The normal method of creating and working with QAction is by creating a new QAction instance with appropriate text, icon, tooltip, etc., and connecting its triggered signal to a function. It is then added to the GUI through a toolbar or menu. QXActions provides an alternative, more concise method.

Defining Actions

QXActions is used by first defining the target function of actions using dtlibs.core.info to assign action properties. The following information may be set:

text:
Sets the action’s text property. This is the only argument which is required.
icon:
Sets the action’s icon. This may be either a QIcon instance or the name of a tango icon (which is then found using tangoIcon.
tooltip:
The tooltip text. If this is not specified, then the function’s docstring is used, or, if there is no docstring, the text property.
shortcut:
Set the shortcut key (or keys) to be used. This can be any value accepted as a constructor argument by QKeySequence. It can also be a list of such values, in which case multiple shortcut keys are assigned.
context:
Set the shortcut context. This is only used if shortcut is also specified, and is Qt.QWidgetWithChildrenShortcut by default.

Creating Actions

A new dtlibs.qt.QXActions instance is created with a list of objects. The first object in the argument list is the widget to use as the parent of the generated QAction instances. All the remaining arguments are scanned for available actions by searching for methods with with __info__['text'].

Creating menus and toolbars

The next step is to group the actions into menu and toolbar groups using the group method. The menubar and toolbars methods create and return a QMenuBar and list of QToolBars respectively. By default, all actions are disabled, and are only enabled when they are added to a menu or toolbar, or explicitly enabled.

Example

>>> from dtlibs.core import info
>>> class MyClass:
...
...     @info(text='Add', shortcut='Ctrl+A')
...     def add_items(self):
...         self.append_new_data()
...
...     @info(text='Delete')
...     def delete_items(self):
...         self.delete_all_items()

Two actions (“Add” and “Delete”) can be created from this:

>>> from dtlibs import qt
>>> parent = qt.QMainWindow()
>>> myclass = MyClass()
>>> actions = qt.QXActions(parent, myclass)
>>> actions.group('Data', ['Add', 'Delete'])
>>> isinstance(actions.menubar(), qt.QMenuBar)
True
>>> for toolbar in actions.toolbars():
...     print(toolbar.objectName())
Data

Typical usage is in a QMainWindow which defines its own actions:

def __init__(self, parent):
    super().__init__(self, parent)
    actions = qt.QXActions(self)
    actions.group('File', ['New', 'Open'])
    self.setMenubar(actions.menuBar)
    [self.addToolbar(t) for t in actions.toolbars]

Document Management Framework

The document management framework consists of two classes which interact closely: QXDocument and QXDocumentWindow. Most of a progam’s functionality lies in the QXDocument class, and QXDocumentWindow provides the GUI, similar to the model/view pattern.

Standard Widgets

PyQt4 has many widgets which behave in a very similar manner, but have slightly different APIs. An example is QLineEdit and QCheckBox. Both of these represent a single piece of data and support read and write operators. However, QLineEdit does this through text() and setText() methods while QCheckBox uses checkState() and setCheckState(). These method names are very descriptive, but make generic programming for unknown widgets quite complicated. dtlibs.qt solves this by providing a new set of widgets which are subclasses of the regular PyQt4 widgets but which conform to the dtlibs.qt.QXStdWidgetABC API.