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 make the program wait some time during the execution?
Forum Updated to NodeBB v4.3 + New Features

How to make the program wait some time during the execution?

Scheduled Pinned Locked Moved General and Desktop
21 Posts 6 Posters 33.1k 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.
  • R Offline
    R Offline
    raven-worx
    Moderators
    wrote on 27 Apr 2013, 14:30 last edited by
    #9

    the program continues after QEventLoop::exec() returns (which happens when the timer fires and calls quit() on the event loop).
    What you want to do afterwards depends on your application logic. E.g. you could run the event loop inside a programmatic loop.

    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
    If you have a question please use the forum so others can benefit from the solution in the future

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vale_shine
      wrote on 27 Apr 2013, 15:27 last edited by
      #10

      mmm... it doesn't work very well...
      for example:
      if I put
      loop = QEventLoop()
      QtCore.QTimer.singleShot(100000, loop, SLOT(quit()))
      loop.exec_()

      in my code it has to wait 100000 msec and than quit the application.. am I right??

      It doesn't act like this.. it just quit the application immediatly.. :(

      1 Reply Last reply
      0
      • R Offline
        R Offline
        raven-worx
        Moderators
        wrote on 27 Apr 2013, 15:41 last edited by
        #11

        show your code please ...
        btw. the correct slot in PyQt is exit (not quit)

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • V Offline
          V Offline
          vale_shine
          wrote on 27 Apr 2013, 15:51 last edited by
          #12

          @from PyQt4 import uic
          from PyQt4 import QtCore, QtGui
          import random
          from time import sleep
          import time
          from PyQt4.QtCore import *

          ( Ui_MainWindow, QMainWindow ) = uic.loadUiType( 'mainwindow.ui' )

          class MainWindow ( QMainWindow ):
          """MainWindow inherits QMainWindow"""

          global a
          a=random.randrange(1,10)
          
          def __init__ ( self, parent = None ):
              QMainWindow.__init__( self, parent )
              self.ui = Ui_MainWindow()
              self.ui.setupUi( self )
              
          
          def __del__ ( self ):
              self.ui = None
          
          
          def action2(self):
              self.ui.plainTextEdit_2.setPlainText('ab')
          
          def action1(self):
              self.ui.plainTextEdit.setPlainText('abr')
              loop = QEventLoop()
              QtCore.QTimer.singleShot(100000, loop, SLOT(exit()))
              loop.exec_()
          
          
          def action2(self):
              self.ui.plainTextEdit_2.setPlainText('ab')
          

          @
          it doesn't work also with exit() :(

          1 Reply Last reply
          0
          • R Offline
            R Offline
            raven-worx
            Moderators
            wrote on 27 Apr 2013, 16:05 last edited by
            #13

            where do you call action1?
            where do you start your QApplication event loop?

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • V Offline
              V Offline
              vale_shine
              wrote on 27 Apr 2013, 16:34 last edited by
              #14

              I use pyqt and it allows me to use the graphic interface to connect signals of the objects of a window and slots of the file I sent you (the functions definited by def)

              so I connected graphically the signal of a button with the function action one, it has to print in the first textplaceEditor 'abr' and after some second print 'ab' in the second plain text editor.
              It's just to try the "wait function"

              this is more clear:

              @from PyQt4 import uic
              from PyQt4 import QtCore, QtGui
              import random
              from time import sleep
              import time
              from PyQt4.QtCore import *

              ( Ui_MainWindow, QMainWindow ) = uic.loadUiType( 'mainwindow.ui' )

              class MainWindow ( QMainWindow ):
              """MainWindow inherits QMainWindow"""

              global a
              a=random.randrange(1,10)
              
              def __init__ ( self, parent = None ):
                  QMainWindow.__init__( self, parent )
                  self.ui = Ui_MainWindow()
                  self.ui.setupUi( self )
                  
              
              def __del__ ( self ):
                  self.ui = None
              
              
              def action2(self):
                  self.ui.plainTextEdit_2.setPlainText('ab')
              
              def action1(self):
                  self.ui.plainTextEdit.setPlainText('abr')
                  loop = QEventLoop()
                  QtCore.QTimer.singleShot(100000, loop, SLOT(exit()))
                  loop.exec_()
                  self.ui.plainTextEdit_2.setPlainText('ab')
              

              @
              sorry but i'm really a beginner and I can't programming well.
              thanks for your help anyway :)

              1 Reply Last reply
              0
              • R Offline
                R Offline
                raven-worx
                Moderators
                wrote on 27 Apr 2013, 17:40 last edited by
                #15

                but somehwere in your code you need to instantiate and start a qapplication .. without it it's clear why your program quits immediatly.
                For example:
                @
                app = QtGui.QApplication(sys.argv)

                widget = QtGui.QWidget()
                widget.resize(250, 150)
                widget.setWindowTitle('my app')
                widget.show()

                sys.exit(app.exec_())
                @

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  artem_pisarenko
                  wrote on 23 Oct 2013, 10:37 last edited by
                  #16

                  [quote author="raven-worx" date="1366993069"]
                  should do the trick ("block" for 1 sec.):
                  @
                  QEventLoop loop;
                  QTimer::singleShot(1000, &loop, SLOT(quit()));
                  loop.exec();
                  @
                  [/quote]

                  In most cases, code using this function expect that GUI will be blocked, but this trick breaks this behavior.
                  I'd recommend to modify like this
                  @
                  loop.exec(QEventLoop::ExcludeUserInputEvents | QEventLoop::ExcludeSocketNotifiers);
                  @
                  to achive real blocking behavior.

                  1 Reply Last reply
                  0
                  • N Offline
                    N Offline
                    NicuPopescu
                    wrote on 23 Oct 2013, 12:56 last edited by
                    #17

                    just for the record QTest::qSleep is blocking the execution ...

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 23 Oct 2013, 14:28 last edited by
                      #18

                      But it's only to be used in unit test

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • JKSHJ Offline
                        JKSHJ Offline
                        JKSH
                        Moderators
                        wrote on 23 Oct 2013, 14:36 last edited by
                        #19

                        @
                        QThread::sleep(1);
                        @
                        or
                        @
                        QThread::msleep(1000);
                        @

                        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                        1 Reply Last reply
                        0
                        • N Offline
                          N Offline
                          NicuPopescu
                          wrote on 23 Oct 2013, 15:32 last edited by
                          #20

                          bq. But it’s only to be used in unit test

                          on Windows using Sleep() in main app's thread is enough to block the execution:

                          @void QTest::qSleep(int ms)
                          {
                          QTEST_ASSERT(ms > 0);

                          #ifdef Q_OS_WIN
                          Sleep(uint(ms));
                          #else
                          struct timespec ts = { ms / 1000, (ms % 1000) * 1000 * 1000 };
                          nanosleep(&ts, NULL);
                          #endif
                          }@

                          and

                          @void QThread::msleep(unsigned long msecs)
                          {
                          ::Sleep(msecs);
                          }@

                          almost the same!

                          on Unix based systems I don't know if qt's thread_sleep does the same?

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 23 Oct 2013, 19:24 last edited by
                            #21

                            I didn't mean that the implementation they use is unit test specific. Just the module is

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0

                            • Login

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