set maxVisibleItems property in Fusion Style for QtComboBox(dropdown box)
-
Hi all,
I am using Qt designer 5.11.1 and building a tool with Python (PyQt5). I saved my GUI in 'Fusion' style. In this, for combobox, there is a property 'maxVisibleItems'. Even though I set this property and limit the dropdown list so that only 5 elements are visible at a time and remaining needs to be scrolled, the combobox still displays all list of items present in the combo box.
I know the reason and this is the limitation that this property is ignored by non-editable styles as per documentation.
Note: This property 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.
With this property set, In Fusion style, the application shows all 20 items at once where as in other style, the application shows 5 items at a time . I do not want to change Fusion style. Could someone please help me if I can overcome.
Few users commented that using
"combo.setStyleSheet("QComboBox { combobox-popup: 0; }")"
helped them. But it did not work for me. Am I doing anything wrong or is there any different solution.
How I have tried:
self.comboBox_2.setStyleSheet("selection-color: rgb(255, 255, 255);\n" "background-color: rgb(249, 255, 255);\n" "selection-background-color: rgb(53, 107, 255);") self.comboBox_2.setObjectName("comboBox_2") self.comboBox_2.setStyleSheet("combobox-popup: 0;") self.comboBox_2.setMaxVisibleItems(5)
Thanks a lot in advance for sharing your knowledge with your valuable suggestions.
-
I know this issue is quite old, but it might still help somebody. I managed to limit the number of visible items in a Fusion style dashboard using this class:
class ComboBox(QtWidgets.QComboBox): """ Modify default class of PyQT5 combobox. """ def __init__(self, parent=None): super(ComboBox, self).__init__(parent) # setMaxVisibleItems only works if the box is editable # this creates a line edit that we need to overwrite self.setEditable(True) self.setMaxVisibleItems(15) # overwrite default line edit by an invisible one self.lineEdit().setFrame(False) self.lineEdit().setReadOnly(True) def showPopup(self): """ Show pop-up. """ # set index of selected choice to highlight it text = self.lineEdit().text() index = self.findText(text, QtCore.Qt.MatchFixedString) self.setCurrentIndex(index) # show pop-up super().showPopup() # add vertical scroll bar self.view().setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded) # increase the width of the elements on popup so they can be read self.view().setMinimumWidth(self.view().sizeHintForColumn(0))
I hope this can help you or others.
Cheers,
Alba