hi,<br><br>I am pyqt beginner and followed tutorials on the internet to learn it until recently. Then I have wanted to develop my own application. I am developing a simple chat application.<br><br>I want to have 2 windows running independently (in other words two separate applications), one for server and one for client. When server starts to run, it is going to wait for client to run, and they will chat. So far, I could only start doing server side. I am using QThread, which is a subject rather unclear to me. I am getting this error when I close my application from X button:<br>
<br>QThread: Destroyed while thread is still running<br><br>I am guessing that this occurs because I am not closing the thread safely before application exists. I was wondering how I can make my thread respond to application exist so that it can close itself. Here is my code:<br>
<br>====<br>chat_server.py<br>====<br><br><div style="margin-left: 40px;">import sys<br>from PyQt4 import QtCore,QtGui<br>from chat_ui import Ui_MainWindow<br>import socket<br><br>class Server(QtGui.QMainWindow):<br> <br>
def __init__(self,parent=None):<br> super(Server,self).__init__()<br> self.ui = Ui_MainWindow()<br> self.ui.setupUi(self)<br> <br> self.ui.textEdit.append("Waiting for inbound connections")<br>
self.netconnector = NetConnectWorker(self)<br> self.netconnector.attemptConnect()<br> <br> self.connect(self.netconnector,QtCore.SIGNAL("Connected()"),self.connected)<br> self.connect(self.netconnector,QtCore.SIGNAL("dataRecieved(QString)"),self.messageRecieved)<br>
self.connect(self.netconnector,QtCore.SIGNAL("socketError(QString)"),self.socketError)<br><br> def connected(self):<br> self.ui.textEdit.append("Connected")<br> <br> def messageRecieved(self,data):<br>
self.ui.textEdit.append(data)<br> <br> def socketError(self,msg):<br> print msg<br> self.ui.textEdit.append("Encountered a socket error:\n %s" % msg)<br> <br><br>class NetConnectWorker(QtCore.QThread):<br>
<br> def __init__(self,parent=None):<br> super(NetConnectWorker,self).__init__(parent)<br> self.exiting = False<br> self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)<br> self.host = ''<br>
self.port = 50000<br> self.backlog = 5<br> self.address = ""<br> self.client = ""<br> <br> def __del__(self):<br> self.exiting = True<br> self.wait()<br>
if self.client:<br> self.client.close()<br> <br> def attemptConnect(self):<br> self.start()<br> <br> def run(self):<br> try:<br> self.socket.bind((self.host,self.port))<br>
except socket.error,error:<br> print dir(error)<br> self.emit(QtCore.SIGNAL("socketError(QString)"),error.message)<br> self.exit() <br> self.socket.listen(self.backlog)<br>
<br> self.client , self.address = self.socket.accept()<br> <br> if self.client:<br> self.emit(QtCore.SIGNAL("Connected()"))<br> <br> while not self.exiting:<br>
data = self.client.recv(1024)<br> if data:<br> self.emit(QtCore.SIGNAL("dataRecieved(QString)"),data)<br> <br> <br> <br>if __name__ == "__main__":<br>
app = QtGui.QApplication(sys.argv)<br> ex=Server()<br> ex.show()<br> sys.exit(app.exec_())<br></div>