Search code examples
pythonqtpyqtqlineeditqcompleter

PyQt Autocomplete QlineEdit Doesn't Show List Items


I write a method that gets data from database.

def connect():
connection = pymssql.connect(".","sa", "1234","Deneme")
cursor = connection.cursor()
cursor.execute("select BolumAdi from BOLUMLER")
return cursor.fetchone()  //problem is here

This method get one data from database and use it :

def main():

    app = QtGui.QApplication(sys.argv)
    edit = QtGui.QLineEdit()
    list = connect()
    completer = QtGui.QCompleter(list,edit)
    edit.setWindowTitle("PyQT QLineEdit Auto Complete")
    edit.setCompleter(completer)
    edit.show()

    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

It works good . But this only shows one data because of above cursor.fetchone().When i change this line cursor.fetchall() , i can get all datas from database but this time raise and exception :

TypeError: arguments did not match any overloaded call:
QCompleter(QObject parent=None): argument 1 has unexpected type 'list'
QCompleter(QAbstractItemModel, QObject parent=None): argument 1 has unexpected type 'list'
QCompleter(list-of-str, QObject parent=None): argument 1 has unexpected type 'list'

So what is the problem ?


Solution

  • Since it works when you use fetchone(), it implies the 3rd constructor of QCompleter is being used. This makes sense if fetchone() returns one "record", ie a tuple of strings (one for each column selected). Since fetchall() returns a list of records, and you only need the first field of each record, you need to extract the data first:

    # strings = connect()  # if fetchone()
    strings = [item[0] for item in connect()]  # if fetchall()
    completer = QtGui.QCompleter(strings)