2016-08-18 2 views
0

Я использую собственный поток python с потоком по умолчанию pyqt. Когда я хочу показать qmessagebox в собственном потоке, моя программа разбилась. Вот мой код:pyqt с нативной линией python qpixmap и родительской ошибкой

......... 
self.serverSoc.listen(5) 
self.status="Server listening on %s:%s" % serveraddr 
self.serverStatus = 1 
thread.start_new_thread(self.listenClients,()) 

def listenClients(self): 
    while 1: 
     clientsoc, clientaddr = self.serverSoc.accept() 
     print("Client connected from %s:%s" % clientaddr) 
     data = clientsoc.recv(self.buffsize) 
     if data.startswith('%sendchatrequest%'): 
      try: 
       requestuser = data.rsplit(':', 1)[1] 
       msg = "%s wanted to chat with you. Do you accept this?" %requestuser 

       reply = QtGui.QMessageBox.question(self, 'Chat Request', 
                msg, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) 

и я получил эту ошибку:

QObject::setParent: Cannot set parent, new parent is in a different thread 
QPixmap: It is not safe to use pixmaps outside the GUI thread 
QPixmap: It is not safe to use pixmaps outside the GUI thread 
QPixmap: It is not safe to use pixmaps outside the GUI thread 
QPixmap: It is not safe to use pixmaps outside the GUI thread 
QPixmap: It is not safe to use pixmaps outside the GUI thread 
QPixmap: It is not safe to use pixmaps outside the GUI thread 
QPixmap: It is not safe to use pixmaps outside the GUI thread 
X Error: BadImplementation (server does not implement operation) 17 
Major opcode: 20 (X_GetProperty) 
Resource id: 0x0 
[xcb] Unknown request in queue while dequeuing 
[xcb] Most likely this is a multi-threaded client and XInitThreads has not been called 
[xcb] Aborting, sorry about that. 
python2.7: ../../src/xcb_io.c:179: dequeue_pending_request: Assertion `!xcb_xlib_unknown_req_in_deq' failed. 

как я могу это исправить? Большое вам спасибо

ответ

0

Qt требует, чтобы все операции с графическим интерфейсом выполнялись в основном потоке.

Почему бы не использовать QTcpSocket вместо этого, который прекрасно интегрируется в mainlop Qt вместо требуемого потока?

Смежные вопросы