I have to create a cookie factory simulation program for an assignment. The program's GUI uses normal Qt widgets (such as: labels, buttons, and line edits) to control the simulation variables and objects. I already created the GUI, but I don't know how to do the simulation.
Our teacher suggested using threads (one per machine). I read on QThreads, but got the impression (from this link) that they are not really ideal for accessing objects from other threads, and I don't really know how to create or handle them.
However, the simulation is simple enough that (I believe) it could be done with only one loop (one thread), but I don't know how to create this loop in the QMainWindow class.
So, my main question is: How can I run a loop that can access the GUI, can be accessed by the GUI (so that it can change machines' values), and can access the machines? I just don't really know how to connect the GUI and the simulation.
Thanks, sorry for the long post.
Edit #1: Pseudo-code of what I am trying to do:
// Machines' initialization:
rawMaterialsTransport = new RawTransport();
doughMachine->conveyour = doughConveyour;
// Simulation loop:
lastTime = 0
while(running) {
// Handle pauses.
while(simulationPaused) {
sleep(100);
}
// Update machines/do the simulation
timePassed = now() - lastTime
lastTime = now()
rawMaterialsTransport->update(timePassed);
doughMachine->update(timePassed);
chocolateMachine->update(timePassed);
doughConveyour->update(timePassed);
// Update the GUI
chocolateGramsProcessedLabel->setText(to_string(chocolateMachine->gramsProcessed()));
// Sleep so as to not waste
sleep(100);
}
// On the GUI side:
onLineEditEnter() {
doughMachine->gramsPerSecond = double(lineEdit->text);
}
onPauseButtonPress() {
simulationPaused = !simulationPaused;
}