Ejemplo n.º 1
0
 def __call__(self, func, *args, **kwds):
     global qCallAfter
     #  If the app is running, dispatch the event directly.
     if self.app is not None:
         self.func_queue.put((func, args, kwds))
         self._postEvent()
         return
     #  Otherwise, we have some bootstrapping to do!
     #  Before dispatching, there must be a running app and we must
     #  be on the same thread as it.
     app = QtCore.QCoreApplication.instance()
     if app is None or not qIsMainThread():
         self.pending_func_queue.put((func, args, kwds))
         return
     #  This is the first call with a running app and from the
     #  main thread.  If it turns out we're not on the main thread,
     #  replace ourselves with a fresh instance.
     if hasattr(self, "thread"):
         if self.thread() is not QtCore.QThread.currentThread():
             qCallAfter = self.__class__()
         else:
             self.app = app
     else:
         self.app = app
     #  OK, we now have the official qCallAfter instance.
     #  Flush all pending events.
     try:
         while True:
             (pfunc, pargs, pkwds) = self.pending_func_queue.get(False)
             qCallAfter(pfunc, *pargs, **pkwds)
     except Queue.Empty:
         pass
     qCallAfter(func, *args, **kwds)
Ejemplo n.º 2
0
 def __call__(self, func, *args, **kwds):
     global qCallAfter
     #  If the app is running, dispatch the event directly.
     if self.app is not None:
         self.func_queue.put((func, args, kwds))
         self._postEvent()
         return
     #  Otherwise, we have some bootstrapping to do!
     #  Before dispatching, there must be a running app and we must
     #  be on the same thread as it.
     app = QtCore.QCoreApplication.instance()
     if app is None or not qIsMainThread():
         self.pending_func_queue.put((func, args, kwds))
         return
     #  This is the first call with a running app and from the
     #  main thread.  If it turns out we're not on the main thread,
     #  replace ourselves with a fresh instance.
     if hasattr(self, "thread"):
         if self.thread() is not QtCore.QThread.currentThread():
             qCallAfter = self.__class__()
         else:
             self.app = app
     else:
         self.app = app
     #  OK, we now have the official qCallAfter instance.
     #  Flush all pending events.
     try:
         while True:
             (pfunc, pargs, pkwds) = self.pending_func_queue.get(False)
             qCallAfter(pfunc, *pargs, **pkwds)
     except Queue.Empty:
         pass
     qCallAfter(func, *args, **kwds)
Ejemplo n.º 3
0
def qCallInMainThread(func, *args, **kwds):
    """Synchronously call the given function in the main thread.

    This helper arranges for the given function to be called in the main
    event loop, then blocks and waits for the result.  It's a simple way to
    call functions that manipulate the GUI from a non-GUI thread.
    """
    if qIsMainThread():
        func(*args, **kwds)
    else:
        future = Future.get_or_create()
        qCallAfter(future.call_function, func, *args, **kwds)
        return future.get_result()
Ejemplo n.º 4
0
def qCallInMainThread(func, *args, **kwds):
    """Synchronously call the given function in the main thread.

    This helper arranges for the given function to be called in the main
    event loop, then blocks and waits for the result.  It's a simple way to
    call functions that manipulate the GUI from a non-GUI thread.
    """
    if qIsMainThread():
        func(*args, **kwds)
    else:
        future = Future.get_or_create()
        qCallAfter(future.call_function, func, *args, **kwds)
        return future.get_result()