繁体   English   中英

QComboBox 自动完成 (QCompleter?)

[英]QComboBox Auto Complete (QCompleter?)

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

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

我做了一些让我困惑的研究。 我在 Python 中创建的列表名为 listofCustOrders,因为我正在构建“业务 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)

For reference: 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++ 中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM