madgui.util.qt module

Miscellaneous utilities for programming with the Qt framework.

Functions

notifyEvent(widget, name, handler)

Connect the handler function to be called when the widget receives an event specified by the given name.

eventFilter(object, events[, parent])

Subscribe to events from object and dispatch via events lookup table.

present(window[, raise_])

Activate window and bring to front.

monospace()

Return a fixed-space QFont.

bold()

Return a bold QFont.

load_ui(widget, package, filename)

Initialize widget from .uic file loaded from the given package.

load_icon_resource(module, name[, format])

Load an icon distributed with the given python package.

Classes

EventFilter(events[, parent])

Implements an event filter from a lookup table.

SingleWindow(construct)

Decorator for widget constructor methods.

Queued(func)

A queued trigger.

class madgui.util.qt.EventFilter(events, parent=None)[source]

Bases: PyQt5.QtCore.QObject

Implements an event filter from a lookup table. It is preferred to use the eventFilter() function rather than instanciating this class directly.

Methods

eventFilter(self, QObject, QEvent)

uninstall()

eventFilter(self, QObject, QEvent)bool[source]
uninstall()[source]
class madgui.util.qt.Queued(func)[source]

Bases: object

A queued trigger. Calling the trigger will invoke the handler function in another mainloop iteration.

Calling the trigger multiple times before the handler was invoked (e.g. within the same mainloop iteration) will result in only a single handler invocation!

This can only be used with at least a QCoreApplication instanciated.

Methods

method(func)

Decorator for a queued method, i.e.

classmethod method(func)[source]

Decorator for a queued method, i.e. a method that when called, actually runs at a later time.

class madgui.util.qt.SingleWindow(construct)[source]

Bases: madgui.util.qt.Property

Decorator for widget constructor methods. It manages the lifetime of the widget to ensure that only one is active at the same time.

madgui.util.qt.bold()[source]

Return a bold QFont.

madgui.util.qt.eventFilter(object, events, parent=None)[source]

Subscribe to events from object and dispatch via events lookup table. Callbacks are invoked with two parameters (object, event) and should return a false-ish value. A true-ish value will stop the event from being processed further.

Example usage:

>>> self.event_filter = eventFilter(window, {
...     'WindowActivate': self._on_window_activate,
...     'Close': self._on_window_close))
... })

(Note that it is important to store the reference to the event filter somewhere - otherwise it may be garbage collected.)

See QEvent for possible events.

madgui.util.qt.load_icon_resource(module, name, format='XPM')[source]

Load an icon distributed with the given python package. Returns a QPixmap.

madgui.util.qt.load_ui(widget, package, filename)[source]

Initialize widget from .uic file loaded from the given package.

This function is for loading GUIs that were developed using the qt-designer rapid development tool which creates .uic description files. These can be saved in the same package alongside the corresponding python code. Now, in the class that implements the widget, use this function as follows:

class MyWidget(QWidget):

    def __init__(self):
        super().__init__()
        load_ui(self, __package__, 'mywidget.uic')
madgui.util.qt.monospace()[source]

Return a fixed-space QFont.

madgui.util.qt.notifyEvent(widget, name, handler)[source]

Connect the handler function to be called when the widget receives an event specified by the given name.

madgui.util.qt.present(window, raise_=False)[source]

Activate window and bring to front.