【问题标题】:Passing data between classes with Signals in PyQt在 PyQt 中使用信号在类之间传递数据
【发布时间】: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


    【解决方案1】:

    问题在于代码行

    MatplotlibFigure().addNote()
    

    这一行实例化了MatplotlibFigure 类的new 实例并调用addNote 方法。但是您不想每次打开对话框时都创建一个新图形,而是要将注释添加到(可能)预先存在的 MatplotlibFigure 实例中。

    因为您没有显示创建QDialog 的代码,所以很难建议选项,但可以将对现有MatplotlibFigure 的引用传递给QDialog 的构造函数。比如

    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()
    

    修改创建Dialog 的代码以传入对图的引用。例如my_dialog = Dialog(reference_to_my_figure)

    我还想指出,将笔记列表存储在类属性中并不是一个特别好的编码习惯。我认为你甚至不需要存储笔记列表,除非你打算重用它们(你当前的代码再次添加所有以前添加的笔记,每次添加新笔记时。这样的东西应该可以正常工作(我'如果您真的想这样做,我们已经在列表中包含了可选存储)

    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)
    
          # now add the note to the figure
          note = self.axes.annotate(text, xy=(0.2, 0.2))  #This is the note i want to create
          note.draggable()
          self.figure_canvas.draw_idle()
    

    但请注意,您的所有注释都将指向图形(0.2,0.2) 上的同一位置(我假设您将在以后的代码中解决此问题)。

    我还想推荐你做一些关于面向对象编程的 Python 教程,特别是涵盖类与实例(或对象)、类属性与实例属性以及如何设计可重用类的内容。看起来您的一些问题源于您对这些概念的理解不够全面。

    【讨论】:

    • 感谢您的回答@three_pineapples,很抱歉回复晚了,这些天我的电脑出现了一些问题。我做了一些修改,我添加了你告诉我的行,但我仍然有同样的问题。你说得对,我不完全理解这个概念,但我在这方面学到了很多东西。再次感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多