Connect PyQt Button to HAL at runtime

More
01 May 2024 13:05 #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 or Create an account to join the conversation.

More
01 May 2024 14:23 #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()


 

JT
Attachments:

Please Log in or Create an account to join the conversation.

More
01 May 2024 22:36 #299512 by MennilTossFlykune
The following user(s) said Thank You: JT

Please Log in or Create an account to join the conversation.

More
02 May 2024 08:23 #299532 by cmorley
Yes I'm sure it's the lamba statement.
The following user(s) said Thank You: JT

Please Log in or Create an account to join the conversation.

More
02 May 2024 12:56 - 02 May 2024 12:57 #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).

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
Last edit: 02 May 2024 12:57 by JT.

Please Log in or Create an account to join the conversation.

More
02 May 2024 13:38 - 02 May 2024 13:43 #299550 by JT
Well lambda be damnda I think this will work.
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
Last edit: 02 May 2024 13:43 by JT.
The following user(s) said Thank You: tommylight

Please Log in or Create an account to join the conversation.

More
02 May 2024 14:25 #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.
The following user(s) said Thank You: JT

Please Log in or Create an account to join the conversation.

More
02 May 2024 14:43 #299558 by MennilTossFlykune

Please Log in or Create an account to join the conversation.

More
02 May 2024 15:11 #299563 by cmorley
Ahh - Much cleaner code...nice!

Please Log in or Create an account to join the conversation.

More
02 May 2024 15:14 #299564 by JT
Very nice and better than my work around.

JT

Please Log in or Create an account to join the conversation.

Time to create page: 0.113 seconds
Powered by Kunena Forum