crashing on accessing 'self' in the slots



  • I am trying to create a basic application in which I will have a listwidget , text Field and button. When ever user enters data in text Feild, using python I will validate the text and add to the list.
    To do this I wrote the following code,

    import sys
    from PyQt5.QtWidgets import QApplication, QWidget, QPushButton ,QLineEdit,QListWidget
    from PyQt5.QtCore import pyqtSlot
     
    class App(QWidget):
     
        def __init__(self):
            super().__init__()
            self.title = 'Some Title'
            self.left = 10
            self.top = 10
            self.width = 500
            self.height = 500
            self.initUI()
     
        def initUI(self):
            self.setWindowTitle(self.title)
            #self.setGeometry(self.left, self.top, self.width, self.height)
     
            button = QPushButton('PyQt5 button', self)
            button.setToolTip('This is an example button')
            button.move(400,450);
            button.clicked.connect(self.on_click)
    
            lineEdit = QLineEdit('lineEdit',self)
            lineEdit.move(0,450);
            lineEdit.resize(400,30);
            lineEdit.returnPressed.connect(self.onEnterClick)
    
            listWidgetComp = QListWidget(self)
            listWidgetComp.move(0,0);
            listWidgetComp.resize(500,300);
            listWidgetComp.addItem("Some thing")
     
            self.show()
     
        @pyqtSlot()
        def on_click(self):
            print('PyQt5 button click')
            #self.button.move(400,250); // This is causing crash
            
        @pyqtSlot()
        def onEnterClick(self):
            print("onEnterPressed "+listWidgetComp.count())    
            
     
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        ex = App()
        sys.exit(app.exec_())
    
    

    When I try to access 'self' in the slot, application is crashing. How to access the elements which are created, in the slots or please suggest the way to achieve the requirement.

    Thanks in advance.



  • I have changed the code like below and it is working :-)

    import sys
    from PyQt5.QtWidgets import QApplication, QWidget, QPushButton ,QLineEdit,QListWidget
    from PyQt5.QtCore import pyqtSlot
     
    class App(QWidget):
     
        def __init__(self):
            super().__init__()
            self.title = 'some title'
            self.left = 10
            self.top = 10
            self.width = 500
            self.height = 500
            self.initUI()
     
        def initUI(self):
            self.setWindowTitle(self.title)
            #self.setGeometry(self.left, self.top, self.width, self.height)
     
            self.button = QPushButton('PyQt5 button', self)
            self.button.setToolTip('This is an example button')
            self.button.move(400,450);
            self.button.clicked.connect(self.on_click)
    
            self.lineEdit = QLineEdit('lineEdit',self)
            self.lineEdit.move(0,450);
            self.lineEdit.resize(400,30);
            self.lineEdit.returnPressed.connect(self.onEnterClick)
    
            self.listWidgetComp = QListWidget(self)
            self.listWidgetComp.move(0,0);
            self.listWidgetComp.resize(500,300);       
     
            self.show()
     
        @pyqtSlot()
        def on_click(self):
            print('PyQt5 button click')                
            
        @pyqtSlot()
        def onEnterClick(self):
            self.listWidgetComp.addItem(self.lineEdit.text())
            
     
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        ex = App()
        sys.exit(app.exec_())
    
    

Log in to reply