发射信号导致堆芯倾倒

2024-05-05 00:32:35 发布

您现在位置:Python中文网/ 问答频道 /正文

作为Python和Qt的新手,我正在处理这段代码。 这可能有点混乱,但我已经尝试削减代码尽可能多,仍然得到核心转储。你知道吗

基本上有一个按钮开始“某物”-现在它只是一个for循环- 进度条和标签。你知道吗

单击界面中的按钮将1输出到控制台,然后核心转储。在进度条或标签中都看不到数据。你知道吗

我使用的是python2.7.11、Qt-4.8.7和PySide 1.2.2。你知道吗

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

我试过把发射线放在循环之外,甚至放在MainDialog类中,似乎只要发射信号来自MainDialog类之外,它就会崩溃。它只在MainDialog中工作(使用静态整数进行测试,在progressbar重置后进行测试)。你知道吗

你知道吗显示gui.py-这里没什么问题-(用designer制作,用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))

你知道吗测试.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_()

Tags: fromimportselfnonesignaldeflabelpyside
1条回答
网友
1楼 · 发布于 2024-05-05 00:32:35

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

要使示例在PySide中工作,首先需要切换到新样式的signal和slot语法。而且,您当前的线程实现是错误的。它实际上并不启动工作线程,因此所有代码都将在主线程中运行,并且会阻塞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)

相关问题 更多 >