Issue
I am trying to display a loading graphic(for now just a label) at the beginning of my pyside program while another function is running. After it's done it should continue and load the main GUI. I have this so far
from PySide import QtCore
from PySide import QtGui
class DoStuff:
def __init__(self):
pass
def ReturnInformation(self):
time.sleep(20) #Sleep to simulate processing time
return "information"
class Main(QtGui.QWidget):
def __init__(self):
super(Main, self).__init__()
self.initQ = queue.Queue()
self.initthread = threading.Thread(target=self.InitThread)
self.initthread.daemon = True
self.initthread.start()
self.setStyleSheet("background-color: black;")
self.setCursor(QtCore.Qt.BlankCursor)
self.loaddisplay = QtGui.QLabel(self)
self.loaddisplay.move(20, 20)
self.loaddisplay.setText("Loading...")
self.show()
self.initthread.join()
self.MainDisplay()
self.show()
def InitThread(self):
self.dostuff = DoStuff()
def MainDisplay(self):
self.display = QtGui.QLabel(self)
self.display.setStyleSheet("font: 70pt Helvetica; color: white;")
self.display.move(20, 20)
self.display.setText(self.dostuff.ReturnInformation())
self.manager = QtCore.QTimer(self)
self.manager.timeout.connect(self.Update)
self.manager.start(100000)
def Update(self): #Update the information once in a while
self.timedisplay.setText(self.dostuff.ReturnInformation())
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
GUI = Main()
sys.exit(app.exec_())
The problem is that only the load graphic is displayed and the GUI from MainDisplay()
is never displayed. I'm pretty sure this has something to do with how I'm calling the show()
function. Is that the problem, and how do I fix it?
Also, how would I delete the loading label once it has finished loading?
P.S. (I asked this question before, but it got no answers or comments and low views so I deleted it and am asking the question again)
Solution
Although python provides several ways to execute task through threads these do not necessarily conform to the rules of Qt, it is appropriate to use the tools of the framework such as QThread:
class DoStuffThread(QtCore.QThread):
displaySignal = QtCore.Signal(str)
timeSignal = QtCore.Signal(str)
def __init__(self, *args, **kwargs):
QtCore.QThread.__init__(self, *args, **kwargs)
self.timer = QtCore.QTimer()
self.timer.moveToThread(self)
self.timer.timeout.connect(self.onTimeout)
self.stuff = DoStuff()
def onTimeout(self):
data = self.stuff.ReturnInformation()
self.timeSignal.emit(data)
def run(self):
data = self.stuff.ReturnInformation()
self.displaySignal.emit(data)
self.timer.start(20000)
loop = QtCore.QEventLoop()
loop.exec_()
class DoStuff:
def ReturnInformation(self):
time.sleep(2) # Sleep to simulate processing time
return "information-{}".format(QtCore.QTime.currentTime().toString("hh:mm:ss"))
class Main(QtGui.QWidget):
def __init__(self):
super(Main, self).__init__()
self.setStyleSheet("background-color: black;")
self.setCursor(QtCore.Qt.BlankCursor)
self.setLayout(QtGui.QVBoxLayout())
self.loaddisplay = QtGui.QLabel(self)
self.display = QtGui.QLabel(self)
self.timedisplay = QtGui.QLabel(self)
self.layout().addWidget(self.loaddisplay)
self.layout().addWidget(self.display)
self.layout().addWidget(self.timedisplay)
self.thread = DoStuffThread(self)
self.thread.displaySignal.connect(self.display.setText, QtCore.Qt.QueuedConnection)
self.thread.timeSignal.connect(self.timedisplay.setText, QtCore.Qt.QueuedConnection)
self.thread.start()
self.loaddisplay.move(20, 20)
self.loaddisplay.setText("Loading...")
self.display.setStyleSheet("font: 70pt Helvetica; color: white;")
self.display.move(20, 20)
def closeEvent(self, event):
self.thread.quit()
QtGui.QWidget.closeEvent(self, event)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
GUI = Main()
GUI.show()
sys.exit(app.exec_())
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.