python - PySide One slot to multiple widgets -
i creating small app in python using pyside. read lines text file , display each line in separate qlineedit widget. each "entry" has 2 line edits , 2 qpushbuttons. every line add widgets. problem set signal-slot qpushbuttons, when "entries" generated, last entries qpushbuttons connects slot. may please me.
here code
class itemlogger(qtgui.qmainwindow, ui.ui_mainwindow): def __init__(self, parent = none): super(itemlogger, self).__init__(parent) self.setupui(self) self.parsebossitem() self.combobox.currentindexchanged.connect(self.parsebossitem) self.increase.clicked.connect(self.add_subtract) def add_subtract(self): initial = 1 print "kajskasdjflsdkjflk" def addrow(self, item): self.frame = qtgui.qframe() self.layout = qtgui.qhboxlayout() self.itemname = qtgui.qlineedit(item) self.itemname.setreadonly(true) self.itemcount = qtgui.qlineedit() self.itemcount.settext("0") self.itemcount.setmaximumwidth(40) self.decrease = qtgui.qpushbutton("-") self.increase = qtgui.qpushbutton("+") self.layout.addwidget(self.itemname) self.layout.addwidget(self.itemcount) self.layout.addwidget(self.decrease) self.layout.addwidget(self.increase) self.frame.setlayout(self.layout) self.verticallayout_3.addwidget(self.frame) def parsebossitem(self): if self.combobox.currenttext() == "item_1": item_list = open("bossitems/random_item") line in item_list.readlines(): self.addrow(line) if self.combobox.currenttext() == "item_2": item_list = open("bossitems/random_item_2") line in item_list.readlines(): self.addrow(line)
this because connected last entry.
here doing:
- you add row item 1, , assign button widgets
self.decrease
,self.increase
. - you add row item 2, replacing values of
self.decrease
,self.increase
newly created widgets. - you connect
self.increase
, last added widget.
if don't need access widgets after creation, should consider using local variables (e.g. without self
) , connecting signal inside addrow
function.
if need keep track of widget references, add them array:
# somewhere in __init__ or in parsebossitem self.increase = [] # in addrow self.increase.append(qtgui.qpushbutton("+")) self.layout.addwidget(self.increase[-1]) self.increase[-1].clicked.connect(self.add_subtract) # , on...
to use same slot form different senders, need identify sent signal. this:
def onincrease(self): button = self.sender() if isinstance(button, qtgui.qpushbutton): buttonname = button.text() if buttonname == 'name of button 1': self.itemcount[0].settext(str(int(self.itemcount[0])+1)) elif buttonname == 'name of button 2': ...
off course, assuming put each qlineedit in array self.itemcount
.
since buttons have same name, need use approach.
# in addrow self.increase.clicked.connect(lambda: self.onincrease(itemcount)) def onincrease(self, edit): edit.settext(str(int(edit.text()+1))
Comments
Post a Comment