进入Qt事件循环后如何自动执行一个方法?

Ben*_*min 4 python multithreading timer event-loop pyqt4

我想执行一个只能在QApplication显示my 后调用的方法,即当它进入主事件循环时exec_()。我是 Qt4 的新手(使用 PyQt4):我希望有一个类似on_start()的回调,但没有找到。我需要创建一个线程或一个计时器吗?或者 API 中是否已经包含了一些回调?

Vin*_*jip 5

您可以为此使用单次计时器,如以下简单脚本所示:

import sys
from PyQt4 import QtGui, QtCore

app = QtGui.QApplication(sys.argv)

def on_start():
    print(' in event loop!')
    print(' telling app to exit ...')
    app.exit(123)

QtCore.QTimer.singleShot(0, on_start)
print('About to enter event loop')
rc = app.exec_()
print('All done - returned %d' % rc)
Run Code Online (Sandbox Code Playgroud)

当你运行这个时,你应该看到

About to enter event loop
 in event loop!
 telling app to exit ...
All done - returned 123
Run Code Online (Sandbox Code Playgroud)


Ben*_*min 0

现在我选择使用QThread这种方式:

class MyThread(QtCore.QThread):
    def run(self):
    ''' reinplemented from parent '''
        # make thread sleep to make sure
        # QApplication is running before doing something
        self.sleep(2)
        do_something()

class MyWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.attr = 'foo'
        self.thread = MyThread(self)
        self.thread.start()

def main():
    app = QtGui.QApplication(sys.argv)
    w = MyWidget()
    w.show()
    sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)

它适合我的目的,因为我的(真实)代码中的线程实际上在 X 显示中查找应用程序窗口,并且会尝试这样做,直到找到它为止。但除此之外,这并不是解决问题的优雅方法。QApplication如果进入事件循环时发出信号就更好了。JAB 提出了Qt.ApplicationActivate但它似乎不是由 发出的QApplication,即使是,因为MyWidget()没有实例化为 的子级QApplication,我不知道如何将信号从 传递appw

在接受我的答案作为所选解决方案之前,我会等待更好的答案(如果有)。