QComboBox 自动完成(QCompleter?)

QComboBox Auto Complete (QCompleter?)

我对 Qt Designer 中的 Python GUI 小部件有另一个问题。 我正在使用 Python 3.7 w/ PyQt5.

我有一个从 SQL table 生成到组合框的值列表。 组合框正确显示了所有值,但总共有大约 100 个值,我希望能够输入并开始自动完成,这样我就可以快速找到并 select 我可能需要的任何值。

我做了一些让我感到困惑的研究。 我在 Python 中创建的列表名为 listofCustOrders,因为我正在构建一个 "business gui" 来帮助我了解有关 Python 编码的更多信息。有没有办法自动完成此列表?

from PyQt5 import QtWidgets, uic
from Classes import CustOrders as CO
import DBConnection as DB
import time

class MyWindow(QtWidgets.QMainWindow):

    listofCustOrders = []

    def __init__(self):
        super(MyWindow, self).__init__()
        uic.loadUi('PyGUI.ui',self)
        self.init()

    def init(self):
        global listofCustOrders

        listofCustOrders = CO.CustOrders.getCustOrders()

        for x in listofCustOrders:
            self.cbCONum.addItem(x.getCustOrderNO())

        self.cbCONum.currentIndexChanged.connect(self.coSelected)
        self.CObutton.clicked.connect(self.Submitted1)
        self.SLabel2.hide()

    def coSelected(self, text):
        cbCOIndex = self.cbCONum.currentIndex()
        selectedCO = listofCustOrders[cbCOIndex]
        self.RLbl2.setText(selectedCO.getPart())
        self.QtyLbl3.setText(str(selectedCO.getQTY()))

    def Submitted1(self):
        self.SLabel1.hide()
        self.SLabel2.show()

        CBW = str(self.cbCONum.currentText())
        PN = self.RLbl2.text()
        QY = self.QLINE.text()
        EP = self.EMPLINE.text()
        TIMER = time.strftime('%m-%d-%Y %H:%M:%S')

        conn1 = DB.DBConnection.getConnection()
        cursor = conn1.cursor()
        cursor.execute('''
                    INSERT INTO database.dbo (CustOrderNo, PartNo, Qty, Employee, Color)
                     VALUES (?, ?, ?, ?, ?)''',
                   (CBW, PN, QY, EP,TIMER,))
        conn1.commit()
        conn1.close()

        self.QLINE.clear()
        self.EMPLINE.clear()
        self.RLbl2.clear()

def main():
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

Qt 具有用于此类任务的 QCompleter class,这里是您如何使用它的示例。

completer = QCompleter(wordList, self)
completer.setCaseSensitivity(Qt.CaseInsensitive)
comboBox.setCompleter(completer)

供参考:https://doc.qt.io/qt-5/qcompleter.html, also checkout simple examples that show how to change your completer if your model (set of words) changed - https://doc.qt.io/qt-5/qtwidgets-tools-completer-example.html。不幸的是,这些示例是用 C++ 编写的。