如何设置QCompleter模型后构造

时间:2018-09-29 08:13:00

标签: python user-interface pyqt5 python-3.6 qcompleter

我刚开始使用PyQt5,并在这里有一个小文件:

class Window(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self.setMinimumWidth(350)

        layout = QGridLayout()
        self.setLayout(layout)

        self.completer = QCompleter()
        self.completer.setCaseSensitivity(Qt.CaseInsensitive)
        self.completer.setCompletionMode(QCompleter.PopupCompletion)
        self.completer.setMaxVisibleItems(10)

        self.lineedit = QLineEdit()
        self.lineedit.setCompleter(self.completer)
        self.lineedit.setFixedHeight(30)
        self.lineedit.setFixedWidth(300)
        #self.lineedit.returnPressed.connect(self.show_selected)
        self.lineedit.textChanged.connect(self.text_changed)

        layout.addWidget(self.lineedit,0,0)

    def text_changed(self):
        #the function get_request returns a list of names taken from 
        #a database e.g ['matthew','mark','morris','mickey']
        vals = self.get_request(self.lineedit.text())
        self.completer.setModel(vals)

app = QApplication(sys.argv)

screen = Window()

sys.exit(app.exec_())

我的问题是这个错误:

TypeError: setModel(self, QAbstractItemModel): argument 1 has unexpected type 'list'

每次我运行上面的代码。所以我的问题是:确实我在做什么错,我只是在这里用pyqt5开始,作为我的第一个程序,我立即收到此错误。作为旁注,当我使用固定列表并将其放在QCompleter构造函数中,程序可以正常工作,但是这不是一个选择,因为我需要随着用户键入的结果而更改

1 个答案:

答案 0 :(得分:0)

您需要通过setStringList()更新QStringListModel

import sys
from PyQt5.QtWidgets import QApplication, QLineEdit, QWidget, QGridLayout, QCompleter
from PyQt5.QtCore import Qt

class Window(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self.setMinimumWidth(350)

        layout = QGridLayout()
        self.setLayout(layout)

        self.completer = QCompleter([])  # Initialze with a list...to establish QStringListModel
        self.completer.setCaseSensitivity(Qt.CaseInsensitive)
        self.completer.setCompletionMode(QCompleter.PopupCompletion)
        self.completer.setMaxVisibleItems(10)

        self.lineedit = QLineEdit()
        self.lineedit.setCompleter(self.completer)
        self.lineedit.setFixedHeight(30)
        self.lineedit.setFixedWidth(300)
        self.lineedit.textChanged.connect(self.text_changed)
        layout.addWidget(self.lineedit,0,0)

    def get_request(self, line_text):
        if not line_text:
            return []
        char = line_text[0]
        if char == 'a':
            return ['aaa', 'abba', 'abby']
        if char == 'm':
            return ['matthew','mark','morris','mickey']
        return ['foo', 'bar', 'baz']

    def text_changed(self):
        #the function get_request returns a list of names taken from
        #a database e.g ['matthew','mark','morris','mickey']
        vals = self.get_request(self.lineedit.text())

        model = self.completer.model()
        model.setStringList(vals)  # Updated the QStringListModel string list

app = QApplication(sys.argv)

screen = Window()
screen.show()

sys.exit(app.exec_())