How to make the program wait some time during the execution?
-
Hi, I'm Valerio and i'm new of this kind of programming language.
I've written a little code so that it waits some second executing each line of code. I tried used the function time.sleep(5) but it waits for the entire sleep to finish the time.sleep(5) and then does all the actions of the rows of te code togheter. How do I rectify this??(and sorry for the english)
-
should do the trick ("block" for 1 sec.):
@
QEventLoop loop;
QTimer::singleShot(1000, &loop, SLOT(quit()));
loop.exec();
@ -
I'm sorry.. I forgot to write that I'm using python.
I used the code you gave me but it returns me a syntax error.. so I think you wrote it in c++ :) -
yep C++, but i think you can manage it to convert the 3 lines to python with the docs?!
-
Sorry for the delay...
I tried to translate it in python in that way:loop = QEventLoop()
QtCore.QTimer.singleShot(1000, loop, SLOT(quit()))
loop.exec()but it gives me the following error:
loop.exec()
^
SyntaxError: invalid syntaxIn what am I wrong? Do you think the rest is well written??
thx -
@loop.exec()
^
SyntaxError: invalid syntax@ -
i'm not very experienced with python, but it
seems that in python the method is called exec_() -
yeah... I was writing the same because I fixed the problem in that way too.
I was reading on the doc of QTimer.singleShot and I understood that it waits 1 sec and then do the action of the SLOT(). but what if I would like to execute the next row?
thx for your kindness -
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. -
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.. :(
-
show your code please ...
btw. the correct slot in PyQt is exit (not quit) -
@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() :( -
where do you call action1?
where do you start your QApplication event loop? -
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 :) -
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_())
@ -
[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. -
just for the record QTest::qSleep is blocking the execution ...
-
But it's only to be used in unit test
-
@
QThread::sleep(1);
@
or
@
QThread::msleep(1000);
@ -
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?