Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Set QLineEdit text from function in second file
Forum Updated to NodeBB v4.3 + New Features

Set QLineEdit text from function in second file

Scheduled Pinned Locked Moved Unsolved Qt for Python
5 Posts 4 Posters 497 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Samuel Bachorik
    wrote on 12 Jan 2021, 19:46 last edited by Samuel Bachorik 1 Dec 2021, 19:49
    #1

    Hello, iam trying to set text from function in other file and it is not working can you help me with this ? Let me give you example....

    My button is connected to function clicked0 and this button should change text.
    This is my main file

       class Window(QMainWindow):
    
       def __init__(self):
           super().__init__()
           self.textboxvalue2 = ""
    
    
           self.setWindowTitle("Samuel calcâ„¢")
           self.setGeometry(200, 200, 259, 258)
    
           self.textbox = QLineEdit(self)
           self.textbox.move(5, 5)
           self.textbox.resize(188, 35)
           self.textbox.setText(str(self.textboxvalue2))
           self.textbox.setPlaceholderText("Enter numbers")
           self.textboxValue = self.textbox.text()
    
           self.b10 = QtWidgets.QPushButton(self)
           self.b10.setText("0")
           self.b10.move(68, 219)
           self.b10.resize(60, 35)
           self.b10.clicked.connect(self.clicked0)
    
    
           self.show()
    
       def setExpression(self,expression):
           print(expression)
           self.textbox.setText(str(expression))
    
    
       def clicked0(self):
           import AbstratctNumberButton
           AbstratctNumberButton.onClick()
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        window = Window()
        sys.exit(app.exec())
    

    And this is my second file from where i call function onClick()
    secondFile.py

    from main import Window
    
    def onClick():
        print("abstract button")
        dj = Window()
        dj.setExpression(str(0))
    

    When i run this my button not working.. when i put this function in main.py file then it is wokking

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 12 Jan 2021, 20:08 last edited by
      #2

      Hi,

      "dj" lifetime is related to onClick.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • S Offline
        S Offline
        Samuel Bachorik
        wrote on 13 Jan 2021, 11:37 last edited by
        #3

        Iam sorry i dont understand ? What should i do to get it work ?

        J J 2 Replies Last reply 13 Jan 2021, 11:57
        0
        • S Samuel Bachorik
          13 Jan 2021, 11:37

          Iam sorry i dont understand ? What should i do to get it work ?

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 13 Jan 2021, 11:57 last edited by
          #4

          @Samuel-Bachorik dj is a local variable inside onClick. It is destroyed as soon as onClick finishes. Make dj a variable in secondFile.py or a member variable if onClick is in a class.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • S Samuel Bachorik
            13 Jan 2021, 11:37

            Iam sorry i dont understand ? What should i do to get it work ?

            J Offline
            J Offline
            JonB
            wrote on 13 Jan 2021, 11:59 last edited by
            #5

            @Samuel-Bachorik
            Probably make dj a member variable in self. As it stands, dj = Window() creates a Window, but then goes out of scope and so the Window gets destroyed as soon as you exit the onClick() slot.

            However, I suspect there is/may be some confusion here. You create one window = Window in the __main__ scope. Window is a QMainWindow. You then create a new, separate Window in onClick. Why? In normal case your program should have only one QMainWindow, why do you want a second one? If you want another window, why make it a QMainWindow?

            1 Reply Last reply
            3

            5/5

            13 Jan 2021, 11:59

            • Login

            • Login or register to search.
            5 out of 5
            • First post
              5/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved