1

I have a class whose internal state determines the layout in a QML file. Now this internal state is mostly determined by a state machine. However part of it is not determined by the state machine, but instead relies on other member variables to be set. I have the following situation:

void foo()
{
   emit stateChange1();
   ...
   mMember = true;
}

Here stateChange1() might change the state of the state machine, resulting in qml changes). mMember directly changes the qml. Now my problem is that it is crucial to process the (possible) state changes implied by statechange1() before the state changes implied by setting mMember. However since the QStateMachine works asynchronously, I cannot guarantee this. Are there any elegant solutions for this problem?

Frank
  • 2,446
  • 7
  • 33
  • 67

1 Answers1

1

Most likely, you need to submit the operation to either the thread's event queue or the state machine's event queue - thus it will be executed after the other operations are done.

Using code from this answer, transform the code as follows:

template <typename F>
static void postToStateMachine(F && fun, QStateMachine * sm) {
   sm->postEvent(new detail::FEvent<F>(std::forward<F>(fun)));
}

void Class::foo() {
  emit stateChange1();
  ...
  postToObject([this]{ mMember = true; }, this);
  // or
  postToStateMachine([this]{ mMember = true; });
}
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313