QInstrumentTree#
- class QInstrument.lib.QInstrumentTree.QInstrumentTree(*args, device=None, fields=None, **kwargs)[source]#
Bases:
ParameterTreeParameterTree that auto-builds from a QAbstractInstrument.
An alternative to
QInstrumentWidgetthat uses a pyqtgraphParameterTreeinstead of a Qt Designer.uifile. 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 viaregisterProperty()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
FIELDSon the subclass or passfieldsto__init__:class QDS345Tree(QInstrumentTree): INSTRUMENT = QDS345 FIELDS = ['frequency', 'amplitude', 'function'] # or at instantiation time: tree = QDS345Tree(fields=['frequency', 'amplitude'])
The order of names in
FIELDScontrols 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
deviceis not supplied to__init__, the base class callsINSTRUMENT().find()automatically. Pass an explicitdeviceto override (e.g. to inject a fake for testing).Class Attributes#
- INSTRUMENTtype | None
The concrete instrument class to instantiate and search for when no
deviceis supplied. Must be aQSerialInstrumentsubclass (or any class whose no-arg constructor returns an object with afind()method).Nonemeans 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
INSTRUMENTis set, the instrument is located viaINSTRUMENT().find().fields (list[str] | None, optional) – Overrides
FIELDSfor this instance.Nonedefers to the class attribute.
- 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
Noneis a no-op. The tree is disabled if the device is not open.- Type:
- 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:
- 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:
- 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 siblingfakemodule if no instrument is connected.Intended to be called from
__main__in each tree module:if __name__ == '__main__': QMyTree.example()
- Return type: