PyQt QCompleter 中的限制行数

问题描述

如何限制 PyQt QCompleter 中显示的行数。例如,如果有 10 个匹配项,我将如何限制。这是我的代码

from PyQt5.QtWidgets import *
from PyQt5 import QtCore
import sys

class Window(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        layout = qgridLayout()
        self.setLayout(layout)
                                              
        names = ["Apple","Alps","August","Alpha","Ate","A""Berry","Cherry" ]
        completer = QCompleter(names)
                               
        self.lineedit = QLineEdit()
        self.lineedit.setCompleter(completer)
        layout.addWidget(self.lineedit,0)

app = QApplication(sys.argv)
screen = Window()
screen.show()
sys.exit(app.exec_())

我确实尝试过查看 this link,但根本不明白。当我尝试设置模型时,我不断收到错误消息。

解决方法

如果要在 QCompleter 中设置最大可见元素,则必须使用 maxVisibleItems 属性:

completer.setMaxVisibleItems(10)