4

So I have created a GUI using QT Designer. It works pretty well, but on more complex calls it doesn't update the main window and locks up. I want to run my CustomComplexFunction() while updating a textEdit in the main window from constantly changing backend information, and I wanted it to run every 2 seconds. The following code seems right and runs without errors, but doesn't update the textEdit. Please note i'm importing a .ui file designed from QT Designer with a pushButton and textEdit and the code won't run without it.

Main.py

import sys
from PyQt5.QtWidgets import QDialog, QApplication, QPushButton, QVBoxLayout, QMainWindow
from PyQt5.QtCore import QCoreApplication, QObject, QRunnable, QThread, QThreadPool, pyqtSignal, pyqtSlot
from PyQt5 import uic, QtGui

class Worker(QObject):
    newParams = pyqtSignal()

    @pyqtSlot()
    def paramUp(self):
        x=1
        for x in range(100):
            time.sleep(2)
            self.newParams.emit()
            print ("sent new params")
            x +=1

Ui_somewindow, _ = uic.loadUiType("mainwindow.ui") #the path to UI

class SomeWindow(QMainWindow, Ui_somewindow, QDialog):

    def __init__(self):

        QMainWindow.__init__(self)
        Ui_somewindow.__init__(self)
        self.setupUi(self)

        # Start custom functions
        self.params = {}
        self.pushButton.clicked.connect(self.complex) #NumEvent

    def complex(self):
        self.work = Worker() 
        self.thread = QThread()

        self.work.newParams.connect(self.changeTextEdit)
        self.work.moveToThread(self.thread)
        self.thread.start()

        self.CustomComplexFunction()

    def CustomComplexFunction(self):
        self.params['City'] = 'Test'

    def changeTextEdit(self):

        try: 
            City = self.params['City']
            self.textEdit_14.setPlainText(City) 
        except KeyError:
            City = None
if __name__ == "__main__":

    app = QApplication(sys.argv)
    window = SomeWindow()
    window.show()
    sys.exit(app.exec_())

You can see the official docs for Signals and Slots here and this SO post was also very helpful, but it seems like I built it correctly. According to the docs, the emitter doesn't care if the signal is used. This might be why the code doesn't have errors but doesn't work either.

Any ideas on how to make it work? Or atleast some way to test the emitter and signals??

RknRobin
  • 391
  • 2
  • 6
  • 21
  • Aren't you always setting the same text, i.e. `self.params['City']`? Is `changeTextEdit` called every two seconds? – m7913d Jun 18 '17 at 19:52
  • Yea probably, but it's unable to run so I haven't noticed that yet. – RknRobin Jun 18 '17 at 22:52
  • Actually no, `CustomComplexFunction()` is in the background changing `self.params['CIty']` every 7 seconds or so. I left this out for the example for simplicity reasons, but `self.params` is what I want to monitor from the main window. – RknRobin Jun 18 '17 at 22:59
  • 1
    Please provide an actual [mcve]. Your current example is incomplete so it's impossible to know where the issue is. – three_pineapples Jun 18 '17 at 23:01
  • My example is not incomplete and is the minimal verifiable example of what my issue is. The complexities of how `CustomComplexFunction` work are not important and can be simplified to a single string as @m7913d correctly pointed out. The function works great until it becomes a thread and I don't understand why. – RknRobin Jun 18 '17 at 23:13
  • 1
    You never call `paramUp`. Note that your example is not complete, we are not able to run it. It would make sense to replace City with a random/counter value. – m7913d Jun 19 '17 at 08:35
  • I double checked and I was incorrect and fixed the code to make it runnable. Please note I import a .ui file created in QT Designer with a pushButton and textEdit. – RknRobin Jun 19 '17 at 18:07
  • Where do I call `paramUp`? I thought I referenced it somehow by connecting to `newParams` signal and starting. – RknRobin Jun 19 '17 at 18:11

1 Answers1

4

You have forgot to connect the thread to the worker object.

self.work = Worker()
self.thread = QThread()
self.thread.started.connect(self.worker.work) # <--new line, make sure work starts.
self.thread.start()

Good luck with the application :-)

Ole
  • 106
  • 5