I am trying to implement Qcompleter in a QlineEdit. When I try to change the maximum size of QAbstractItemView nothing happens, it remains at a default maximum size.
Image depicting the maximum size of QCompleter.
If I put a maximum Height below this size, it works and limits. But if I put a value above it does not work and remains at the default size.
test code of what I'm trying to do:
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setFixedSize(800, 500)
p = QtGui.QPalette()
p.setColor(QtGui.QPalette.Window, QtCore.Qt.white)
self.setAutoFillBackground(True)
self.setPalette(p)
frame = FrameAutoComplete()
complete = QtWidgets.QCompleter(['dsadasdsadsa', 'PSDPASDA', 'PDWQEWQEWQE', 'POSTAL ASDSa', 'PODSADsadsa', 'gfgsdfgsdfgsd', 'PSDPASDA', 'PDWQEWQEWQE', 'POSTAL ASDSa', 'PODSADsadsa'])
complete.setPopup(frame)
complete.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
entry = QtWidgets.QLineEdit(self)
entry.setCompleter(complete)
self.init_window()
def init_window(self):
self.show()
class FrameAutoComplete(QtWidgets.QListView):
def __init__(self, parent=None):
super().__init__(parent)
self.setFrameStyle(QtWidgets.QFrame.Box | QtWidgets.QFrame.Plain)
self.setWindowFlags(QtCore.Qt.Popup | QtCore.Qt.FramelessWindowHint | QtCore.Qt.NoDropShadowWindowHint)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
#self.setMaximumHeight(400) #NOT WORK
#self.setMaximumHeight(50) #WORK
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
The height of the popup is calculated taking into account the maxVisibleItems
as the docs points out:
maxVisibleItems : int
This property holds the maximum allowed size on screen of the completer, measured in itemsBy default, this property has a value of 7.
And is also limited by the maximum height allowed by the popup itself, in conclusion it is equivalent to:
height_popup = min(maximumHeightof the popup,
height calculate using maxVisibleItems items of QCompleter)
which is equivalently implemented in the source code.
So assuming you want to increase the height of the popup to show more items then the solution is to increase the maxVisibleItems
property:
complete.setMaxVisibleItems(number_of_visibles_items)