Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get this Qt state machine to work?
    primarykey
    data
    text
    <p>I have two widgets that can be checked, and a numeric entry field that should contain a value greater than zero. Whenever both widgets have been checked, and the numeric entry field contains a value greater than zero, a button should be enabled. I am struggling with defining a proper state machine for this situation. So far I have the following:</p> <pre><code>QStateMachine *machine = new QStateMachine(this); QState *buttonDisabled = new QState(QState::ParallelStates); buttonDisabled-&gt;assignProperty(ui_-&gt;button, "enabled", false); QState *a = new QState(buttonDisabled); QState *aUnchecked = new QState(a); QFinalState *aChecked = new QFinalState(a); aUnchecked-&gt;addTransition(wa, SIGNAL(checked()), aChecked); a-&gt;setInitialState(aUnchecked); QState *b = new QState(buttonDisabled); QState *bUnchecked = new QState(b); QFinalState *bChecked = new QFinalState(b); employeeUnchecked-&gt;addTransition(wb, SIGNAL(checked()), bChecked); b-&gt;setInitialState(bUnchecked); QState *weight = new QState(buttonDisabled); QState *weightZero = new QState(weight); QFinalState *weightGreaterThanZero = new QFinalState(weight); weightZero-&gt;addTransition(this, SIGNAL(validWeight()), weightGreaterThanZero); weight-&gt;setInitialState(weightZero); QState *buttonEnabled = new QState(); buttonEnabled-&gt;assignProperty(ui_-&gt;registerButton, "enabled", true); buttonDisabled-&gt;addTransition(buttonDisabled, SIGNAL(finished()), buttonEnabled); buttonEnabled-&gt;addTransition(this, SIGNAL(invalidWeight()), weightZero); machine-&gt;addState(registerButtonDisabled); machine-&gt;addState(registerButtonEnabled); machine-&gt;setInitialState(registerButtonDisabled); machine-&gt;start(); </code></pre> <p>The problem here is that the following transition:</p> <pre><code>buttonEnabled-&gt;addTransition(this, SIGNAL(invalidWeight()), weightZero); </code></pre> <p>causes all the child states in the <code>registerButtonDisabled</code> state to be reverted to their initial state. This is unwanted behaviour, as I want the <code>a</code> and <code>b</code> states to remain in the same state.</p> <p>How do I ensure that <code>a</code> and <code>b</code> remain in the same state? Is there another / better way this problem can be solved using state machines?</p> <hr> <p><strong>Note</strong>. There are a countless (arguably better) ways to solve this problem. However, I am only interested in a solution that uses a state machine. I think such a simple use case should be solvable using a simple state machine, right?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload