MyOwnIssues
MyOwnIssues

Reputation: 53

QComboBox Auto Complete (QCompleter?)

I have another question with Python GUI widgets in Qt Designer. I am using Python 3.7 w/ PyQt5.

I have a list of values being generated to a combo box from an SQL table. The combo box displays all of the values correctly but there are around 100 values total, I want to be able to type and it start to autocomplete so I can quickly find and select any value I may need.

I've done some research which has me baffled. The list I created in Python is named listofCustOrders as I am building a "business gui" to help me learn more about Python coding. Is there a way to autocomplete this list?

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()

Upvotes: 2

Views: 5367

Answers (1)

htzfun
htzfun

Reputation: 1448

Qt has QCompleter class for such tasks, here is an example of how you can use it.

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 . The examples are in C++, unfortunately.

Upvotes: 7

Related Questions