QxtSignalWaiter Class Reference
[QxtCore module]

Block and process events until a signal is emitted. More...

 #include <QxtSignalWaiter>

Inherits QObject.

Public Functions

Public Static Functions

Public Slots


Detailed Description

In many cases, writing code that assumes certain actions are synchronous is considerably simpler than breaking your function into multiple blocks and using signals and slots to connect them all. Using this class, QSignalWaiter::wait will block until a certain signal is emitted and then return. The return value is true if the signal was caught, or false if a user-specified timeout elapses before catching the signal.

void MyObject::myFunction() {
     QxtSignalWaiter waiter(myOtherObject, SIGNAL(longProcessFinished()));
     myOtherObject->longProcess();
     if(waiter.wait(5000)) {
         doSomething(myOtherObject->information());
         } else {
         QMessageBox::information(0, "MyObject", "Timed out while waiting on longProcessFinished()", QMessageBox::Ok);
     }
}

Bug

QxtSignalWaiter is not reentrant. In particular, only one QxtSignalWaiter object per thread can be safely waiting at a time. If a second QxtSignalWaiter is used while the first is waiting, the first will not return until the second has timed out or successfully caught its signal.

Member Documentation

QxtSignalWaiter ( const QObject * sender , const char * signal )

Constructs a QxtSignalWaiter that will wait for sender::signal() to be emitted. QxtSignalWaiter objects are intended to be created on the stack, therefore no parent parameter is accepted.




bool wait ( int msec = -1 , QEventLoop::ProcessEventsFlags flags = QEventLoop::AllEvents )

Blocks the current function until sender::signal() is emitted. If msec is not -1, wait() will return before the signal is emitted if the specified number of milliseconds have elapsed. Returns true if the signal was caught, or false if the timeout elapsed. Note that wait() may continue to block after the signal is emitted or the timeout elapses; the function only guarantees that it will not return BEFORE one of these conditions has occurred. This function is not reentrant.




bool hasCapturedSignal ( ) const

Indicates whether the desired signal was emitted during the last wait() call.




bool wait ( const QObject * sender , const char * signal , int msec = -1 , QEventLoop::ProcessEventsFlags flags = QEventLoop::AllEvents ) static

This is an overloaded function provided for convenience. This version can be invoked without first instantiating a QxtSignalWaiter object.




void signalCaught ( )

Signals a waiting object to stop blocking because the desired signal was emitted. QxtSignalWaiter::hasCapturedSignal() will return true after this slot is invoked. Use this slot to allow QxtSignalWaiter to wait for the first of multiple signals.




void cancelWait ( )

Signals a waiting object to stop blocking because the timeout has elapsed. QxtSignalWaiter::hasCapturedSignal() will return false after this slot is invoked. Use this slot to allow QxtSignalWaiter to be interrupted for reasons other than a timeout.






Copyright © 2007 Qxt Foundation Trademarks
Qt Extension Library 0.4