Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Calling a function from a slot



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


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

  • Lifetime Qt Champion

    Hi,

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



  • 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
    

  • Lifetime Qt Champion

    That's not a Qt issue.

    What is your level with Python ?



  • 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.


  • Lifetime Qt Champion

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



  • @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.



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

Log in to reply