发出信号会导致核心转储

Emitting signals causes core dumps

作为 Python 和 Qt 的(相当)新手,我正在摆弄这段代码。 它可能有点混乱,但我已尝试尽可能减少代码并仍然获得核心转储。

基本上有一个启动按钮 "something" - 现在它只是一个 for 循环 - 进度条和标签。

点击界面中的按钮输出1到控制台,然后core-dumps。进度条和标签都没有数据。

我正在使用 Python 2.7.11、Qt-4.8.7 和 PySide 1.2.2。

线程代码来自这个 youtube 视频: https://www.youtube.com/watch?v=ivcxZSHL7jM

我试过将发射线放在循环外,甚至放在 MainDialog class 中,但似乎只要发射信号来自 MainDialog class 之外,它就会崩溃.它仅在 MainDialog 内部有效(使用静态整数进行测试,在进度条重置后进行测试)。

showGui.py - 这里没有问题 -(由设计师制作并由 pyside 转换):

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

# Form implementation generated from reading ui file 'show.ui'
#
# Created: Wed Jul 13 09:10:12 2016
#      by: pyside-uic 0.2.15 running on PySide 1.2.2
#
# WARNING! All changes made in this file will be lost!

from PySide import QtCore, QtGui

class Ui_mainDialog(object):
    def setupUi(self, mainDialog):
        mainDialog.setObjectName("mainDialog")
        mainDialog.resize(369, 171)
        self.pushButton = QtGui.QPushButton(mainDialog)
        self.pushButton.setGeometry(QtCore.QRect(50, 40, 84, 33))
        self.pushButton.setObjectName("pushButton")
        self.progressBar = QtGui.QProgressBar(mainDialog)
        self.progressBar.setGeometry(QtCore.QRect(50, 110, 231, 23))
        self.progressBar.setProperty("value", 0)
        self.progressBar.setObjectName("progressBar")
        self.label = QtGui.QLabel(mainDialog)
        self.label.setGeometry(QtCore.QRect(170, 40, 81, 31))
        self.label.setObjectName("label")

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

    def retranslateUi(self, mainDialog):
        mainDialog.setWindowTitle(QtGui.QApplication.translate("mainDialog", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setText(QtGui.QApplication.translate("mainDialog", "Button", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("mainDialog", "TextLabel", None, QtGui.QApplication.UnicodeUTF8))

test.py - 发射信号时失败:

from __future__ import print_function

import sys
import time
from PySide import QtCore, QtGui

import showGui
from PySide.QtCore import *
from PySide.QtGui import *


class MainDialog(QDialog, showGui.Ui_mainDialog):

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

        self.threadclass = ThreadClass()
        self.connect(self.threadclass, QtCore.SIGNAL('GFX_PROGRESS'), self.setProgress)
        self.connect(self.threadclass, QtCore.SIGNAL('TEXT_PROGRESS'), self.setTextLabel)
        self.connect(self.pushButton, SIGNAL("clicked()"), self.threadclass.doSomething)
        self.progressBar.reset()


    def setTextLabel(self, val):
        self.label.setText(val)

    def setProgress(self, val):
        self.progressBar.setValue(val)


class ThreadClass(QtCore.QThread):
    def __init__(self, parent=None):
        super(ThreadClass, self).__init__(parent)

    def doSomething(self):
        self.runcmd()
        # some more code here

    def runcmd(self):
        for i in range(1, 100):
            print("Status at : %s " % i)

            # this one crashes
            self.emit(QtCore.SIGNAL('TEXT_PROGRESS'), i)

            # this one crashes too
            self.emit(QtCore.SIGNAL('GFX_PROGRESS'), i)
            time.sleep(1)


app = QApplication(sys.argv)
form = MainDialog()
form.show()
app.exec_()

不要使用old-style signal and slot syntax。它很容易出错,如果你弄错了也不会抛出异常。除此之外,PySide 中的实现似乎有些问题。我将你的代码示例转换为 PyQt4,它不会转储核心。

要让您的示例在 PySide 中运行,您首先需要切换到新式信号和槽语法。此外,您当前的线程实现是错误的。它实际上并没有启动工作线程,因此所有代码都将 运行 在主线程中,并且会阻塞 gui。

以下修复应该使示例按预期工作:

class MainDialog(QDialog, Ui_mainDialog):
    def __init__(self, parent=None):
        ..
        # use new-style connections
        self.threadclass.gfxProgress.connect(self.setProgress)
        self.threadclass.textProgress.connect(self.setTextLabel)
        self.pushButton.clicked.connect(self.threadclass.doSomething)   


class ThreadClass(QtCore.QThread):
    # define new-style signals
    gfxProgress = QtCore.Signal(int)
    textProgress = QtCore.Signal(str)

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

    def doSomething(self):
        # start the thread
        # by default, this will automatically call run()
        self.start()

    def run(self):
        for i in range(1, 100):
            print("Status at : %s " % i)
            # emit the new-style signals
            self.gfxProgress.emit(i)
            self.textProgress.emit(str(i))
            time.sleep(1)