Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The state machine you used above does not correspond to what you described. Using a final state is not correct because after enter a value greater zero I don't see anything that prevents the user from enter zero again. Therefore the valid states can't be final. As far as I can see from your code the user is allowed to change the state of the widgets in any order. Your state machine has to pay attention to this.</p> <p>I would use a state machine with four child states (no valid input, one valid input, two valid inputs, three valid inputs). You obviously start with no valid input. Each widget can make a transition from no to one an back (same counts for two and three). When three is entered all widgets are valid (button enabled). For all other states the button has to be disabled when the state is entered.</p> <p>I wrote a sample app. The main window contains two QCheckBoxes a QSpinBox and a QPushButton. There are signals in the main window the ease write down the transitions of the states. There are fired when the state of the widgets are changed.</p> <p>MainWindow.h</p> <pre><code>#ifndef MAINWINDOW_H #define MAINWINDOW_H #include &lt;QtGui&gt; namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = 0); ~MainWindow(); private: Ui::MainWindow *ui; bool m_editValid; bool isEditValid() const; void setEditValid(bool value); private slots: void on_checkBox1_stateChanged(int state); void on_checkBox2_stateChanged(int state); void on_spinBox_valueChanged (int i); signals: void checkBox1Checked(); void checkBox1Unchecked(); void checkBox2Checked(); void checkBox2Unchecked(); void editValid(); void editInvalid(); }; #endif // MAINWINDOW_H </code></pre> <p>MainWindow.cpp</p> <pre><code>#include "MainWindow.h" #include "ui_MainWindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow), m_editValid(false) { ui-&gt;setupUi(this); QStateMachine* stateMachine = new QStateMachine(this); QState* noneValid = new QState(stateMachine); QState* oneValid = new QState(stateMachine); QState* twoValid = new QState(stateMachine); QState* threeValid = new QState(stateMachine); noneValid-&gt;addTransition(this, SIGNAL(checkBox1Checked()), oneValid); oneValid-&gt;addTransition(this, SIGNAL(checkBox1Checked()), twoValid); twoValid-&gt;addTransition(this, SIGNAL(checkBox1Checked()), threeValid); threeValid-&gt;addTransition(this, SIGNAL(checkBox1Unchecked()), twoValid); twoValid-&gt;addTransition(this, SIGNAL(checkBox1Unchecked()), oneValid); oneValid-&gt;addTransition(this, SIGNAL(checkBox1Unchecked()), noneValid); noneValid-&gt;addTransition(this, SIGNAL(checkBox2Checked()), oneValid); oneValid-&gt;addTransition(this, SIGNAL(checkBox2Checked()), twoValid); twoValid-&gt;addTransition(this, SIGNAL(checkBox2Checked()), threeValid); threeValid-&gt;addTransition(this, SIGNAL(checkBox2Unchecked()), twoValid); twoValid-&gt;addTransition(this, SIGNAL(checkBox2Unchecked()), oneValid); oneValid-&gt;addTransition(this, SIGNAL(checkBox2Unchecked()), noneValid); noneValid-&gt;addTransition(this, SIGNAL(editValid()), oneValid); oneValid-&gt;addTransition(this, SIGNAL(editValid()), twoValid); twoValid-&gt;addTransition(this, SIGNAL(editValid()), threeValid); threeValid-&gt;addTransition(this, SIGNAL(editInvalid()), twoValid); twoValid-&gt;addTransition(this, SIGNAL(editInvalid()), oneValid); oneValid-&gt;addTransition(this, SIGNAL(editInvalid()), noneValid); threeValid-&gt;assignProperty(ui-&gt;pushButton, "enabled", true); twoValid-&gt;assignProperty(ui-&gt;pushButton, "enabled", false); oneValid-&gt;assignProperty(ui-&gt;pushButton, "enabled", false); noneValid-&gt;assignProperty(ui-&gt;pushButton, "enabled", false); stateMachine-&gt;setInitialState(noneValid); stateMachine-&gt;start(); } MainWindow::~MainWindow() { delete ui; } bool MainWindow::isEditValid() const { return m_editValid; } void MainWindow::setEditValid(bool value) { if (value == m_editValid) { return; } m_editValid = value; if (value) { emit editValid(); } else { emit editInvalid(); } } void MainWindow::on_checkBox1_stateChanged(int state) { if (state == Qt::Checked) { emit checkBox1Checked(); } else { emit checkBox1Unchecked(); } } void MainWindow::on_checkBox2_stateChanged(int state) { if (state == Qt::Checked) { emit checkBox2Checked(); } else { emit checkBox2Unchecked(); } } void MainWindow::on_spinBox_valueChanged (int i) { setEditValid(i &gt; 0); } </code></pre> <p>This should do the trick. As you yourself mentioned already there are better ways to achive this behaviour. Especially keep track of all transistions between the state is error prone.</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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