0

I was looking everywhere but I was unable to figure out answer to my problem. I need your help regarding QThread threading in PyQt5. For the purpose of learning I've created two classes in my main.py file. One of those classes is a GUI class and the other one is a thread created using QThread object.

from PyQt5 import QtCore, QtGui, QtWidgets
import threading_gui # contains GUI generated by pyuic5

class Change_values(QtCore.QThread):
    def __init__(self, parent = None, str_variable, int_variable):
        self.var1 = str_variable
        self.var2 = int_variable

    def run(self):
        self.var1 += ' testing thread communication'
        self.var2 += 4

class MainWindow(QtWidgets.QMainWindow, threading_gui.UI_MainWindow):

    change_signal = QtCore.pyqtSignal(str, int, name='change_signal')

    def __init__(self, parent = None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)

        self.btn_Run.clicked.connect(self.process_data)

        self.thread1 = Change_values('I am', 3)

    def process_data(self):
        self.thread1.start()

This is as far as I managed to do. Now I need someone to explain me how can I emit signal from thread Change_values and display the content of variables var1 and var2 in my GUI thread.

I was able to find examples for PyQt4 but nothing useful when it comes to PyQt5. Thank you all in advance.

Beller0ph0n
  • 105
  • 1
  • 2
  • 10
  • Could you link to the examples you looked at that didn't work? I didn't think there was significant differences between threading in PyQt4 and PyQt5 – three_pineapples Dec 28 '15 at 03:40
  • @ three_pineapples: emitting the signals is different and that's in fact the core of my problem. How to emit the signal from Change_values thread and how to read it inside MainWindow thread. Thanks. – Beller0ph0n Dec 28 '15 at 04:48
  • 1
    My understanding is that PyQt4 new-style signals/slots are the same as PyQt5 signals/slots (with some minor technical differences that aren't likely to matter). If you look at this SO answer [here](http://stackoverflow.com/a/21071865/1994235), you should be able to see how to do signal/slot connections with a thread. Does this look like the examples that weren't working for you? – three_pineapples Dec 28 '15 at 05:54
  • @three_pineapples: Jut to let you know that the link you've provided was not one of the examples that I had a chance to examine. With that being said I'm looking at it right now and I'll let you know if I had any more questions. Thanks a lot three_pineapples for all the help you've provided so far ;) – Beller0ph0n Dec 28 '15 at 07:31
  • You're great three_pineapples. Thanks to you I got it working. I've also managed to pass the arguments (QLineEdit text that user inputs) to my worker thread, process those arguments and display them in my QTextEdit inside MainWindow class. – Beller0ph0n Dec 28 '15 at 08:58
  • However, I have a few more questions for you. I know that they might sound stupid but since I'm a beginner and I'm learning I want to learn the right way. I hope you will find time and will to answer them and help me on my way of learning Python. – Beller0ph0n Dec 28 '15 at 09:06
  • My first question is about "Decorator". Are they necessary and what is their purpose? And my send question is about thread cleanup. Second time I click on my pushButton this message is displayed: "QThread: Destroyed while thread is still running" Could you please instruct me or direct me to some documentation where I can learn more on dealing with threads properly. Thank you once again, you've been more than helpful! Wish you a nice day, Tomislav. – Beller0ph0n Dec 28 '15 at 09:06

0 Answers0