Home · Modules · Classes · Namespaces · Functions

QxtSignalWaiter Class Reference
[QxtCore module]

The QxtSignalWaiter class blocks and processes events until a signal is emitted More...

    #include <QxtSignalWaiter>

Inherits QObject.

Public Functions

Public Slots

Static Public Members

Additional Inherited Members


Detailed Description

The QxtSignalWaiter class blocks and processes events until a signal is emitted

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);
        }
    }

Note: 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 Function Documentation

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

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

void QxtSignalWaiter::cancelWait ()   [slot]

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.

bool QxtSignalWaiter::hasCapturedSignal () const

Returns true if the desired signal was emitted during the last wait() call.

void QxtSignalWaiter::signalCaught ()   [slot]

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.

bool QxtSignalWaiter::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. Waits for signal from sender to be emitted within msec while processing events according to flags. Returns true if the signal was caught, or false if the timeout elapsed.

bool QxtSignalWaiter::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.


Copyright © 2007-2011 Qxt Foundation
Qxt 0.6.2