Donkyhotay
Donkyhotay

Reputation: 81

using qt with custom loop in python

I have been experimenting around with using QT in python3 and figured out how to make a simple .ui file and import it directly into a python program. However I have to use python QT app while I would like to have my own custom loop in order to be able to add things to it. So what I have now is:

import sys
from PyQt4 import QtCore, QtGui, uic

class MyWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.desktop = uic.loadUi('mytest.ui')
        self.desktop.show()

if __name__ == '__main__':
    app = QtGui.QApplication.instance()
    window = MyWindow()
    sys.exit(app.exec_())

I've played around a bit with PGU where I have been able to do this with what would be

while True:
    window.loop()

in the main which allows me to put in my own extra code that is unrelated to the GUI without dealing with multiple threads. However I have been unable find some equivalent to "loop" in qt and searching for "qt custom loop/update/blit python" hasn't found anything relevant besides suggestions to add a 0 second timer to my app and place extra code there which seems... inelegant to me. I would like to import a GUI into my app rather then building my app around a GUI.

Edit: Here is my updated code taking Phyatt's answer into account for anyone else looking for the same answer I was.

import sys
from PyQt4 import QtGui, QtCore, uic

class TestApp(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        self.ui = uic.loadUi('mytest.ui')
        self.ui.show()

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    win = TestApp()
    app.processEvents()
    while True:
        app.processEvents()

Upvotes: 1

Views: 408

Answers (1)

phyatt
phyatt

Reputation: 19102

Instead of using your own custom loop, the more common way to do that is to have a timer event scheduled on the Qt loop, or to launch another thread.

If you still really wanted to use your own loop, you need to manually call QApplication.processEvents() in your loop and it probably should work.

http://qt-project.org/doc/qt-4.8/qcoreapplication.html#processEvents

http://doc.qt.io/qt-5/qtimerevent.html#details

MyWindow.start(100) # start the built in timer event for that QObject 10 times a second

And then in MyWindow put def timerEvent and it should work.

Hope that helps.

Upvotes: 2

Related Questions