Connect PyQt Button to HAL at runtime
- JT
- Topic Author
- Away
- Administrator
- Posts: 886
- Thank you received: 457
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.
- JT
- Topic Author
- Away
- Administrator
- Posts: 886
- Thank you received: 457
<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.
- MennilTossFlykune
- Offline
- Junior Member
- Posts: 35
- Thank you received: 25
Please Log in or Create an account to join the conversation.
- cmorley
- Offline
- Moderator
- Posts: 7796
- Thank you received: 2080
Please Log in or Create an account to join the conversation.
- JT
- Topic Author
- Away
- Administrator
- Posts: 886
- Thank you received: 457
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
Please Log in or Create an account to join the conversation.
- JT
- Topic Author
- Away
- Administrator
- Posts: 886
- Thank you received: 457
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 or Create an account to join the conversation.
- cmorley
- Offline
- Moderator
- Posts: 7796
- Thank you received: 2080
I think this strange looking code should work too.
Please Log in or Create an account to join the conversation.
- MennilTossFlykune
- Offline
- Junior Member
- Posts: 35
- Thank you received: 25
Please Log in or Create an account to join the conversation.
- cmorley
- Offline
- Moderator
- Posts: 7796
- Thank you received: 2080
Please Log in or Create an account to join the conversation.
- JT
- Topic Author
- Away
- Administrator
- Posts: 886
- Thank you received: 457
JT
Please Log in or Create an account to join the conversation.