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;
}
starball
  • 20,030
  • 7
  • 43
  • 238
Ali
  • 55
  • 6
  • 2
    Does this answer your question? [Wait until button pressed(QT)](https://stackoverflow.com/questions/17858847/wait-until-button-pressedqt) – Karen Baghdasaryan Oct 19 '22 at 02: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. – Stephen Quan Oct 19 '22 at 04:59

2 Answers2

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.

Botje
  • 26,269
  • 3
  • 31
  • 41
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.

Kupofty
  • 45
  • 8
  • 1
    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. – Jesper Juhl Oct 19 '22 at 11:25