PDA

View Full Version : Asynchronous loop with QStateMachine



QTie
31st May 2011, 08:35
Hi all,

I am new guy using a Qt. I am developing a HW testing SW (GUI) with Qt.
I need to develop a statemachine which has four states: stopped, running and paused.
On the mainwindow there are eight widgets which represents the components to be tested. User can enter test parameters for each of the eight component.
The main problem is that when statemachine goes to running state I need to loop in the switch case method (running state == switch case method) I have one case for each of the HW component to be tested.
Test procedure:
Start state-> (button pressed) -> Test component1->break from the switch case->loop back to switch case->test component2->break and so on until component8 is tested. When user presses Stop button statemachine goes to stopped state. I can manage all this by creating a simple switch case structure but the problem is that the loop is synchronous and user actions on the GUI are disabled (User can not stop the loop by pressing Stop button). So How to develop a asynchronous loop with QStateMachine? Thanks for a help!

joyer83
31st May 2011, 11:04
I think you should think this a bit differently.
Do those component tests block the execution when they are run? If they do then you should do something to turn each of those tests into asynchronous calls instead of synchronous, so that they will not block main thread and freeze GUI.
like:


void StateMachine::doTests() {
...
case TestComponent1:
testComponent1->start(); // returns immediately, doesn't wait for the execution to start
...
}

void StateMachine::onTestComponent1Finished() { // this slot is connected to testComponent1's signal finished()
if( false == isStopped )
{
doTests(); // execute next test
}
}


When you click the stop-button in GUI you can just set the isStopped to true and the execution should stop when currently running test has finished.

moe
31st May 2011, 11:54
You can put in a call to


QEventLoop::ProcessEvents(...)

QTie
1st June 2011, 07:43
Calling qApp->ProcessEvents(); did the trick. Thanks.