Functions

qte.compactSeparators(toolbar)

Remove adjacent separators in a QTooBar instance.

qte.error(prompt)

Use a QMessageBox to display an error message.

qte.fadeBrush(colour, percentage=0.0)

Return a brush with colour faded by fraction.

fraction should be between 0 and 1, where 0 has no effect on the colour and 1 results in white.

qte.float2(s, default=0.0)

Convert s to a float, returning default if it cannot be converted.

>>> float2('33.4', 42.5)
33.4
>>> float2('cannot convert this', 42.5)
42.5
>>> float2(None, 0)
0
>>> print(float2('default does not have to be a float', None))
None
qte.getOpenFileName(filters, title='Open', path=None)

Display an “Open File” dialog.

Parameters:
  • filters – A list of file filters to apply.
  • title – The dialog title. The default is 'Open'.
  • path – The path at which to open the dialog.
Returns:

The name of the file, or None if the dialog is canceled.

qte.getOption(prompt, options, current=0)

Display a dropdown list of options.

Parameters:
  • prompt – Prompt text to display.
  • options – A list of strings.
  • current – The initially selected index.
Returns:

The newly selected index

qte.getSaveFileName(filters, title='Save', path=None)

Display a “Save File” dialog.

Parameters:
  • filters – A list of file filters to apply.
  • title – The dialog title. The default is 'Save'.
  • path – The path at which to open the dialog.
Returns:

The name of the file, or None if the dialog is canceled.

qte.getText(prompt, default='', validate=None, handle_invalid='confirm')

Requests a text string through a gui prompt.

Parameters:
  • prompt – Prompt text to display.
  • default – Default value in the entry widget.
  • validate – A function which accepts a single argument and returns True or False.
  • handle_invalid – Control how an invalid entry is handled.
Returns:

The value entered, or None if the request was cancelled.

handle_invalid can be any one of the following strings

Value Description
‘confirm’ A confirmation dialog is displayed, asking whether to accept.
‘warn_accept’ A warning dialog is displayed and the value is accepted.
‘warn_deny’ A warning dialog is displayed and the value can be re-entered.
‘cancel’ Nothing is displayed or accepted.
qte.guessWidget(value)

Attempt to guess which widget to provide based on a value.

The return value is a class which implements EditWidgetABC. value is the value the widget should work with. If no suitable widget is found, TextEdit is returned.

qte.int2(s, default=0)

Convert s to an int, returning default if it cannot be converted.

>>> int2('33', 42)
33
>>> int2('cannot convert this', 42)
42
>>> print(int2('default does not have to be an int', None))
None
qte.loadResource(qrc, register=True, split=False)

Compile the resource file qrc.

Parameters:
  • qrc – The name of a qrc file.
  • register – If True (default), register the resource data with Qt’s resource system.
  • split – If True, return tuple of (tree, names, data)
Returns:

A bytes object containing the raw, uncompressed resource data.

The raw resource data is returned, and can be written to a file to fully mimic Qt’s rcc tool. However, in most cases this is not required and can be ignored.

If split is True, a tuple of (tree, names, data) is returned. Each of theses is a bytes object and is the same as the data contained in qt_resource_struct, qt_resource_name and qt_resource_data respectively, in a file created with pyside-rcc. This is mainly useful for debugging.

qte.loadUi(ui, parent=None, widgets=None)

Create a new instance of the widget described in ui.

Parameters:
  • ui – The name of a ui file.
  • parent – The parent widget, or None.
  • widgets – A list of custom widgets which are reference in ui
Returns:

A widget instance.

This uses QtUiTools.QUiLoader to create the widget, but first registers all custom widgets in qte and those explicitly set in widgets, so these widgets can be used in Qt Designer by promoting them from their base classes. Note that the header file field in Qt Designer can be set to anything.

loadUi also supports the direct use of qrc files, so they do not need to be compiled in advance.

qte.menuToToolBar(menu, toolbar=None)

Add all actions in a QMenu to a QTooBar.

If toolbar is missing a new QToolBar is created, otherwise toolbar is updated. In either case, the toolbar is returned.

Actions are added in the same order as they were to the QMenu instance. Separators are added to correspond to those in the menu, and submenus are also added to one level, surrounded by separators. Further submenus are added as dropdown menus.

qte.message(prompt)

Use a QMessageBox to display an information message.

qte.question(prompt)

Display a question dialog with Yes and No buttons.

The return value is True if Yes was selected or False if No was selected.

qte.registerWidget(name, widget=None)

Register a widget referenced in a ui file.

If widget is omitted, then a class decorator is returned, allowing it to be used like this:

@registerWidget('MyWidget')
class MyWidget(QWidget):
    ...
qte.runApp(mainWindow, name)

Sets the main window and name of the the application and runs it.

This also sets the locale to the current system locale.

Example usage is:

from gui import MainWindow

if __name__== '__main':
    runApp(MainWindow, 'My Application')

Once the application has been terminated, runApp calls sys.exit, so this should be the final command to be run.

mainWindow can be either a QWidget instance or subclass. If it is a subclass, then an instance is created.

This is implemented by calling the following:

locale.setLocal(locale.LC_ALL, '')
wiApplicationmainWindow()
Application.set_appName(name)
Application.setMainWindow(win)
win.setWindowTitle(name)
win.show()
sys.exit(QX_app.exec_())
qte.standardBrush(color_role)

Return a brush for a Qt standard color role.

qte.standardIcon(icon)

Return a standard icon in the application style.

icon may either be a QStyle.StandardPixmap flag (e.g. QStyle.SP_DirIcon) or the name of a standard pixmap, omitting the ‘SP’ portion (e.g. 'DirIcon').

qte.textHeight(widget, text=None)

Return the height of text painted by widget.

If text is omitted, then the height of a single character is returned.

qte.textWidth(widget, text=None)

Return the width of text painted by widget.

If text is omitted, the average character width is returned.

qte.uiWrapper(uifile)

Return a class inherited from a widget defined in a ui file.

This is typically used when customising a widget created using Qt Designer:

class MyWidget(uiWrapper('mywidget.ui')):
    def __init__(self, value):
        super().__init__()
        self.value = value

Resource files referenced by the ui file are automatically loaded using loadResource.

There are several opportunities for naming clashes, and understanding these requires a knowledge of how the working globals are used.

  • The globals dict is first populated with all available widgets, i.e those provided by PySide, qte and registered using registerWidget.
  • When loading the ui file, a special class is created named UI_<widget_name>, which will override any similarly named widgets.
  • When the class is first initialised, all widgets added to in in Qt Designer are added to its dictionary.

To avoid the potential pitfalls, ensure that registered widget names do not start with Ui_ and that objects added in Qt Designer do not have the same name as a parent widget property. For example, do not add a child widget named “objectName”.

Project Versions

Previous topic

TypedDelegate

Next topic

UIs and Resources

This Page