QInstrumentTree#

class QInstrument.lib.QInstrumentTree.QInstrumentTree(*args, device=None, fields=None, **kwargs)[source]#

Bases: ParameterTree

ParameterTree that auto-builds from a QAbstractInstrument.

An alternative to QInstrumentWidget that uses a pyqtgraph ParameterTree instead of a Qt Designer .ui file. The tree is constructed at runtime from the device’s property registry so no UI file is needed. Property metadata (ptype, minimum, maximum, step) registered via registerProperty() maps directly to pyqtgraph parameter types and constraints. Read-only properties (setter=None) appear as non-editable display items. Registered methods appear as action buttons.

On first show, saved settings are reconciled with the hardware state via QReconcileDialog, and the device is moved to a dedicated worker thread so that serial I/O does not block the GUI. Settings are saved on close.

Subclass this for each instrument and declare INSTRUMENT:

class QDS345Tree(QInstrumentTree):
    INSTRUMENT = QDS345

To restrict the tree to a subset of properties and methods, declare FIELDS on the subclass or pass fields to __init__:

class QDS345Tree(QInstrumentTree):
    INSTRUMENT = QDS345
    FIELDS = ['frequency', 'amplitude', 'function']

# or at instantiation time:
tree = QDS345Tree(fields=['frequency', 'amplitude'])

The order of names in FIELDS controls display order. If any name does not match a registered property or method a warning is issued and all properties and methods are shown instead.

When device is not supplied to __init__, the base class calls INSTRUMENT().find() automatically. Pass an explicit device to override (e.g. to inject a fake for testing).

Class Attributes#

INSTRUMENTtype | None

The concrete instrument class to instantiate and search for when no device is supplied. Must be a QSerialInstrument subclass (or any class whose no-arg constructor returns an object with a find() method). None means no auto-instantiation.

FIELDSlist[str] | None

Names of properties and/or methods to display, in display order. None (the default) shows all registered properties and methods.

Parameters:
  • device (QAbstractInstrument, optional) – Instrument to display. When omitted and INSTRUMENT is set, the instrument is located via INSTRUMENT().find().

  • fields (list[str] | None, optional) – Overrides FIELDS for this instance. None defers to the class attribute.

INSTRUMENT: type | None = None#
FIELDS: list[str] | None = None#
HARDWARE_DOMINANT: bool = False#
property device: QAbstractInstrument | None#

instrument bound to this tree.

Setting this property builds the parameter tree from the device’s registered properties and methods, syncs current values if the device is open, and connects signals. Setting to None is a no-op. The tree is disabled if the device is not open.

Type:

QAbstractInstrument

showEvent(event)[source]#

Reconcile device settings and move to a worker thread on first show.

Schedules _firstShow() via a zero-delay timer so that reconciliation runs after Qt finishes processing the show event, avoiding a nested event loop inside an event handler. Subsequent show events are passed through unchanged.

Return type:

None

closeEvent(event)[source]#

Stop the worker thread and save settings when the tree is closed.

Stops the device worker thread before saving so that no queued slot calls arrive after the tree is gone. If the device has a stopPolling() slot, it is called before the thread is stopped; it only sets a flag, so it is safe from any thread. Only saves if the tree was previously shown, so that test instances closed during teardown do not overwrite saved configuration.

Return type:

None

classmethod example()[source]#

Display the tree.

Creates a QApplication, instantiates the tree, shows it, and runs the event loop. Falls back to the fake device class from the sibling fake module if no instrument is connected.

Intended to be called from __main__ in each tree module:

if __name__ == '__main__':
    QMyTree.example()
Return type:

None