0

So I am programming a Poker game im C++ and first it was done with terminal only where I would get the input from cin (check or fold or ...) and program reacts based on the input in a while(true) loop.

Now I want to do the same in Qt with GUI where after clicking 'start' the game is created and then in a while(true) loop we are waiting for a signal of fold or check or raise or call button and we react based on that. How can I implement this? Basically:

while (true) {
    If (signal== button-fold) // do a;
    else if (signal == button-check ) // do b;
}
2
  • 2
    Does this answer your question? Wait until button pressed(QT) Commented Oct 19, 2022 at 2:57
  • Since we're talking qt here. I would do what you say in QML and Javascript and make use of Promises. Promises give us the ability to put our thoughts in a sequential manner, even though the actors behind each bit are actually asynchronous. i.e. we don't know when the user will click, but, when the user does click, we can move to the next step in our Promise chain. Commented Oct 19, 2022 at 4:59

2 Answers 2

3

What you are trying to do is the complete opposite of the Qt paradigm.

Instead of spinning in a while(true) loop, you basically register signal handlers for the button presses and then let the Qt event loop call your handlers when the button is pressed.

I suppose you are asking this question because you do not want to change your existing program too much, but the paradigm really is fundamentally different. The one thing you could try is isolating your game logic into a different thread and interacting with it via a pair of queues. Your game logic would block, waiting on commands from the command queue and send out data packages on the data queue. Your UI can then send commands to the command queue on a button press and read any UI actions from the data queue.

Sign up to request clarification or add additional context in comments.

Comments

1

The way I do it is by creating a "Push Button" on the UI. Then you right-click on the button and click on "go to slot" to select the function/signal you want (clicked, pressed, toggled, etc) and use a "emit" signal :

void GUI::on_pushButton_function1_clicked()
{
    emit function1_asked();
}

Then you connect the signal to the function you want to run :

connect(GUI,SIGNAL(function1_asked()),this,SLOT(function2()));

You will find more information on the official qt website : https://doc.qt.io/qt-6/signalsandslots.html or on youtube tutorials.

1 Comment

Please don't use the SIGNAL() and SLOT() macros these days. Qt has type safe compile time checked signal/slot connections - use those, not the slow, unsafe, run time checked crap we had to use in the old days.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.