Connect PyQt Button to HAL at runtime

More
2 years 5 months ago #299459 by JT
So I'm struggling with connecting a QPushButton to HAL using Dynamic Properties to define the HAL pin type.

This is my current code that creates the HAL pins and they show up in halshow but I'm stumped as how to make the button clicked (or other signals) to change the HAL value.

def setup_hal_buttons(parent):
    for button in parent.findChildren(QPushButton):
        if button.property('function') == 'hal_pin':
            props = button.dynamicPropertyNames()
            for prop in props:
                prop = str(prop, 'utf-8')
                if prop.startswith('pin_'):
                    #print(prop)
                    pin_settings = button.property(prop).split(',')
                    name = button.objectName()
                    #print(name)
                    pin_name = pin_settings[0]
                    pin_type = getattr(hal, f'{pin_settings[1].upper().strip()}')
                    pin_dir = getattr(hal, f'{pin_settings[2].upper().strip()}')
                    setattr(parent, f'{prop}', parent.halcomp.newpin(pin_name, pin_type, pin_dir))

                    getattr(parent, f'{name}').toggled.connect(lambda: getattr(parent, f'{prop}').set(getattr(parent, f'{name}').isChecked()))
    parent.halcomp.ready()


For some reason only the last button found with a Dynamic Property of function is toggling the pin in halshow. I gotta be missing something simple...

JT

 

Please Log in to join the conversation.

More
2 years 5 months ago #299467 by JT
Adding a couple of debug prints I can see that both buttons and pins are unique.

<PyQt6.QtWidgets.QPushButton object at 0x7ff158ceea70>
<hal.Pin object at 0x7ff15c8d95d0>
<PyQt6.QtWidgets.QPushButton object at 0x7ff158ceeb00>
<hal.Pin object at 0x7ff15c8d9210>

                if prop.startswith('pin_'):
                    #print(prop)
                    pin_settings = button.property(prop).split(',')
                    name = button.objectName()
                    #print(name)
                    pin_name = pin_settings[0]
                    pin_type = getattr(hal, f'{pin_settings[1].upper().strip()}')
                    pin_dir = getattr(hal, f'{pin_settings[2].upper().strip()}')
                    setattr(parent, f'{prop}', parent.halcomp.newpin(pin_name, pin_type, pin_dir))
                    print(getattr(parent, f'{name}'))
                    print(getattr(parent, f'{prop}'))
                    getattr(parent, f'{name}').toggled.connect(lambda: getattr(parent, f'{prop}').set(getattr(parent, f'{name}').isChecked()))
    parent.halcomp.ready()


 

Attachment not found



JT

Please Log in to join the conversation.

More
More
2 years 5 months ago #299532 by cmorley
Yes I'm sure it's the lamba statement.

Please Log in to join the conversation.

More
2 years 5 months ago #299548 by JT
Should I make the connection between the button and the HAL pin in a different manner?

I've tried a few things from the link above and while this works on the last button it still does not work on the first button found (only 2 buttons).

Code:
getattr(parent, f'{name}').pressed.connect(lambda num=n: getattr(parent, f'{prop}').set(getattr(parent, f'{name}').isDown())) getattr(parent, f'{name}').released.connect(lambda num=n: getattr(parent, f'{prop}').set(getattr(parent, f'{name}').isDown()))

JT

Please Log in to join the conversation.

More
2 years 5 months ago #299550 by JT
Well lambda be damnda I think this will work.
Code:
getattr(parent, f'{name}').pressed.connect(partial(utilities.hal_pins, parent, getattr(parent, f'{prop}'))) getattr(parent, f'{name}').released.connect(partial(utilities.hal_pins, parent, getattr(parent, f'{prop}'))) # the function def hal_pins(parent, hal_pin): hal_pin.set(parent.sender().isDown())

JT

Please Log in to join the conversation.

More
2 years 5 months ago #299555 by cmorley
getattr(parent, f'{name}').pressed.connect(lambda parent=parent,prop=prop,name=name: getattr(parent, f'{prop}').set(getattr(parent, f'{name}').isDown()))

I think this strange looking code should work too.

Please Log in to join the conversation.

More
2 years 5 months ago #299558 by MennilTossFlykune

Please Log in to join the conversation.

More
2 years 5 months ago #299563 by cmorley
Ahh - Much cleaner code...nice!

Please Log in to join the conversation.

More
2 years 5 months ago #299564 by JT
Very nice and better than my work around.

JT

Please Log in to join the conversation.

Time to create page: 0.061 seconds
Powered by Kunena Forum