
class TextField:
    def __init__(self, model, view):
        self.model = model
        self.view = view
        self.model.view = self
        self.view.editingFinished.connect(self.editingFinished)
    
    def editingFinished(self):
        self.model.text = self.view.text()
    
    # model --> view
    def refresh(self):
        self.view.setText(self.model.text)
    


class TextField:
    def __init__(self, model, view):
        self.model = model
        self.view = view
        self.model.view = self
        # Make TextField also work for QLabel, which doesn't allow editing
        if hasattr(self.view, 'editingFinished'):
            self.view.editingFinished.connect(self.editingFinished)
    
    def editingFinished(self):
        self.model.text = self.view.text()
    
    # model --> view
    def refresh(self):
        self.view.setText(self.model.text)
    

Make TextField also work with a QLabel view, which doesn't allow editing.
Make TextField also work with a QLabel view, which doesn't allow editing.

  
  class TextField:
      def __init__(self, model, view):
          self.model = model
          self.view = view
          self.model.view = self
+         # Make TextField also work for QLabel, which doesn't allow editing
+         if hasattr(self.view, 'editingFinished'):
-         self.view.editingFinished.connect(self.editingFinished)
+             self.view.editingFinished.connect(self.editingFinished)
      
      def editingFinished(self):
          self.model.text = self.view.text()
      
      # model --> view
      def refresh(self):
          self.view.setText(self.model.text)
      
  
Make TextField also work with a QLabel view, which doesn't allow editing.
## Code Before:

class TextField:
    def __init__(self, model, view):
        self.model = model
        self.view = view
        self.model.view = self
        self.view.editingFinished.connect(self.editingFinished)
    
    def editingFinished(self):
        self.model.text = self.view.text()
    
    # model --> view
    def refresh(self):
        self.view.setText(self.model.text)
    

## Instruction:
Make TextField also work with a QLabel view, which doesn't allow editing.
## Code After:

class TextField:
    def __init__(self, model, view):
        self.model = model
        self.view = view
        self.model.view = self
        # Make TextField also work for QLabel, which doesn't allow editing
        if hasattr(self.view, 'editingFinished'):
            self.view.editingFinished.connect(self.editingFinished)
    
    def editingFinished(self):
        self.model.text = self.view.text()
    
    # model --> view
    def refresh(self):
        self.view.setText(self.model.text)
    
