How to make the program wait some time during the execution?
-
@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?
-
I didn't mean that the implementation they use is unit test specific. Just the module is