Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add table-cell change callbacks #205

Closed
tlambert03 opened this issue Apr 2, 2021 · 7 comments · Fixed by #209
Closed

Add table-cell change callbacks #205

tlambert03 opened this issue Apr 2, 2021 · 7 comments · Fixed by #209

Comments

@tlambert03
Copy link
Member

As pointed out by @pranathivemuri in https://github.com/napari/magicgui/pull/182#issuecomment-812670169 ... magicgui Table doesn't yet have a good way to bind a callback to a change event on a cell or a group of cells. (It was left for a later PR, and that time has come! 😄 )

https://github.com/napari/magicgui/blob/a07b6d3885fb63a6d7947fbb03fc90a971759006/magicgui/backends/_qtpy/widgets.py#L828-L832

@pranathivemuri
Copy link

Thanks so much for working on this! - Looking forward to this change.

@tlambert03
Copy link
Member Author

tlambert03 commented Apr 2, 2021

@pranathivemuri, there is a workaround you can use in the meantime, by directly using the Qt api. Let's take the example at https://github.com/napari/magicgui/blob/master/examples/table.py ...

If, before running table.show(run=True) you added something like this, you can get a handle the cell change events:

def on_item_changed(item):
    # item will be an instance of `QTableWidgetItem`
    # https://doc.qt.io/Qt-5/qtablewidgetitem.html
    print('row:', item.row())
    print('col:', item.column())
    print('new data:', item.data(table._widget._DATA_ROLE))  # _DATA_ROLE will likely just be the number 255
    # whatever you want to do with the new value

table.native.itemChanged.connect(on_item_changed)

that might let you get started on some stuff until there is a "magicgui" way of doing it

@pranathivemuri
Copy link

awesome, I was just researching for this! in my case, it doesn't necessarily have to be a table it is just one text input from user that needs to be passed so there is probably multiple ways to work around it

@tlambert03
Copy link
Member Author

oh! If it doesn't have to be a table... like if you just want to know if a user has changed a text input, then it's already supported (again, tables are a specific, special case)

see some docs here, but the basic idea is this:

# if using the decorator:

@magicgui
def func(your_name='Bob'):
    return f'hello {your_name}!'

# to connect to just the specific parameter changing
def _on_your_name_changed(event):
    print(f'your_name was changed to {event.value}')

func.your_name.changed.connect(_on_your_name_changed)


# to connect to the whole function being called
def _on_func_called(event):
    print(f"func called: {event.value}")

func.called.connect(_on_your_name_changed)

@pranathivemuri
Copy link

nice, thanks! I will just have to figure out passing this to current_properties in napari shapes layer to change the label for that bounding box. So far so good, thanks for all the example code! :)

@tlambert03
Copy link
Member Author

feel free to point me to code if you need help

@pranathivemuri
Copy link

Thanks, it worked out. I will open-source the code in a few days, but it is working as I wanted. Thanks to the workaround you pointed out with QT table! I appreciate your quick replies today!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants