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.



  • 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?



  • 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.



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.