【问题标题】:PyQt5 QThread Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)PyQt5 QThread 进程以退出代码 139 完成(被信号 11 中断:SIGSEGV)
【发布时间】:2021-09-12 06:56:22
【问题描述】:

我有一个带有 PyQt5 的 GUI。 GUI 有一个表单、2 个按钮和一个进度条。 我想用 QThread 实现一个启动/等待线程。 不幸的是,当我单击“开始”按钮运行线程时,我以退出代码 139(被信号 11:SIGSEGV 中断)完成了进程。

MainWindow.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'MainWindow.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 138)
        self.btnRun = QtWidgets.QPushButton(Form)
        self.btnRun.setGeometry(QtCore.QRect(20, 20, 89, 25))
        self.btnRun.setObjectName("btnRun")
        self.btnStop = QtWidgets.QPushButton(Form)
        self.btnStop.setGeometry(QtCore.QRect(20, 60, 89, 25))
        self.btnStop.setObjectName("btnStop")
        self.progressBar = QtWidgets.QProgressBar(Form)
        self.progressBar.setGeometry(QtCore.QRect(20, 100, 371, 23))
        self.progressBar.setProperty("value", 24)
        self.progressBar.setObjectName("progressBar")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Test Thread"))
        self.btnRun.setText(_translate("Form", "Run"))
        self.btnStop.setText(_translate("Form", "Stop"))

当我单击“开始”按钮时,应用程序因分段错误而崩溃。

线程.py

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from MainWindow import *
import sys
import time

class MainWindow(QWidget, QThread):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        self.center()
        #Init progressBar
        self.ui.progressBar.setValue(0)
        #Buttons
        self.ui.btnRun.clicked.connect(self.start)
        self.ui.btnStop.clicked.connect(self.wait)

    def run(self):
        count = 0
        while count <= 100:
            count += 1
            self.ui.progressBar.setValue(count)
            time.sleep(1)

    def center(self):
        # geometry of the main window
        qr = self.frameGeometry()

        # center point of screen
        cp = QDesktopWidget().availableGeometry().center()

        # move rectangle's center point to screen's center point
        qr.moveCenter(cp)

        # top left of rectangle becomes top left of window centering it
        self.move(qr.topLeft())



if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setAttribute(Qt.AA_DontShowIconsInMenus, False)
    w = MainWindow()
    #   Disable maximize window button
    w.setWindowFlags(Qt.WindowCloseButtonHint | Qt.WindowMinimizeButtonHint)
    w.show()
    sys.exit(app.exec_())

【问题讨论】:

  • 1.不要尝试使用 qt 小部件和线程进行多重继承,那不会有任何好处;使用两个分开的类。 2. 禁止从外部线程访问 UI 元素,而应使用信号/插槽。对该主题进行一些研究,因为实际上有数百篇关于它的帖子。

标签: python pyqt5 qthread


【解决方案1】:

问题是不能同时继承QWidget和QThread,因为:

  • 无法从 2 个 QObject 继承,存在行为冲突。
  • PyQt 限制多重继承。

解决方案是使用组合而不是多重继承。

class Thread(QThread):
    progressChanged = pyqtSignal(int)

    def run(self):
        count = 0
        while count <= 100:
            count += 1
            self.progressChanged.emit(count)
            time.sleep(1)
class MainWindow(QWidget):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        self.center()
        self._custom_thread = Thread()
        #Init progressBar
        self.ui.progressBar.setValue(0)
        #Buttons
        self.ui.btnRun.clicked.connect(self._custom_thread.start)
        self.ui.btnStop.clicked.connect(self._custom_thread.wait)

        self._custom_thread.progressChanged.connect(self.ui.progressBar.setValue)

【讨论】:

  • 谢谢。我自己也找到了答案,我实施了一个与您类似的解决方案。但是你的比我的好,所以我保留它!再次感谢。
猜你喜欢
  • 2020-07-02
  • 2020-12-05
  • 1970-01-01
  • 2018-08-31
  • 2018-05-03
  • 2021-04-02
  • 2019-11-25
  • 2022-01-26
相关资源
最近更新 更多