清除 QTreeWidget 时 PyQt5 GUI 崩溃 [英] PyQt5 GUI crashes when QTreeWidget is cleared

查看:97
本文介绍了清除 QTreeWidget 时 PyQt5 GUI 崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码行中去掉#后,为什么点击按钮时GUI会崩溃?

When the # is removed from the line in the code below, why does the GUI crash when the button is clicked?

代码中的行是:

self.treeWidget.currentItemChanged.connect(self.current_selection)

self.treeWidget.currentItemChanged.connect(self.current_selection)

from PyQt5 import QtWidgets

class Ui_Form(object):

    def setupUi(self, Form):

        Form.setWindowTitle("Test")
        Form.resize(600, 400)

        self.gridLayout = QtWidgets.QGridLayout(Form)

        self.treeWidget = QtWidgets.QTreeWidget(Form)
        self.treeWidget.setColumnCount(3)
        self.treeWidget.setHeaderLabels(['No.', 'Colour', 'Animal'])
        #self.treeWidget.currentItemChanged.connect(self.current_selection)

        self.buttonWidget1 = QtWidgets.QPushButton('Click1')
        self.buttonWidget1.clicked.connect(self.test1)

        self.gridLayout.addWidget(self.treeWidget, 0, 0)
        self.gridLayout.addWidget(self.buttonWidget1, 1, 0)

        self.populate()

    def test1(self):
        self.treeWidget.clear()

    def current_selection(self):
        item = self.treeWidget.currentItem()
        self.no = item.text(0)
        self.colour = item.text(1)
        self.animal = item.text(2)
        print(self.no, self.colour, self.animal)

    def populate(self):
        item = QtWidgets.QTreeWidgetItem(['1', 'Yellow', 'Dog'])
        self.treeWidget.addTopLevelItem(item)

        item = QtWidgets.QTreeWidgetItem(['2', 'Black', 'Cat'])
        self.treeWidget.addTopLevelItem(item)

        item = QtWidgets.QTreeWidgetItem(['3', 'Green', 'Frog'])
        self.treeWidget.addTopLevelItem(item)

        item = QtWidgets.QTreeWidgetItem(['4', 'Blue', 'Snail'])
        self.treeWidget.addTopLevelItem(item)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

如果有人能向我解释发生了什么,我将不胜感激.

If anyone can explain to me what is occurring, it'd be greatly appreciated.

推荐答案

回答具体问题:GUI为什么崩溃?

To answer the specific question: Why does the GUI crash?

这是由于 PyQt-5.5 引入的新行为.引用 PyQt5 文档:

This is due to new behaviour which was introduced with PyQt-5.5. To quote from the PyQt5 docs:

在 PyQt v5.5 中,未处理的 Python 异常将导致调用Qt 的 qFatal() 函数.默认情况下,这将调用 abort() 和应用程序将终止.请注意,安装了一个应用程序异常钩子仍然优先.

In PyQt v5.5 an unhandled Python exception will result in a call to Qt’s qFatal() function. By default this will call abort() and the application will terminate. Note that an application installed exception hook will still take precedence.

幸运的是,正如最后一句所表明的,这种新行为可以通过安装 excepthook,这将使您的程序以更优雅的方式处理未处理的异常.

Fortunately, as the final sentence indicates, this new behaviour can be bypassed by installing an excepthook, which will allow your program to deal with unhandled exceptions in a more graceful manner.

作为最低限度,简单地将回溯打印到 stdout/stderr 的旧行为可以像这样恢复:

As a bare minimum, the old behaviour of simply printing the traceback to stdout/stderr can be restored like this:

def except_hook(cls, exception, traceback):
    sys.__excepthook__(cls, exception, traceback)

if __name__ == "__main__":

    import sys
    sys.excepthook = except_hook

但显然,在某种消息框中显示错误是通知用户出现问题的更可取的方式.

But obviously, displaying the error in some kind of message-box would be a far more preferrable way to inform the user that a problem has occurred.

这篇关于清除 QTreeWidget 时 PyQt5 GUI 崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆