Search code examples
pythonmultithreadingpyqtpyqt5signals-slots

How to connect a signal to a slot in a different thread


I am using Python 3.6 and PyQt5. I have a gui drawn in a MainWindow class which includes a QComboBox. I want to send the currentTextChangedsignal to a slot in a different thread. I am relatively new to signals and slots. How would I go about doing this? Would prefer a written example of a class Ui_MainWindow(object): sending the signal to class Threadclass2(QtCore.QThread): Here is a shortened version of my code and what I want:

from PyQt5 import QtCore, QtGui, QtWidgets
from selenium import webdriver
import time
import threading
from bs4 import BeautifulSoup as soup
import requests

class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            self.keyworddict = {}
            self.count = {}
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(698, 581)
            MainWindow.setMinimumSize(QtCore.QSize(698, 581))
            MainWindow.setMaximumSize(QtCore.QSize(698, 581))
            palette = QtGui.QPalette()
            brush = QtGui.QBrush(QtGui.QColor(154, 161, 161))
            brush.setStyle(QtCore.Qt.SolidPattern)
            palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Button, brush)
            brush = QtGui.QBrush(QtGui.QColor(206, 206, 206))
            brush.setStyle(QtCore.Qt.SolidPattern)
            palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush)
            brush = QtGui.QBrush(QtGui.QColor(214, 214, 214))
            brush.setStyle(QtCore.Qt.SolidPattern)
            palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush)
            brush = QtGui.QBrush(QtGui.QColor(154, 161, 161))
            brush.setStyle(QtCore.Qt.SolidPattern)
            palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Button, brush)
            brush = QtGui.QBrush(QtGui.QColor(206, 206, 206))
            brush.setStyle(QtCore.Qt.SolidPattern)
            palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush)
            brush = QtGui.QBrush(QtGui.QColor(214, 214, 214))
            brush.setStyle(QtCore.Qt.SolidPattern)
            palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush)
            brush = QtGui.QBrush(QtGui.QColor(154, 161, 161))
            brush.setStyle(QtCore.Qt.SolidPattern)
            palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Button, brush)
            brush = QtGui.QBrush(QtGui.QColor(214, 214, 214))
            brush.setStyle(QtCore.Qt.SolidPattern)
            palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush)
            brush = QtGui.QBrush(QtGui.QColor(214, 214, 214))
            brush.setStyle(QtCore.Qt.SolidPattern)
            palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush)
            MainWindow.setPalette(palette)
            self.centralWidget = QtWidgets.QWidget(MainWindow)
            self.centralWidget.setObjectName("centralWidget")
            self.comboBox = QtWidgets.QComboBox(self.centralWidget)
            self.comboBox.setGeometry(QtCore.QRect(20, 60, 371, 31))
            font = QtGui.QFont()
            font.setFamily("Yu Gothic")
            font.setPointSize(16)
            self.comboBox.setFont(font)
            self.comboBox.setAcceptDrops(False)
            self.comboBox.setObjectName("comboBox")
            self.comboBox.addItem("")
            self.comboBox.addItem("")
            MainWindow.setCentralWidget(self.centralWidget)
            self.retranslateUi(MainWindow)

        def retranslateUi(self, MainWindow):
            _translate = QtCore.QCoreApplication.translate
            MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
            self.comboBox.setItemText(0, _translate("MainWindow", "Jackets"))
            self.comboBox.setItemText(1, _translate("MainWindow", "Shirts"))
class Threadclass2(QtCore.QThread):
    def __init__(self, parent = None):
        super(Threadclass2, self).__init__(parent)

    def run(self):
        print("awd")

if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

Before this a button will start the thread whilst the gui is still being drawn by the mainwidnow class


Solution

  • @eyllanesc Solution worked. Adding this class is the key. Cheers

    class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        QtWidgets.QMainWindow.__init__(self, parent)
        self.setupUi(self)
    
        self.thread = Threadclass2(self)
        self.comboBox.currentTextChanged.connect(self.thread.setText)
        self.thread.start()