发射信号会导致核心转储

时间:2016-07-13 12:29:45

标签: python multithreading pyside signals-slots coredump

对Python和Qt来说都是(非常)新手我正在摆弄这段代码。 它可能有些混乱,但我试图尽可能地削减代码并仍然获得核心转储。

基本上有一个按钮可以启动"某些东西" - 现在它只是一个for循环 - 进度条和标签。

单击界面中的按钮将1输出到控制台,然后输出核心转储。进度条或标签中未显示任何数据。

我使用的是Python 2.7.11,Qt-4.8.7和PySide 1.2.2。

线程代码来自此youtube vid: https://www.youtube.com/watch?v=ivcxZSHL7jM

我尝试将发射线放在循环外部,甚至放入MainDialog类中,似乎只要发射信号来自MainDialog类之外,它就会崩溃。它只能在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_()

1 个答案:

答案 0 :(得分:1)

请勿使用old-style signal and slot syntax。它容易出错,如果你弄错了,也不会引发异常。除此之外,看起来在PySide中实现有些破坏。我将您的代码示例转换为PyQt4,并且它不会转储核心。

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

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

results_list = list()

for (i in 1:nrow(base_data)) {
  LCS1 <- expand.grid(STREET=base_data$STREET[1:nrow(base_data)],REF_STREET=base_data$STREET[i])
  LCS$LCS_stringdist=stringdist(LCS$STREET,LCS$Name1,method="lcs")
  results_list[[i]] <- head(LCS[order(LCS$LCS_stringdist),],20) 
}

Results = do.call(rbind, results_list)

## faster options in other packages
Results = dplyr::bind_rows(results_list)
Results = data.table::rbindlist(results_list)