#๐Ÿ”’ PyQt two-way binding/auto-updating label?

49 messages ยท Page 1 of 1 (latest)

kindred beacon
#

Hey all, new to Qt. I have a few input fields, a calculations based on those, and a label displaying the output. I'm currently manually calling label.setText() whenever the input changes, but as my application grows I foresee this becoming unmanageable. is there a way to have the label (or a disabled input, idc) automatically listen to changes on an internal value?

prime nexusBOT
#

@kindred beacon

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

shell cargo
kindred beacon
shell cargo
#

this is a pretty common implementation

#

self.some_input_field.textChanged.connect(self.on_field_updated)

#

and then they can all connect to this same self.on_field_updated method

#

and from there, you handle what happens when text changes

#

as a similar example, this is a tool I wrote for exporting animations from animation software

#

the fields are all connected to the same method

#

the text of the field changes to orange if the filename exists (just as a warning that you will be overwriting)

#

it uses textEdited to achieve this

kindred beacon
#

Ok, I think that helps, gonna try that! tyvm!

shell cargo
#

another example

#

this tells you what the filename you'll be creating will be named based on the text fields

shell cargo
frail kraken
# shell cargo

๐Ÿ˜ฎ you nazi ! why don't you let the user pick the name directly ?

kindred beacon
# shell cargo

And in that case, it's not the input field that triggers the update chain for the label?

shell cargo
shell cargo
#
self.asset_name_line.textChanged.connect(self.update_filepath_label)
self.scene_name_line.textChanged.connect(self.update_filepath_label)
self.version_line.valueChanged.connect(self.update_filepath_label)
#

here's my connections

kindred beacon
#

But there's no way to like, store the full name in an observable value that the label can connect to?

shell cargo
#
def update_filepath_label(self):
    asset = self.asset_name_line.text().strip('_')
    scene = self.scene_name_line.text().strip('_').title()
    num = '{:03d}'.format(self.version_line.value())
    filename = '{}_{}_{}.ma'.format(asset, scene, num)
    self.filepath_label.setText(filename)
#

and my method

shell cargo
#

but there's really no reason to. I query the value as needed

#

there's not really a benefit to storing it elsewhere

kindred beacon
#

I understand that's simpler in your use case, but it doesn't solve my issue

shell cargo
#

Perhaps you can elaborate then

#

in my use case, I just query the label text to get the filename I'm meant to create

#

I don't need a separate variable to get that filename

kindred beacon
#

As my app grows, I expect inputs and labels all over the place, all interconnected.
I wanna keep my data centralized

shell cargo
#

but if any single one of them changing needs to update a label somewhere, then that's your option

#

when a field changes, figure out what to display from a method

kindred beacon
#

Specifying all labels that need an update post calculation is what I'm trying to avoid

shell cargo
#

are you worried about speed?

kindred beacon
#

Not really, just wanna keep direct UI calls out of my logic

shell cargo
#

signals is really the only way to keep information "live"

#

that's really what they're there for

kindred beacon
#

So there's no observables?

shell cargo
#

I don't believe so

kindred beacon
#

sigh the hard way it is then. ty

#

!close

prime nexusBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.