Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
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.
andre last edited by
Did you spot the QComboBox::maxVisibleItems property?
[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?
andre last edited by
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.
@Andre: I see. Thank you very much for your help.
MadisonTrash last edited by
A bit overdated, but for a goods sake, this solution works fine for me:
Also you can set stylesSheet property from the designer.
slymas last edited by
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?
Bramha last edited by Bramha
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 ☺️.
metaliuyong last edited by
@MadisonTrash It does work, thank you!