PyQt QComboBox sets the number of visible items in the drop-down menu.

I am working on an application in PyQt that accepts a dictionary of objects and allows you to build variable flows from a robot in real time. One of the things I'm working on to enable this is the drop down menu. Unfortunately, we have a couple of hundred variables, so my PyQt Combobox pops up from the top of the screen to the bottom with items when clicked. I would like to limit the number of elements displayed simultaneously to 20, with the ability to scroll to see the rest. I tried using the documented setMaxVisibleItems method, but it doesn’t affect the dropdown at all. Any recommendations?

The code is here:

#!/usr/bin/env python

from PyQt4.QtCore import Qt
from PyQt4.QtGui import QComboBox, QApplication

from cli.parc2_od import cli_od
import sys

app = QApplication(sys.argv)

items = cli_od.OD.keys() #Items to populate dropdown.
combo = QComboBox()

#The solution:
combo.setStyleSheet("QComboBox { combobox-popup: 0; }")
combo.setMaxVisibleItems(10)




combo.addItems(items)

combo.resize(300, 30)
combo.show()


sys.exit(app.exec_())
+5
source share
1

:

maxVisibleItems comboboxes , true ` QStyle:: SH_ComboBox_Popup, Mac Gtk +.

SH_ComboBox_Popup :

combo.setStyleSheet("QComboBox { combobox-popup: 0; }");
+7

All Articles