views:

85

answers:

1

Here my code snippet written in Qt.

bool myFunc()
{
  .......
  while(!tcpCommunicator->isLoginReplyExist)
  {             
    qApp->processEvents(QEventLoop::AllEvents);
  }
  .......
  return tcpCommunicator->res;
}

After "isLoginReplyExist" is changed by another part of program I want to exit from loop, is there any better way to accomplish this?

Thanks.

+1  A: 

if myFunc() is a method of some class (for example Class1) you can inherit this class from a QObject and define a slot:

Class1 : public QObject
{
Q_OBJECT
...
public slots:
void mySlot(bool tcpResult);
...
}

The object, which change the tcpCommunicator state (Class2) should have the signal:

Class2 : public QObject
{
Q_OBJECT
...
signals:
void tcpChangedTo(bool);
...
}

At last you should connect the signal and the slot. And so, when tcpCommunicator is changed, mySlot(bool res) is executed. If Class1 and Class2 objects are working in different threads you should use Qt::QueuedConnection type, when connecting the signal to the slot.

Andrew