Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to update a flag value on qpuhbutton click event ?
Forum Updated to NodeBB v4.3 + New Features

How to update a flag value on qpuhbutton click event ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 1.7k Views 1 Watching
  • 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.
  • RajniR Offline
    RajniR Offline
    Rajni
    wrote on last edited by
    #1

    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
    0
    • jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      RajniR 1 Reply Last reply
      1
      • jsulmJ jsulm

        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.

        RajniR Offline
        RajniR Offline
        Rajni
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4
          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
          0
          • RajniR Offline
            RajniR Offline
            Rajni
            wrote on last edited by
            #5

            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
            0
            • jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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

              kshegunovK 1 Reply Last reply
              0
              • jsulmJ jsulm

                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.

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by kshegunov
                #7

                @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
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved