【问题标题】:PyQt segmentation fault with QThreadQThread的PyQt分段错误
【发布时间】:2013-05-21 13:48:15
【问题描述】:

我想在我用 Python 和 PyQt 编写的项目中使用 QThreads,但是当我运行这段代码时,我收到一个错误,它什么也没说:

segmentation fault (core dumped)  python file.py

我不知道这意味着什么以及代码有什么问题。 这是我的代码:

import sys
from PyQt4 import QtCore, QtGui
import time
from PyQt4.QtCore import SIGNAL, QObject, QTimer
import libtorrent as lt


try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Form(object):

    def setupUi(self, Form):
        #some code...

    def retranslateUi(self, Form):
        #some code...


class MyForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Form()
        self.ui.setupUi(self)   


class Runnable(QtCore.QRunnable):

    def run(self):
        count = 0
        app = QtCore.QCoreApplication.instance()
        while count < 5:
            print "Increasing"
            time.sleep(1)
            count += 1
        app.quit()

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    apps = QtCore.QCoreApplication(sys.argv)
    myapp = MyForm()
    myapp.show()
    runnable = Runnable()
    QtCore.QThreadPool.globalInstance().start(runnable)
    sys.exit(app.exec_())

【问题讨论】:

  • 带有最新更新的 Ubuntu 13.04
  • 您可以尝试的一件事是重新安装 PyQt。另一件事是确保所有模块都是从非损坏文件导入的。您可以通过在另一台机器上运行代码来检查这一点,例如在虚拟机上运行代码。

标签: python segmentation-fault pyqt qthread


【解决方案1】:

您不能创建多个应用程序实例。将apps = QtCore.QCoreApplication(sys.argv) 注释掉即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    • 2015-09-08
    • 2014-03-25
    • 2014-09-05
    相关资源
    最近更新 更多