Waiting for QPushButton press



  • Hi,
    I'm doing my first project in QT and I have the problem. I know that there are many similar requests on the forum and SO, but no one answered my question.

    I would like to stop the program execution and wait for the click of the button. More or less the situation:

    @slot1() {

    for (int i=0; i<10; i++) {
    /*
    * some code
    */

    // STOP EXECUTION - WAITING FOR THE BUTTON PRESSED

    /*
    * the rest of code
    */
    }

    }@

    I know that this is contrary to event-driven programming, but the requirements of the project (task of course on studies) require that way. It is very important for me, so please - give possible solutions :)

    Waiting for your replies,
    Ux.



  • "LibQxt":http://libqxt.org/ has a class called "QxtSignalWaiter":http://libqxt.bitbucket.org/doc/tip/qxtsignalwaiter.html that is used exactly for this kind of purposes. The idea is to process the event loop on your own (by calling QCoreApplication::processEvents) in a loop until you find the event (signal in this case) you are looking for. You can just use that if you don't mind linking to it, or you can just implement your own version based on it. The source can be found inside libqxt/src/core/.

    There are other things to mind, though. First, blocking the main thread will freeze the GUI, and you obviously can't click a button if the GUI is frozen. So you'll need to run the waiting waiter in a background loop instead. Also, the event loop processing approach is not reentrant, so you'll need to be careful if you have multiple places in your code that waits for signals. Especially when you run waiters in multiple threads, which is what you might need to to here.



  • else you can declare the i variable private - for the class only and use "the rest of the code in a new push button" if is the same button then you implemented into the same button but with a variable of brake = true, then
    button->text() you change it to button->setText("continue");

    something like
    @
    void classname::on_button_clicked(){
    QString buttonName;
    buttonName = button->text();
    if (buttonName == "new") {
    for (int i=0; i<where to stop; i++) {
    /*
    * some code
    /
    }
    button->setText("Continue");
    } else {
    for (int i=where was stop; i<end or 10; i++) {
    /

    * the rest of code
    */
    }
    button->setText("Finish/or New"); // new for doing again
    }
    }
    @
    i just types in it is not tested but it give you the idea hope helped you



  • I believe there is more elegant solution for your task, but since i have no enough information, here is one of the solutions:
    @QEventLoop loop;
    connect(button, &QPushButton::pressed, &loop, &QEventLoop::quit);
    loop.exec();@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.