0

I have two signals which are NOT necessarily received at the same time.

First signal

First signal is emitted by MyClass and is handled by a slot of MyClass:

QObject::connect(this, &MyClass::ready, 
                 this, &MyClass::handleReady);

void MyClass::handleReady()
{
    qDebug() << "Ready to do next step";
}

// ... somewhere in MyClass implementation, signal is emitted:
emit ready();

Second signal

Second signal is emitted by a member object and is handled by a slot of MyClass:

QObject::connect(m_member, &MemberClass::enabledChanged, 
                 this,     &MyClass::handleEnabledChange);
//...
void MyClass::handleEnabledChange(const bool enabled)
{
    qDebug() << "Is member enabled?" << enabled;
}

I intend to run some code (like a function), depending upon two condition:

  1. First signal, i.e. ready is received
  2. Second signal, i.e. enabledChanged, is received and its Boolean parameter is true

I cannot figure out how to check for satisfaction of above conditions.

UPDATE

Now my signals count is increased to three. So my code execution depends on three conditions.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Megidd
  • 7,089
  • 6
  • 65
  • 142

2 Answers2

2

Write a method in MyClass which checks if both conditions are true and call this methiod in both slots. Make the conditions member of myClass

bool MyClass::areBothConditionsTrue() {
    if(m_conditionA && m_conditionB)
        return true:
    return false;
}

void MyClass::handleReady()
{
    qDebug() << "Ready to do next step";
    m_conditionA = true;
    bool b = areBothConditionsTrue();
}

void MyClass::handleEnabledChange(const bool enabled)
{
    qDebug() << "Is member enabled?" << enabled;
    m_conditionB = enabled;
    bool b = areBothConditionsTrue();
}

The further code does depend on what you want to do with this information

RoQuOTriX
  • 2,871
  • 14
  • 25
2

I do not have reputation enough to comment RoQuOTriX answer so made a separate one.

In case your handlers count will increase, you can create the state object in MyClass which stores all conditions by adding extra level of abstraction:

void MyClass::handleReady(State& state)
{
    qDebug() << "Ready to do next step";
    state.m_conditionA.setFlag(true);
}

...but then you will need to implement the State class and State's conditions holder class inherited from QObject to emit signal flagChanged when setFlag is called. Then just connect(m_conditionA, &Condition::flagChanged, this, &State::checkAllConditions) and store all conditions in some associative container.

ilev4ik
  • 23
  • 4