Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
877 views
in Technique[技术] by (71.8m points)

python - Cannot send posted events for objects in another thread

When I try to use one QDialog object from threads I get this error. Here is the code I'm using:

import threading
import test_qdialog
from PyQt4 import QtGui, QtCore


class MyThread(threading.Thread):
    def __init__(self, id, window, mutex):
        self.id = id
        self.window = window
        self.mutex = mutex
        super(MyThread, self).__init__()

    def run(self):
        with self.mutex:
            result = self.window.exec_()
            if result == QtGui.QDialog.Accepted:
                print "Thread %d: %s" % (self.id, self.window.message_input.text())


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)

    mutex = threading.Lock()
    threads = []
    window = test_qdialog.MyDialog()

    for i in range(5):
        thread = MyThread(i, window, mutex)
        thread.start()
        threads.append(thread)

    for thread in threads:
        thread.join()

    sys.exit(app.exec_())

As written in this answer, if I get it right, I can't do it this way. But how can I do it then?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can only create and use GUI widgets on main thread (every UI library that I know is like that). However, you can easily pass signals from threads to main using QtCore.QtThread. See for example the answer to PyQt threads and signals - how to properly retrieve values (even if the answer is not what the OP was looking for, it is relevant to your situation). May also find this SO post useful.

So instead of creating or accessing the dialog from thread, you would emit a signal from thread, and have your main window connected to it create the dialog when it receives the signal. Qt takes care of transfering data between threads. Will work like a charm.

Definitely take a close look at Qt Threading Basics, if you haven't already (if you have, may want to post questions about parts you don't understand, there is tons of important info there).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...