from PyQt5.QtCore import QEventLoop, QTimer def my_function(): print("Hello world!") loop = QEventLoop() QTimer.singleShot(2000, loop.quit) # Wait for 2 seconds (2000 ms) before executing my_function loop.exec_() my_function()
from PyQt5.QtCore import QEvent, QEventLoop class CustomEvent(QEvent): def __init__(self): super().__init__(QEvent.User) def some_action(self): print("Custom event handled successfully!") def __repr__(self): return "In this example, we define a CustomEvent class that inherits from QEvent and implements a custom action that is handled when the event is executed. We then create an instance of this class and use loop.processEvents(QEventLoop.AllEvents) to manually trigger the event handling. Finally, we check if the event was accepted and execute the custom action if not." loop = QEventLoop() event = CustomEvent() loop.processEvents(QEventLoop.AllEvents) if not event.isAccepted(): event.some_action()