Qt - What is the correct way to pause execution of a program to wait for user input?

Langleson

I have a program that runs through a series of steps where the user selects points on a displayed picture. I am unsure of the proper way to pause execution while the user selects these points. Currently I am using something like this:

while(m_MouseCount < 2)
{
    qApp->processEvents(QEventLoop::WaitForMoreEvents);
    if(m_cancelFlag){
        // disconnect mousePressSignal
        return;
    }
}

The problem that I am running into is that when the window is closed during this loop, the program continues to run in the background.

It was brought to my attention that a Finite State Machine would be more appropriate to use in this case, and what I have done here is "horrible pseudo-synchronous" programming. But if I did use a FSM, I would still have the problem of waiting for the FSM to run its course before moving on to the next user selected measurement.

Is there a better way to wait for user input?

Edit

I understand that the Qt's UI is event driven and doesn't generally pause. My program works as follows:

  1. The user is shown a picture and presented with a few options via pushbuttons (Crop, Start Analysis, Cancel)
  2. User Selects Crop
  3. User is prompted to select a rectangle to crop picture (until rectangle is selected program is "paused" - cancel option still exists)
  4. User selects Full Analysis
  5. User is prompted to select measurement 1.
  6. Program "pauses" until two points are selected.
  7. User is prompted to select measurement 2.
  8. Program "pauses" until two points are selected.
  9. This repeats until all measurements have been selected.
  10. Calculations are done based on measurements.

I don't see anyway around some sort of pausing or waiting.

Fire Lancer

I am not entirely sure on what your end result is meant to be, but when working with UI's, you would normally never try to wait for anything in your own code block / loop. Let QT / the other framework handle how these loops need to work, which may or may not include blocking "modal" dialogues.

Instead you just wait for things (events) to happen. In this case you likely want to wait for QT to tell you that the user clicked on some coordinate in the image, then do whatever you want to do in response.

If you care about the user trying to close the window before completing the task, there will be an event for that as well.

Not real code.

//measurements may want to be a UI thing, such as a table or whatever
//is suitable for your use . You would then also have the ability to go
//back and make corrections, etc.
measurements.push_back(Measurement("Measurement Foo"));
measurements.push_back(Measurement("Measurement Bar"));
currentMeasurement = 0;
myImage->addMouseDownHandler(std::bind(&MyApp::onImageClickedOn, this));
...

void MyApp::startMeasurement()
{
    auto &measurement = measurements[currentMeasurement];
    delete fromMarker; fromMarker = nullptr;
    instructionCtrl->setText(
        "Please select two points to define " +
        measurement.getName());
}
void MyApp::onImageClickedOn(MouseEvent event)
{
    if (!fromMarker)
    {   //Create a marker where the from/start point is
        //Likely wants to be visual/ui object to aid user
        fromMarker = new FromSelectMarker(myImage, event.mousePos);
    }
    else
    {
        //Second point
        auto from = fromMarker.getPos();
        auto to = event.mousePos;
        auto &measurement = measurements[currentMeasurement];
        measurement.setPositions(from, to);

        ++currentMeasurement;
        if (currentMeasurement < measurements.size()
        {
            startMeasurement();
        }
        else
        {
            //finished, do next thing
        }
    }

}

If you really have a complex sequence of users going through a number of different finite "states" with some form of navigation, then maybe a FSM framework is what you want. Or if you don't have a simple one available, you can just tidy up the code to get something similar.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pause execution and wait for user input

From Dev

What is the simplest way to make your program wait for a signal in Qt?

From Dev

java Program does not wait for the user input

From Dev

Program doesnt wait to ask for user input

From Java

How to pause task running on a worker thread and wait for user input?

From Dev

Pause execution until user input received from tkinter window

From Dev

C# Pause Program Execution

From Dev

What is the correct way to pause AsyncTask from the UI-thread

From Dev

Python: wait for user input, and if no input after 10 minutes, continue with program

From Dev

pause for user correction to input

From Dev

What is the proper way to prevent a program from exiting after a user enters wrong input in Java?

From Dev

Implicit wait in WebDriver doesn't pause execution

From Dev

What is the correct way to wait until MongoDB is ready after restart?

From Dev

To pause and resume the program execution in the command prompt

From Dev

What is the best way to safely read user input?

From Dev

What is the proper way to validate user input?

From Dev

What is the correct way to configure Qt Creator to use Git?

From Dev

Qt: What is the correct and safe way to write the destructor of this class?

From Dev

What is the execution context of a program?

From Dev

TThread Wait for User Input

From Dev

Wait for user input

From Dev

Stop/pause execution if user iterate with form element

From Dev

In debug mode, is there a way to break(pause the execution) whereever it is?

From Dev

Best way to efficiently pause execution in c

From Dev

In debug mode, is there a way to break(pause the execution) whereever it is?

From Dev

At what point do breakpoints pause execution in Xcode?

From Dev

What's the correct way to write a form for Spring MVC with form:input

From Dev

what is the correct way to handle multiple input command differently in c++?

From Dev

What is the correct way of find out if user is logged in in MVC WEB API?

Related Related

  1. 1

    Pause execution and wait for user input

  2. 2

    What is the simplest way to make your program wait for a signal in Qt?

  3. 3

    java Program does not wait for the user input

  4. 4

    Program doesnt wait to ask for user input

  5. 5

    How to pause task running on a worker thread and wait for user input?

  6. 6

    Pause execution until user input received from tkinter window

  7. 7

    C# Pause Program Execution

  8. 8

    What is the correct way to pause AsyncTask from the UI-thread

  9. 9

    Python: wait for user input, and if no input after 10 minutes, continue with program

  10. 10

    pause for user correction to input

  11. 11

    What is the proper way to prevent a program from exiting after a user enters wrong input in Java?

  12. 12

    Implicit wait in WebDriver doesn't pause execution

  13. 13

    What is the correct way to wait until MongoDB is ready after restart?

  14. 14

    To pause and resume the program execution in the command prompt

  15. 15

    What is the best way to safely read user input?

  16. 16

    What is the proper way to validate user input?

  17. 17

    What is the correct way to configure Qt Creator to use Git?

  18. 18

    Qt: What is the correct and safe way to write the destructor of this class?

  19. 19

    What is the execution context of a program?

  20. 20

    TThread Wait for User Input

  21. 21

    Wait for user input

  22. 22

    Stop/pause execution if user iterate with form element

  23. 23

    In debug mode, is there a way to break(pause the execution) whereever it is?

  24. 24

    Best way to efficiently pause execution in c

  25. 25

    In debug mode, is there a way to break(pause the execution) whereever it is?

  26. 26

    At what point do breakpoints pause execution in Xcode?

  27. 27

    What's the correct way to write a form for Spring MVC with form:input

  28. 28

    what is the correct way to handle multiple input command differently in c++?

  29. 29

    What is the correct way of find out if user is logged in in MVC WEB API?

HotTag

Archive