如何将信号连接到不同线程中的插槽

时间:2017-09-25 13:06:04

标签: python multithreading pyqt pyqt5 signals-slots

我正在使用Python 3.6和PyQt5。我在MainWindow课程中绘制了一个gui,其中包含QComboBox。我想将currentTextChanged信号发送到不同线程中的插槽。我对信号和插槽比较新。我该怎么做呢?更喜欢一个类Ui_MainWindow(object):的书面示例,将信号发送到类Threadclass2(QtCore.QThread): 这是我的代码的简化版本和我想要的:

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_())

在此之前,按钮将启动线程,而gui仍由mainwidnow类绘制

1 个答案:

答案 0 :(得分:1)

@eyllanesc解决方案有效。添加这个类是关键。干杯

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()