0

I have 3 identical physical instruments. I can control each instrument separately by instanciating an InstrumentDriver class.

I have a Ui_MainWindow class, automatically created from Qt Designer (via pyuic5). This class contains "duplicated" controls: I have 3 identical sets of UI controls (buttons, lineEdits, etc.), each one is used to control one instrument.

I have a InstrumentGui class doing the mapping between the UI elements defined in Ui_MainWindow and the actions allowed by InstrumentDriver. InstrumentGui is also instanciated 3 times.

I don't know how to bind a GUI control to a specific instance of InstrumentDriver class:

class InstrumentGui :
    def __init__(self, instrument: InstrumentDriver, ui: Ui_MainWindow, type: str)
        # How do I reach toolButton_startA if type=="A",
        # toolButton_startB if type == "B", etc.?
        ui.toolButton_startA.clicked.connect(instrument.start)

ui = Ui_MainWindow()

instrumentA = InstrumentDriver(address=1)
instrumentGuiA = InstrumentGui(instrumentA, ui, "A")

instrumentB = InstrumentDriver(address=2)
instrumentGuiB = InstrumentGui(instrumentB, ui, "B")

instrumentC = InstrumentDriver(address=3)
instrumentGuiC = InstrumentGui(instrumentC, ui, "C")

My first idea was to use variable variable name, something like setattr(self, "start_button", "toolButton_start"+type), but appart from the fact that I didn't manage to get it work, I read everywhere that it is a bad practice.

What is the smart way of doing that, if possible keeping the Qt Designer way of building GUI layout?

Blacksad
  • 1,230
  • 1
  • 14
  • 23
  • The "smart" way of doing this is not to have the actual tool buttons in the UI already (everything else can be) and then have the `InstrumentGUI` class add an appropriate button to the toolbar (or just have the button in the actual relevant `InstrumentGUI`) – three_pineapples Mar 15 '18 at 10:32
  • I didn't mention that in order to keep the example simple, but I have in fact more than 10 controls per Instrument, and I would like to place then quite precisely on my interface. Qt Designer is great for that. Your solution is good, but does not fit very well to the above-metionned constraints. – Blacksad Mar 15 '18 at 12:26
  • 1
    It sounds like you need to create a [custom designer plugin](http://pyqt.sourceforge.net/Docs/PyQt5/designer.html#writing-qt-designer-plugins) for the instrument, so that the contained widgets have the same names. Alternatively, you could create a separate designer ui file for the instruments, and then use [widget promotion](https://stackoverflow.com/a/42076247/984421) to add them the main ui. – ekhumoro Mar 15 '18 at 18:13

0 Answers0