繁体   English   中英

从变量中获取文本并在另一个类的QLineedit中显示

[英]Get the text from a variable and show it in a QLineedit in a different class

我试图从类A获取一个veriable的文本,并在每次变量更改文本时在QLineEdit使用它的文本来显示它。 像这样:

class A(self):
  variable = None

  def __init__(self):
    pass

  def get_text(self):
    self.variable = "Hello" #Every time that this value changes, it must be shown in the QLineEdit

class B(self):
  def __init__(self):
    pass

  def show_text(self):
    qle = QLineEdit()
    qle.setText(A.variable)

我想做的是获取鼠标的坐标,将其设置为str并将其显示在我在QtDesigner创建的QDialogQLineEdit

我怎样才能做到这一点? 希望您能够帮助我。

编辑

我需要class B成为QDialog 我添加了一些更改,以使其更易于理解。

class A(QMainWindow):
  def __init__(self):
    pass

  def get_text(self, event):
    if event.button == 1:
      self.variable = event.xdata

  @property
  def variable(self):
    self.b.qle.setText()

  @variable.setter
  def variable(self, value):
    self.b.qle.setText(value) 

class B(QDialog):
  def __init__(self, parent):
    QDialog.__init__(self, None)
    # you need to store the lineedit as a instance attribute of B so it can be accessed in A
    self.qle = QLineEdit()

有什么我想念的吗?

您可以将变量定义为Python属性,以便每次更新变量时都可以运行一个方法。 如果要在存储值之前对值进行检查(例如,确保它是有效数字或您喜欢的任何数字),这也很有用。

您可以这样做:

class A(self):
  variable = None

  def __init__(self):
    # you need a reference to the instance of the B class for this to work. 
    # dependning on how you create B currently, you might need to adapt this code
    self.b = B() 

  def get_text(self):
    self.variable = "Hello" #Every time that this value changes, it must be shown in the QLineEdit

  @property
  def variable(self):
    return self.b.qle.text()

  @variable.setter
  def variable(self, value):
    # do any checks you want
    self.b.qle.setText(value)

class B(self):
  def __init__(self):
    # you need to store the lineedit as a instance attribute of B so it can be accessed in A
    self.qle = QLineEdit()

如果你不想实例BA ,你可以实例化时,而不是传递到线编辑参考A

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM