PyQt自动完成QlineEdit不显示列表项

问题描述 投票:1回答:2

我编写了一种从数据库获取数据的方法。

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

此方法从数据库获取一个数据并使用它:

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

效果很好。但是,由于cursor.fetchone()以上,因此仅显示一个数据。当我更改此行cursor.fetchall()时,我可以从数据库中获取所有数据,但是这次引发了异常:

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'

那是什么问题?

python qt pyqt qlineedit qcompleter
2个回答
2
投票

由于它在使用fetchone()时有效,因此它意味着正在使用QCompleter的第3个构造函数。如果fetchone()返回一个“记录”,即字符串的元组(每选定的列一个),这是有意义的。由于fetchall()返回记录列表,并且您只需要每个记录的第一个字段,因此需要首先提取数据:

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

1
投票

方法connect()返回列表。您需要此列表的一个元素:

val = connect()[0]
completer = QtGui.QCompleter(val, edit)   
© www.soinside.com 2019 - 2024. All rights reserved.