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. Calling a function from a slot
Forum Updated to NodeBB v4.3 + New Features

Calling a function from a slot

Scheduled Pinned Locked Moved Solved Qt for Python
8 Posts 3 Posters 1.3k 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.
  • B Offline
    B Offline
    BD4L
    wrote on last edited by
    #1

    Hi friends, I'm trying to figure out how to call a function from a Slot. The reason I want this as a function is it is something that will be called from multiple different slots. Here is an example of what I'm trying to make work:

    from PySide2.QtWidgets import QApplication, QWidget, QPushButton, QLineEdit, QVBoxLayout
    from PySide2.QtCore import Qt, Slot
    
    from sys import exit as sysExit
    
    class Widget(QWidget):
        def __init__(self):
            QWidget.__init__(self)
            self.ButtonA = QPushButton("Button A")
            self.ButtonA.clicked.connect(self.on_A_click)
    
            self.ButtonB = QPushButton("Button B")
            self.ButtonB.clicked.connect(self.on_B_click)
    
            self.TextA = QLineEdit()
            self.TextB = QLineEdit()
    
            FormLayout = QVBoxLayout()
            FormLayout.addWidget(self.TextA)
            FormLayout.addWidget(self.TextB)
            FormLayout.addWidget(self.ButtonA)
            FormLayout.addWidget(self.ButtonB)
    
            self.setLayout(FormLayout)
        
        def myFunction(s):
            # Do something with s. Data = s just used to allow file to run
            data = s
    
        @Slot()
        def on_A_click(self, Item):
            getData = self.TextA.text()
            myFunction(getData)
    
        @Slot()
        def on_B_click(self, Item):
            getData = self.TextB.text()
            myFunction(getData)
    
    if __name__ == "__main__":
        MainEventHandler = QApplication([])
    
        application = Widget()
        application.show()
    
        sysExit(MainEventHandler.exec_())
    
    1 Reply Last reply
    0
    • B Offline
      B Offline
      BD4L
      wrote on last edited by
      #8

      Thanks guys. Once I realized this was part of class structures I did some reading on that and I've got the code working now. Adding the code I used to make it work below in case anyone looks at this post in the future.

      def myFunction(self, s):
              self.TextB.setText(s)
      
          @Slot()
          def on_A_click(self, Item):
              getData = self.TextA.text()
              self.myFunction(getData)
      
      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        Any reason for the missing self parameter in that function definition ?

        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
        0
        • B Offline
          B Offline
          BD4L
          wrote on last edited by
          #3

          I'm not super familiar with QT yet. I tried changing that line to read

          def myFuntion(self, s):
          

          but I'm not sure if that's right. I still received this error when trying to click one of the buttons.

          NameError: name 'myFunction' is not defined
          
          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            That's not a Qt issue.

            What is your level with Python ?

            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
            0
            • B Offline
              B Offline
              BD4L
              wrote on last edited by BD4L
              #5

              Self taught, have built several programs but all command line only. Just started trying to figure out GUI coding with QT in the last few weeks. When calling functions from a CLI program I've never had to use self, just call the function name directly so that piece is all new to me. Edit: Looks like self is related to using the class structure which I've never needed to use before this work with QT and am only using as needed for the GUI interface.

              JonBJ 1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #6

                Ok, what is your level with regard to object oriented programming and concurrency ?

                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
                1
                • B BD4L

                  Self taught, have built several programs but all command line only. Just started trying to figure out GUI coding with QT in the last few weeks. When calling functions from a CLI program I've never had to use self, just call the function name directly so that piece is all new to me. Edit: Looks like self is related to using the class structure which I've never needed to use before this work with QT and am only using as needed for the GUI interface.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #7

                  @BD4L
                  In the most encouraging way possible, you need to learn about classes & self. You need that anyway, but you definitely need to get familiar with it to do Qt stuff. I'm not a good book recommender but there is loads out there about this topic (you'll also see "object oriented programming"), see if you can find something to read or watch to get the idea.

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    BD4L
                    wrote on last edited by
                    #8

                    Thanks guys. Once I realized this was part of class structures I did some reading on that and I've got the code working now. Adding the code I used to make it work below in case anyone looks at this post in the future.

                    def myFunction(self, s):
                            self.TextB.setText(s)
                    
                        @Slot()
                        def on_A_click(self, Item):
                            getData = self.TextA.text()
                            self.myFunction(getData)
                    
                    1 Reply Last reply
                    0

                    • Login

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