0

I'm trying to get the text of a veriable from a class A, and use the text of it in a QLineEditto show it every time that the text changes in the variable. Something like this:

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)

What i am trying to do is get the coordinates of the mouse, set them as str and show them in a QLineEdit in a QDialog that i created in QtDesigner

How can i accomplish this? Hope you can help me.

EDIT

I need class B to be a QDialog. I added some changes to make it more readable for what i need.

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

Is there anything that i am missing?

Pablo Flores
  • 667
  • 1
  • 13
  • 33

1 Answers1

1

You can define your variable as a Python property so that you can run a method every time the variable is updated. This is also useful if you want to run checks on the value before storing it (eg make sure it is a valid number or whatever you feel like).

You can do this like so:

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

If you didn't want to instantiate B inside A, you could instead pass in a reference to the line edit when instantiating A.

three_pineapples
  • 11,579
  • 5
  • 38
  • 75
  • Thank you for your answer @three_pineapples. Do i have to write `@property` above the method?. Sorry, I am new in python and I do not know what is that call. – Pablo Flores Mar 14 '16 at 23:53
  • @PabloFlores Yes, It is called a decorator. You can read about the property decorator [here](http://stackoverflow.com/a/17330273/1994235) and [here](https://docs.python.org/2/library/functions.html#property) – three_pineapples Mar 15 '16 at 02:55
  • Thank you, i learned a lot from here. I tried this and it does not work. I edited my question with some changes that i made for another attributes that i need for this figure. – Pablo Flores Mar 15 '16 at 14:12
  • Is there anything that i am missing? – Pablo Flores Mar 15 '16 at 14:21