我试图创建函数,它将8个值传递给使用QtDesigner制作的窗口应用程序中的进度条。 也就是说有一个实际产生这个值的函数。我想每秒执行一次以获取theese值并更新显示值的进度条。
我结合了有关使用Python制作图形应用程序的教程,并制作了一个动态更新progess栏的应用程序:
问题是值正确传递,但是当发送信号时,整个应用程序崩溃并显示消息:
处理以退出代码139结束(由信号11中断: SIGSEGV)
这是我的代码:
生成值的模块(probkowanie.py):
import time
from random import randint
import threading
def getSignals():
time.sleep(1)
signals = (randint(0, 100), randint(0, 100), randint(0, 100), randint(0, 100), randint(0, 100), randint(0, 100), randint(0, 100), randint(0, 100), )
return signals
,这是主程序代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PySide.QtCore import *
from PySide.QtGui import *
from PyQt4 import QtCore
import sys
import time
import signalPreview # window library
import probkowanie # values generating library
class MainDialog(QDialog, signalPreview.Ui_mainDialog):
def __init__(self, parent=None):
super(MainDialog, self).__init__(parent)
self.setupUi(self)
self.threadclass = ThreadClass()
self.threadclass.start()
self.connect(self.threadclass, QtCore.SIGNAL('MYO_SIGNALS'), self.updateProgressBars)
def updateProgressBars(self, signals):
self.progressBar.setValue(signals[0])
class ThreadClass(QtCore.QThread):
def __init__(self, parent = None):
super(ThreadClass, self).__init__(parent)
def run(self):
while 1:
signals = probkowanie.getSignals()
self.emit(QtCore.SIGNAL('MYO_SIGNALS'), 5) # this line causes crash
print signals # it works correctly
app = QApplication(sys.argv)
form = MainDialog()
form.show()
app.exec_()
我认为这可能是由于混合库造成的。我使用PySide的QtCore,即使教程2是基于PyQt4。我的决定是因为1号教程是基于PySide的。 我尝试过改变:
from PySide import QtCore
到
from PyQt4 import QtCore
然后我又收到了一堆错误,我不知道该怎么办:
Traceback (most recent call last):
File "/usr/lib/pycharm/helpers/pydev/pydevd.py", line 1580, in <module>
globals = debugger.run(setup['file'], None, None, is_module)
File "/usr/lib/pycharm/helpers/pydev/pydevd.py", line 964, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "/mnt/Grubas/Projekty/Biomed/MYO/myo.py", line 36, in <module>
form = MainDialog()
File "/mnt/Grubas/Projekty/Biomed/MYO/myo.py", line 20, in __init__
self.connect(self.threadclass, QtCore.SIGNAL('MYO_SIGNALS'), self.updateProgressBars)
TypeError: 'PySide.QtCore.QObject.connect' called with wrong argument types:
PySide.QtCore.QObject.connect(ThreadClass, str, instancemethod)
Supported signatures:
PySide.QtCore.QObject.connect(PySide.QtCore.QObject, str, callable, PySide.QtCore.Qt.ConnectionType = Qt.AutoConnection)
PySide.QtCore.QObject.connect(PySide.QtCore.QObject, PySide.QtCore.QMetaMethod, PySide.QtCore.QObject, PySide.QtCore.QMetaMethod, PySide.QtCore.Qt.ConnectionType = Qt.AutoConnection)
PySide.QtCore.QObject.connect(PySide.QtCore.QObject, str, PySide.QtCore.QObject, str, PySide.QtCore.Qt.ConnectionType = Qt.AutoConnection)
PySide.QtCore.QObject.connect(PySide.QtCore.QObject, str, str, PySide.QtCore.Qt.ConnectionType = Qt.AutoConnection) PySide.QtCore.QObject.connect(str, callable, PySide.QtCore.Qt.ConnectionType = Qt.AutoConnection)
PySide.QtCore.QObject.connect(str, PySide.QtCore.QObject, str, PySide.QtCore.Qt.ConnectionType = Qt.AutoConnection)
@EDIT 在提供由@ekhumoro脚本提出的更改后,doens崩溃了,但现在我又收到了另一个错误:
QObject :: connect:无法对类型&#39; object&#39;的参数进行排队(确保 &#39;对象&#39;使用qRegisterMetaType()注册。)
我试图自己寻找解决方案,但我找不到我需要的确切代码。我也试过将信号类型从(对象)转换为(元组)或(列表),但它会导致另一个错误:
TypeError:用于调用元函数的未知类型(可能是 信号):元组
我发现的大多数解决方案都基于PyQT。是否有一种简单的方法可以为PySide重写它? 这是一个解决方案的例子,它似乎是正确的,但使用PyQT: https://stackoverflow.com/a/2595607/2550466
答案 0 :(得分:2)
你认为混合PySide和PyQt会导致问题是正确的,所以你需要删除其中一个。但是,崩溃本身可能是由PySide中的一个错误引起的。这里有一个类似问题的SO问题:
因此,您还需要更改定义和发出MYO_SIGNALS
的方式。
以下是您脚本的固定版本(已更改的行已注释):
# from PySide.QtCore import *
# from PySide.QtGui import *
# from PyQt4 import QtCore
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
import time
import signalPreview # window library
import probkowanie # values generating library
class MainDialog(QDialog):
def __init__(self, parent=None):
super(MainDialog, self).__init__(parent)
self.setupUi(self)
self.threadclass = ThreadClass()
self.threadclass.start()
# self.connect(self.threadclass, QtCore.SIGNAL('MYO_SIGNALS'), self.updateProgressBars)
self.connect(self.threadclass, SIGNAL('MYO_SIGNALS'), self.updateProgressBars)
def updateProgressBars(self, signals):
self.progressBar.setValue(signals[0])
# class ThreadClass(QtCore.QThread):
class ThreadClass(QThread):
def __init__(self, parent = None):
super(ThreadClass, self).__init__(parent)
def run(self):
while 1:
signals = probkowanie.getSignals()
# self.emit(QtCore.SIGNAL('MYO_SIGNALS'), 5) # this line causes crash
self.emit(SIGNAL('MYO_SIGNALS'), signals)
print signals # it works correctly
app = QApplication(sys.argv)
form = MainDialog()
form.show()
app.exec_()