简体   繁体   中英

How to execute a method automatically after entering Qt event loop?

I would like to execute a method which can only be called once my QApplication is displayed, ie when it has entered its main event loop exec_() . I'm new to Qt4 (using PyQt4): i was hoping to have a on_start() -like callback, but didn't find one. Do i need to create a thread or a timer? Or is there some callback included in the API already?

You can use a single-shot timer for this, as in the following simple script:

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)

when you run this, you should see

About to enter event loop
 in event loop!
 telling app to exit ...
All done - returned 123

I haven't used Qt before, so I might be wrong on this, but I imagine you could bind your on_start() method to the ApplicationActivate event, and set a flag from inside your on_start() method so that the code would only be run that very first time and not any other times the ApplicationActivate event is triggered during execution of your program.

On the other hand if the on_start() method would be called by multiple threads, you might want to use thread-specific flags. Haven't done much multithreading myself, so I'm not sure of what the specifics for that would be, or how simple/complicated it would be.

For now i have chosen to use QThread this way:

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_())

It works for my purpose because my thread in my (real) code actually looks for the application window in X display and will try doing so until it finds it. But otherwise that's not an elegant way to solve the problem. It would be nicer if there was a signal emitted by QApplication when entering the event-loop. JAB proposed the Qt.ApplicationActivate but it doesn't seem to be emitted by QApplication , and even if it was, because the MyWidget() is not instantiated as a child of QApplication , i wouldn't know how to pass the signal from app to w

I'll wait for a better answer, if any, before accepting my answer as the chosen solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM