Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved How to update a flag value on qpuhbutton click event ?

    General and Desktop
    3
    7
    1161
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Rajni
      Rajni last edited by

      Hello All ,

      I want to call some function on some specific qpushbutton clicked and terminate it in between if some other button is clicked.
      I want to use flag value for that.
      Please help me...

      1 Reply Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion last edited by

        So, your function takes long to execute, right? Then you will only be able to stop it via UI if it does not block the event loop! You can for example call QCoreApplication::processEvents() periodically in your function. Then just implement a slot which sets the flag and connect it to the clicked() signal of another QPushButton.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        Rajni 1 Reply Last reply Reply Quote 1
        • Rajni
          Rajni @jsulm last edited by

          Thanks for your reply, but please can you give me a small example .

          1 Reply Last reply Reply Quote 0
          • jsulm
            jsulm Lifetime Qt Champion last edited by

            void MyClass::myLongLastingMethod()
            {
                flag = false;
                while(someCondition) {
                    // Do something
                    QCoreApplication::processEvents();
                    if (flag) break;
                }
            }
            
            void MyClass::onStopButtonClicked()
            {
                flag = true;
            }
            

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 0
            • Rajni
              Rajni last edited by

              Hi Jsulm , its not work in my case because i am using qWait various time in my code. so its block there .

              in place of wait what can i use for delay of various second after executing one function then call another.

              actually I want to execute some function sequentially after having delay of some seconds one by one.

              1 Reply Last reply Reply Quote 0
              • jsulm
                jsulm Lifetime Qt Champion last edited by

                As I said it will only work if you don't block Qt event loop.
                Blocking the event loop is usually a really bad idea and is not how Qt applications should be written.
                I still don't understand why you need to wait in a function? I would recommend to redesign your application to make use of asynchronous programming.
                If you really want to wait you can still use qWait but with smaller timeout in a loop and call QCoreApplication::processEvents() on each iteration.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                kshegunov 1 Reply Last reply Reply Quote 0
                • kshegunov
                  kshegunov Moderators @jsulm last edited by kshegunov

                  @jsulm

                  A local event loop is an option in this case as well (instead of calling processEvents).

                  @Rajni said:

                  its not work in my case because i am using qWait various time in my code. so its block there .

                  You shouldn't use that at all. qWait is for unit testing, not for production code, and to top that, as @jsulm said, you don't want to block the event loop or your GUI will simply freeze. Either run your lengthy operation in a loop and call QCoreApplication::processEvents regularly, or use a local event loop to block and schedule calls through it. For example:

                  class SomeClass : public QObject
                  {
                      Q_OBJECT
                  
                  public:
                      Q_INVOKABLE doSomething1()
                      {
                          // Do stuff
                          // ...
                          // Schedule the next batch of work
                          QMetaObject::invokeMethod(this, "doSomething2", Qt::QueuedConnection);
                      }
                  
                      Q_INVOKABLE doSomething2()
                      {
                          // Do more stuff
                          // ...
                          // Exit the blocking local event loop
                          emit finished();
                      }
                  
                  signals:
                      void finished();
                  }
                  

                  Used like this:

                  SomeClass doWorkObject;
                  
                  QEventLoop loop;
                  QObject::connect(&doWorkObject, &SomeClass::finished, &loop, &QEventLoop::quit);
                  
                  QMetaObject::invokeMethod(&doWorkObject, "doSomething1", Qt::QueuedConnection);
                  
                  loop.exec(); //< Blocks until QEventLoop::quit is called.
                  

                  How is best, however, really depends on exactly what you want to achieve.

                  Kind regards.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply Reply Quote 1
                  • First post
                    Last post