I'm trying to find a way to update labels which are within dynamically created widgets (that can be deleted), according to properties dynamically set in the preceding widgets.
Is there a way to automatically and dynamically link a pyqt object to a property of other widgets, so that when I change a value anywhere, that object updates itself?
Example: object 'a' has property start, bars and end; bars is given, start is taken by the previous object (or 1 if None), end is calculated. object 'b' takes its start from a.end and so on.
class myclass(object):
def __init__(self, referrer=None, value=1):
self.referrer=referrer
self.value=value
def _get_start(self):
if not self.referrer:
return 1
else:
return self.referrer.end+1
def _get_end(self):
return self.start+self.value-1
start=property(_get_start)
end=property(_get_end)
def create(value=1):
if not vList:
ref=None
else:
ref=vList[-1]
val=myclass(ref, value)
vList.append(val)
def showList():
for i in vList:
print 'item: %d\tstart: %d\tend: %d' % (vList.index(i),i.start, i.end)
vList=[]
If I call create() 3 times, showList() will show:
item: 0 start: 1 end: 1
item: 1 start: 2 end: 2
item: 2 start: 3 end: 3
if I change vList[0].value to 3:
item: 0 start: 1 end: 3
item: 1 start: 4 end: 4
item: 2 start: 5 end: 5
The problem raises when I need to keep those values updated in the gui (think to it as an interface like this): every horizontal widget has a label showing the property of start, a spinbox for bars, and a label for end, and as soon as any spinbox value changes, every subsequent widget should update its start and end properties according to its previous widget and show them in the relative labels.
Moreover, when any widget is deleted, all the subsequent widget should recompute every property.
Using getter/setter to set the values in the labels of the widgets' next instancies doesn't obviously work as I need, because when I change any x.value, the following instancies' start and end will be actually updated only when recalled, AFAIU.
I could connect every new widget to its previous (with valueChanged()) or create a function which finds the subsequent widget and update their properties, but that's not a good solution.
Since I am almost new to python (most important: I'm not a programmer) I think that I am ignoring something about "connecting" variables in a better and cleanest way (maybe related to signals or threading?).
Consider that those widgets will actually be children widgets of another "main" widget, which will have similar properties: start taken from its previous main widget (if any), bars which is the sum of all bars in every children widget, end which will be again start+bars-a.
Thanks!
(I hope you will understand what I meant, my english is not perfect and, since I'm not a programmer, my terminology is not always correct)