Home > Enterprise >  Qt GUI C How to wait for a button click input inside a function
Qt GUI C How to wait for a button click input inside a function

Time:10-19

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;
}

CodePudding user response:

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.

CodePudding user response:

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.

  • Related