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. crashing on accessing 'self' in the slots
Forum Updated to NodeBB v4.3 + New Features

crashing on accessing 'self' in the slots

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 2 Posters 1.4k 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.
  • Y Offline
    Y Offline
    Yaswanth
    wrote on last edited by Yaswanth
    #1

    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.

    1 Reply Last reply
    0
    • Y Offline
      Y Offline
      Yaswanth
      wrote on last edited by
      #2

      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_())
      
      
      1 Reply Last reply
      0
      • G Offline
        G Offline
        Giannis G
        wrote on last edited by
        #3

        You have created the button variable without the "self." and you try to using it with the "self."
        you have to change this line:

        button = QPushButton('PyQt5 button', self)

        with this:

        self.button = QPushButton('PyQt5 button', self)

        and then you can access it with:

        self.button.move(400,250)

        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