Frage

I know how to wait for a single object to finish, using

QEventLoop eventLoop;
connect(&obj, SIGNAL(finished()), &eventLoop, SLOT(quit()));
eventLoop.exec();

But now I have several objects which I want to 'run' in parallel, so I need to wait until all of them have sent their finished() SIGNALs.

This would be like a signals-and-slots version of the WaitForMultipleObjects WinApi function.

How should I go about doing that?

War es hilfreich?

Lösung

I would connect the finished signal to a class that counts the # of signals received and emits quit() when it hits the expected count.

Something like this:

class EmitIfCountReached : public QObject
{
Q_OBJECT
public:
   EmitIfCountReached( int expectedCount, QObject* parent =  nullptr) : m_expected(expectedCount), m_count(0), QObject(parent) {}
signals:
   void finished();

protected slots:
   void increment() {
      m_count++;
      if (m_count >= m_expected) {
         emit finished();
      }
   }

protected:
   int m_count;
   int m_expected;
};
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top