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?
#๐ PyQt two-way binding/auto-updating label?
49 messages ยท Page 1 of 1 (latest)
@kindred beacon
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.
Closes after a period of inactivity, or when you send !close.
you can add a textChanged signal connected to all your input fields
I intend to, but I don't wanna call setText() directly. Right now these fields and labels are close together, but as the app grows I'd like something more elegant
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
looks good
Ok, I think that helps, gonna try that! tyvm!
another example
this tells you what the filename you'll be creating will be named based on the text fields
Haha hey! Thanks!
๐ฎ you nazi ! why don't you let the user pick the name directly ?
And in that case, it's not the input field that triggers the update chain for the label?
because certain characters will need unique animation names and we don't have a source of data yet ๐ฌ
it does. Every time ANY of these fields are updated, it queries the text of all the fields and constructs the new string, then updates the label
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
But there's no way to like, store the full name in an observable value that the label can connect to?
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
I could update an attribute or something with that name if I wanted to
but there's really no reason to. I query the value as needed
there's not really a benefit to storing it elsewhere
I understand that's simpler in your use case, but it doesn't solve my issue
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
As my app grows, I expect inputs and labels all over the place, all interconnected.
I wanna keep my data centralized
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
Specifying all labels that need an update post calculation is what I'm trying to avoid
are you worried about speed?
Not really, just wanna keep direct UI calls out of my logic
signals is really the only way to keep information "live"
that's really what they're there for
So there's no observables?
I don't believe so
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.