pythonpython-3.xpyqtpyqt5qcompleter

Limit Number Of Rows in PyQt QCompleter


How can I limit the number of rows that are displayed in a PyQt QCompleter. For example, if there are 10 matches, how would I limit. This is my code:

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, 0)

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

I did try looking this link, but didn't understand it at all. When I tried to set the model, I kept getting errors.


Solution

  • If you want to set a maximum of visible elements in the QCompleter then you must use the maxVisibleItems property:

    completer.setMaxVisibleItems(10)