Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[SOLVED]Pressing a button doesn't stop running the code withing it's on_click definition.



  • Hey folks,

    I wanted to print with @qDebug()@ all objects names when pressing a button. Here is the code:
    @void MainWindow::on_pushButton_2_clicked()
    {
    for(int i=0; i<objects.size(); i++){
    qDebug() << objects[i]->getObjectName();
    }
    }
    @

    But doing like this it doesn't stop looping?

    Thanks in advance.

    [Solved]

    The problem was that an advance() function was running in the object, that I was printing the objects name there too.



  • Your problem is hard to understand... not many words, making not much sense. Nevertheless I try:

    As long as your "MainWindow::on_pushButton_2_clicked()" is running, your application cannot process more events, because the function is running in the context of the event-processing thread. So lets assume I understood your problem correctly, your second button cannot be used to "break" the execution of the for-loop.

    Best Regards,
    Tobias



  • Thanks for the reply!

    Excuse me for not explaining the problem in depth.

    Here is the expected output of pressing pushButton_2 if in the objects QVector i have 2 objects named: Object1 and Object2:
    @//Console output
    Object1
    Object2@
    But the output i get is
    @Object1
    Object2
    Object1
    Object2
    ...@

    About that you posted. Should't the function that is running in that event-processing thread stop when I release the button OR when the for loop is done looping through all objects in the QVector?



  • Hi,
    I am not sure where to begin. My feeling is, you have a problem with the concept of event-loop, I will get to that in a moment. To clarify: you are wondering, that your event-handler is executed often instead of once?

    Concept of Event-Loop. This is your program-entry (??):

    @
    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    MyMainWindow window;
    window.show()
    return app.exec();
    }
    @

    "app.exec" enter the "Qt-Mainloop". This mainloop does something like this:

    @
    // Event-Loop:
    while (1)
    {
    waitForEvent();
    processEvent();
    }
    @

    So you see -- while an event is being processed, another event cannot be processed, until processing is done and the Event-Loop is reentered.

    Assuming that you are using "mouse-clicked", exactly one event should be fired when you press and then release the mouse-button over your button. Or have you used "press" and "release" signals?

    Now the question is, why your event-handler is called more than once. So there have to be multiple events in the queue which trigger your function.

    From your code I cannot see, why this is. Have you connected multiple signals to that slot? Can you give more code? Can you set a breakpoint in your handler-function and use the call-stack to find out, which events are triggering it?

    Best Regards,
    Tobias


Log in to reply