【问题标题】:PyQt Autocomplete QlineEdit Doesn't Show List ItemsPyQt 自动完成 QlineEdit 不显示列表项
【发布时间】:2015-12-27 01:20:59
【问题描述】:

我写了一个从数据库中获取数据的方法。

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


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      connect() 方法返回一个列表。你想要这个列表中的一个元素:

      val = connect()[0]
      completer = QtGui.QCompleter(val, edit)   
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-12
        • 2012-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-12
        • 1970-01-01
        相关资源
        最近更新 更多