【发布时间】:2016-05-16 16:13:08
【问题描述】:
我有两个不同的类,我需要使用一个信号将变量中的文本从一个类发送到另一个类。
第一类是 Matplotlib 图。我在导航工具栏中添加了一个QPushButton,以访问带有QLineEdit 的QDialog(这是另一个类)。我需要使用一个变量将QLineEdit中写的文本发送到 Matplotlib 图形的类,这样我就可以在图形中创建一个注释。
这是目前为止的代码:
class Dialog(QDialog):
list_of_notes = [] #I create a list to add text, each time i open the 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):
for i,s in enumerate(Dialog.list_of_notes):
note = self.axes.annotate(i, xy=(0.2, 0.2)) #This is the note i want to create
note.draggable()
self.figure_canvas.draw_idle()
所以,当我按下QDialog 中的“接受”按钮时,我可以看到文本已添加到列表中,每次打开对话框时。但是,当我想从添加到列表中的最后一个对象创建注释时,在 for 循环中没有任何反应。
有什么问题?希望你能帮助我。
----------------编辑------------------
我做了一些修改。我创建了一个信号mysignal 来发出变量text,其中包含我在QDialog 中编写的文本。
这是修改后的代码:
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 get the text from the QLineEdit and send it 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 the QDialog clicking a 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()
所以,我打开QDialog,我可以写文字了。已正确添加到列表中,但图中仍未绘制注释。
【问题讨论】:
标签: python matplotlib pyqt signals-slots