PyQt - Zmodyfikuj GUI z innego wątku
Próbuję zmodyfikować mój główny układ z innego wątku. Ale funkcja run () nigdy nie jest wywoływana i mam błąd:
QObject :: setParent: Nie można ustawić rodzica, nowy rodzic znajduje się w innym wątku
Oto mój kod:
class FeedRetrievingThread(QtCore.QThread):
def __init__(self, parent=None):
super(FeedRetrievingThread, self).__init__(parent)
self.mainLayout = parent.mainLayout
def run(self):
# Do things with self.mainLayout
class MainWindow(QtGui.QDialog):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.mainLayout = QtGui.QGridLayout()
self.setLayout(self.mainLayout)
self.feedRetrievingThread = FeedRetrievingThread(self)
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.updateFeed)
self.timer.start(1000)
def updateFeed(self):
if not self.feedRetrievingThread.isRunning():
print 'Running thread.'
self.feedRetrievingThread.start()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
Naprawdę tego nie rozumiem, dlaczego tak trudno jest uzyskać dostęp do GUI z PyQt? W C # masz Invoke. Czy w PyQt jest coś takiego?
Próbowałem utworzyć wątek bezpośrednio zMainWindow.__init__
(bez użycia timera), ale też nie działa.