Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQt QComboBox setting number of visible items in dropdown

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

Code 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_())
like image 298
Bradley Powers Avatar asked Jun 28 '12 20:06

Bradley Powers


1 Answers

According to the documentation:

The property maxVisibleItems is ignored for non-editable comboboxes in styles that returns true for `QStyle::SH_ComboBox_Popup such as the Mac style or the Gtk+ Style.

And you can override that SH_ComboBox_Popup style hint with a style sheet:

combo.setStyleSheet("QComboBox { combobox-popup: 0; }");
like image 86
alexisdm Avatar answered Oct 14 '22 02:10

alexisdm