python - Passing data between classes with Signals in PyQt -
i have got 2 different classes need connect using signal sends text in variable 1 class, another.
the first class matplotlib figure. added qpushbuttonin navigation toolbar access qdialog (wich other class) qlineedit. need send text written in qlineeditto matplotlib figure´s class, using variable, can create note in figure.
this code far:
class dialog(qdialog): list_of_notes = [] #i create list add text, each time open qdialog def __init__(self): #a lot of stuff in here def printvariable(self, text): dialog.lisf_of_notes.append(text) matplotlibfigure().addnote() self.close() class matplotlibfigure(qmainwindow): def __init__(self): #a lot of stuff in here self.axes = self.figure_canvas.figure.add_subplot(111) def addnote(self): i,s in enumerate(dialog.list_of_notes): note = self.axes.annotate(i, xy=(0.2, 0.2)) #this note want create note.draggable() self.figure_canvas.draw_idle() so, when press button "accept" in qdialog can see text added list, each time open dialog. but, when want create note last object added list, in loop nothing happens.
what problem? hope can me.
----------------edit-----------------------------
i´ve made modifications. created signal mysignal emit variable text, have text wrote in qdialog.
this modificated code:
class dialog(qdialog): def __init__(self, my_figure): mysignal = pyqtsignal(str) self.my_figure = matplotlibfigure() qdialog.__init__(self) self.connect(self.btn_accept, signal("clicked()"), self.send_text) self.mysignal.connect(self.printvariable) def send_text(self): #i text qlineedit , send using text = self.lineedit.text() #the created signal self.mysignal.emit(text) def printvariable(self, text): self,my_figure.addnote(text) self.close() class matplotlibfigure(qmainwindow): def __init__(self): qmainwindow.__init__(self) self.notes_list =[] def opendialog(self, text): #i open qdialog clicking button textmat = dialog(text) textmat.exec_() def addnote(self, text): self.notes_list.append(text) notes = self.axes.annotate(text, xy=(0.2, 0.2), bbox = dict(facecolor="red")) notes.draggable() self.figure_canvas.draw_idle() so, open qdialog , can write text. added list correctly, note still not drawn in figure.
the problem line of code
matplotlibfigure().addnote() this line instantiates new instance of matplotlibfigure class , calls addnote method. don't want make new figure every time open dialog, want add note (presumably) pre-existing instance of matplotlibfigure.
because don't show code qdialog created, difficult suggest options, 1 pass reference existing matplotlibfigure constructor of qdialog. instance
class dialog(qdialog): list_of_notes = [] def __init__(self, my_figure): self.my_figure = my_figure ... def printvariable(self, text): dialog.list_of_notes.append(text) self.my_figure.addnote() self.close() modify code create dialog pass in reference figure. example my_dialog = dialog(reference_to_my_figure)
i point out storing list of notes in class attribute not particularly coding practice. don't think need store list of notes unless planning on reusing them (your current code adds added notes again, each time new note added. should work fine (i've included optional storing in list if want that)
class dialog(qdialog): def __init__(self, my_figure): self.my_figure = my_figure ... def printvariable(self, text): self.my_figure.addnote(text) self.close() class matplotlibfigure(qmainwindow): def __init__(self): #a lot of stuff in here self.notes_list = [] self.axes = self.figure_canvas.figure.add_subplot(111) def addnote(self, text): #optional self.notes_list.append(text) # add note figure note = self.axes.annotate(text, xy=(0.2, 0.2)) #this note want create note.draggable() self.figure_canvas.draw_idle() but aware notes going point same location on figure (0.2,0.2) (i assume fix in later code).
i recommend python tutorials on object oriented programming, covers classes vs. instances (or objects), class attributes vs. instance attributes , how design reusable classes. looks few of issues stem not quite understanding these concepts in full.
Comments
Post a Comment