Limit the number of visible items on QComboBox
-
Hi, I have a QComboBox that I am using to display some data to the user. In the drop down list there are more than 20 items, all of which are visible at once when the list pops up. I want to limit the number of visible items to 10, so that the later elements may be chosen by scrolling down the popup list. I have used the maxVisibleItems property, but it doesn't seem to work.
Any idea on how to do this? I am using Qt on Linux Mint 11, by the way.
-
[quote author="Andre" date="1321253737"]Did you spot the QComboBox::maxVisibleItems property?[/quote]
Yes, I've set the maxVisibleItems property to 5 from Qt Designer's Property Explorer, but it doesn't work.
From this "link":http://doc.qt.nokia.com/stable/qcombobox.html#maxVisibleItems-prop -
bq. Note: This property is ignored for non-editable comboboxes in styles that returns false for QStyle::SH_ComboBox_Popup such as the Mac style or the Gtk+ Style.
Could this be the reason? If it is, then what workarounds (i.e., some other widgets to use) do I have here?
-
Ah, it could very well be, yes.
First of all, this behaviour is a property of the style you are using. Your users choose to use that style, and it would be good if you can accomodate their preference. If you don't like the style, then perhaps you should be using another one.If you really cannot, I see few options than actually using a proxy style to override the way the combox creates its list. I guess the styles mentioned above use a menu-like popup instead of a list-like popup for non-editable comboboxes. So, perhaps you can override that somehow, possibly by making the combobox itself render as if it is non-editable but behave as if it is. It is going to be a bit of a hack, I am afraid.
-
A bit overdated, but for a goods sake, this solution works fine for me:
@
QComboBox combo;
combo.setStyleSheet("combobox-popup: 0;");
@Also you can set stylesSheet property from the designer.
-
Hi!
I ran into this same problem and the last answer (setting combox-popup property) worked for me but I cannot find documentation about this property anywhere. Could someone point me in the direction of a reference about this exact property of where did anybody find it?
Thanks!
-
A bit overdated, but for a goods sake, this solution works fine for me:
@
QComboBox combo;
combo.setStyleSheet("combobox-popup: 0;");
@Also you can set stylesSheet property from the designer.
@MadisonTrash It does work, thank you!
-
Hi!,
I am using Qt 4.8 version in my project, and I wanted to limit the QComboBox's drop-down list height, I accomplished that by using styleSheet setStyleSheet("combobox-popup: 0;"), this may help others too.
Thank you ☺️.
@Bramha Hi, Could you please share few lines on how did you add this? I used the same, but it did not work for me unfortunately. I am using python and Qt Designer 5.11 version. Is this correct if I add like this?
I already have a stylesheet: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);")I added two lines below this stylesheet.
self.comboBox_2.setStyleSheet("combobox-popup: 0;")
self.comboBox_2.setMaxVisibleItems(5) --------># I tried even without this line of property 'setMaxVisibleItems'. But no luckCould you please let me know incase if it worked out for you
-
@Bramha Hi, Could you please share few lines on how did you add this? I used the same, but it did not work for me unfortunately. I am using python and Qt Designer 5.11 version. Is this correct if I add like this?
I already have a stylesheet: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);")I added two lines below this stylesheet.
self.comboBox_2.setStyleSheet("combobox-popup: 0;")
self.comboBox_2.setMaxVisibleItems(5) --------># I tried even without this line of property 'setMaxVisibleItems'. But no luckCould you please let me know incase if it worked out for you
@Qt_Python-Learner Don't use 2 styleSheets, use stylesheet like this QString style = "QComboBox { combobox-popup: 0; font-size: 10px; font-face: ArialMT; max-height: 25px; min-width: 150px; }";
-
A bit overdated, but for a goods sake, this solution works fine for me:
@
QComboBox combo;
combo.setStyleSheet("combobox-popup: 0;");
@Also you can set stylesSheet property from the designer.
It works thanks!!