尝试在pyqt5的QCompleter中捕获块未捕获异常

时间:2019-07-11 15:19:42

标签: python pyqt5

我有一个qcompleter设置,以便当突出显示建议时它可以填写两个不同的lineEdits。当用户使用箭头迭代建议并超出范围时,将出现索引错误。例如,如果完成者给出了一个建议,但用户两次按下了向下箭头,则所选建议超出范围并引发异常。错误是我将其包围在try try块周围,但无论出于何种原因都没有捕获到异常。当用户尝试越界时,是否可以强制突出显示的选择从索引0开始?

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QCompleter


class Ui_MainWindow(object):

    def on_button(self, text):
        try:
            nameList = text.split(', ')
            self.lineEdit_2.setText(nameList[0])
            QtCore.QTimer.singleShot(0, lambda: self.lineEdit.setText(nameList[1]))

        except IndexError:
            print("Out of bounds")

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(439, 254)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(20, 90, 180, 25))
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_2.setGeometry(QtCore.QRect(240, 90, 180, 25))
        self.lineEdit_2.setObjectName("lineEdit_2")

        food = ["pizza, chikin", "chikin, pizza", "chikin, pizza pizza", "chikin, pizza", "fried, pizza"]

        completer = QCompleter(food)
        completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
        completer.setFilterMode(QtCore.Qt.MatchContains)
        self.lineEdit.setCompleter(completer)

        try:

            completer.highlighted.connect(self.on_button)
            completer.activated.connect(self.on_button)

        except IndexError:
            print("Out of bounds")
        MainWindow.setCentralWidget(self.centralwidget)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

回溯(最近通话最近):   在第13行的文件“ stack.py”中     QtCore.QTimer.singleShot(0,lambda:self.lineEdit.setText(nameList [1])) IndexError:列表索引超出范围

3 个答案:

答案 0 :(得分:1)

主要的理解是,在try ... except块中,您具有将功能连接到信号的功能。不执行它们。

节省计时器使用量的解决方案:

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QCompleter


class Ui_MainWindow(object):

    def on_button(self, text):
        if text not in self.food:
            self.lineEdit_2.setText("")
            return
        nameList = text.split(', ')
        self.lineEdit_2.setText(nameList[0])
        QtCore.QTimer.singleShot(0, lambda: self.lineEdit.setText(nameList[1]))


    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(439, 254)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(20, 90, 180, 25))
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_2.setGeometry(QtCore.QRect(240, 90, 180, 25))
        self.lineEdit_2.setObjectName("lineEdit_2")

        self.food = ["pizza, chikin", "chikin, pizza", "chikin, pizza pizza", "chikin, pizza", "fried, pizza"]

        completer = QCompleter(self.food)
        completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
        completer.setFilterMode(QtCore.Qt.MatchContains)
        self.lineEdit.setCompleter(completer)

        try:

            completer.highlighted.connect(self.on_button)
            # completer.activated.connect(self.on_button)

        except IndexError:
            print("Out of bounds")
        MainWindow.setCentralWidget(self.centralwidget)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

答案 1 :(得分:0)

我只运行您的代码,但不明白为什么必须在QTimer中更改lineEdit的值。我只是这样做

self.lineEdit.setText(nameList[1])

以及IndexError的建议和捕获:据我所知,它的工作原理是这样。错误可能来自QTimer内部的调用。也许您有使用QTimer的理由,但我不清楚。

答案 2 :(得分:0)

如果我没记错,OP将使用

QtCore.QTimer.singleShot

因此,一旦在索引1中设置了文本,就可以创建计时器事件以将列表索引切换回0。

尝试简单地设置文字

self.lineEdit.setText(nameList[1])

然后使用您的except块来调用一个函数,该函数将在nameList中重新选择索引0。

异常处理中的错误可能与计时器事件的活动运行时间有关-尽管不确定。