1

Pyqt App crashing after sometime. my best guess would be that it's a thread safety issue.

Here is the error code :

    Problem signature:
Problem Event Name: APPCRASH
Application Name: python.exe
Application Version: 0.0.0.0
Application Timestamp: 53b1ee02
Fault Module Name: QtCore4.dll
Fault Module Version: 4.8.7.0
Fault Module Timestamp: 5566ccf4
Exception Code: c0000005
Exception Offset: 0000000000046f63
OS Version: 6.3.9600.2.0.0.272.7
Locale ID: 1033
Additional Information 1: 434d
Additional Information 2: 434d5893bd4d95749e45b6c395b12927
Additional Information 3: dd24
Additional Information 4: dd241847526058ce8cc7a34e073c1515

I'm going to try to post a short version of the window's code :

class refreshTask(QtCore.QThread):

    def __init__(self, tableWidget):

        self.tableWidget = tableWidget
        QtCore.QThread.__init__(self)

    def run(self):

        while running:    
            try:
                self.conn = pymysql.connect(host=host,
                self.cur = self.conn.cursor()
                self.tableWidget.setRowCount(0)
                for result in results:

                    self.tableWidget.setRowCount(self.tableWidget.rowCount()+1)

                    self.cur.execute(sql, (result[0]))
                    entry = self.cur.fetchone()
                    self.emit(QtCore.SIGNAL('button ready'), result[0], entry[1], entry[2], str(result[1]),  i)
                    i += 1

            except :    
                self.tableWidget.setRowCount(0)

            time.sleep(5)


class Ui_MainWindow(QtGui.QMainWindow):

    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.conn = pymysql.connect(host=host,

        self.cur = self.conn.cursor()

    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(958, 587)
        self.centralWidget = QtGui.QWidget(MainWindow)
        self.centralWidget.setObjectName(_fromUtf8("centralWidget"))
        self.tableWidget = QtGui.QTableWidget(self.centralWidget)
        self.tableWidget.setGeometry(QtCore.QRect(100, 110, 800, 401))
        self.tableWidget.setRowCount(0)
        self.tableWidget.setColumnCount(5)
        self.tableWidget.setObjectName(_fromUtf8("tableWidget"))

        self.tableWidget.horizontalHeader().setCascadingSectionResizes(False)
        self.tableWidget.horizontalHeader().setMinimumSectionSize(50)
        self.tableWidget.verticalHeader().setDefaultSectionSize(50)
        self.tableWidget.setColumnWidth(0, 175)
        self.tableWidget.setColumnWidth(1, 175)
        self.tableWidget.setColumnWidth(2, 175)
        self.tableWidget.setColumnWidth(3, 175)
        self.tableWidget.horizontalHeader().setStretchLastSection(True)
        self.tableWidget.setHorizontalHeaderLabels(['username', 'IP Address', 'Port Number', 'Latest heartbeat', 'open gate'])
        self.label = QtGui.QLabel(self.centralWidget)
        self.label.setGeometry(QtCore.QRect(40, 40, 161, 31))
        self.label.setObjectName(_fromUtf8("label"))
        MainWindow.setCentralWidget(self.centralWidget)    
        self.refresher = refreshTask(self.tableWidget)
        self.refresher.start()
        self.connect(self.refresher, QtCore.SIGNAL("button ready"), self.funct)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):    
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.label.setText(_translate("MainWindow", "Server Status :", None))
    def funct(self, username, ip, port, hearbeat, i):

        item = QtGui.QTableWidgetItem(username)
        self.tableWidget.setItem(i, 0, item)
        item = QtGui.QTableWidgetItem(ip)
        self.tableWidget.setItem(i, 1, item)
        item = QtGui.QTableWidgetItem(port)
        self.tableWidget.setItem(i, 2, item)
        item = QtGui.QTableWidgetItem(str(hearbeat))
        self.tableWidget.setItem(i, 3, item)
        btn = QtGui.QPushButton(self.tableWidget)
        btn.setText('Open gate\n'+username)
        btn.clicked.connect(self.buttonClicked)
        self.tableWidget.setCellWidget(i, 4, btn)

    def buttonClicked(self):
        sender = self.sender()
        liste = sender.text()
        print liste + " was pressed"



def myExitHandler():

    global running
    running = False

if __name__ == "__main__":

    import sys
    app = QtGui.QApplication(sys.argv)
    app.aboutToQuit.connect(myExitHandler)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()    
    sys.exit(app.exec_())

I'm a bit lost here and don't know where to start. The GUI performs a simple task, fetches a list from a SQL database and inserts it into a widget list, this task is repeated every 5 seconds.

  • Looks like duplicate http://stackoverflow.com/questions/21051710/pyqt-crashing-out-as-a-windows-appcrash – Achayan Sep 08 '15 at 00:18
  • 1
    You must not attempt to update the gui from outside the main thread. Instead, emit a [custom signal](http://pyqt.sourceforge.net/Docs/PyQt4/new_style_signals_slots.html#defining-new-signals-with-pyqtsignal) from the thread and update the gui in a slot connected to it. – ekhumoro Sep 08 '15 at 02:48
  • Is it thread safe to call a function from the main thread, instead of emitting a custom signal that is connected to this function? – Guimoute Aug 22 '18 at 11:29

0 Answers0