Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Limit the number of visible items on QComboBox
Forum Updated to NodeBB v4.3 + New Features

Limit the number of visible items on QComboBox

Scheduled Pinned Locked Moved General and Desktop
12 Posts 8 Posters 15.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sayem.bd
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    1
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Did you spot the QComboBox::maxVisibleItems property?

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sayem.bd
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sayem.bd
            wrote on last edited by
            #5

            @Andre: I see. Thank you very much for your help.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MadisonTrash
              wrote on last edited by
              #6

              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.

              M S 2 Replies Last reply
              11
              • S Offline
                S Offline
                slymas
                wrote on last edited by
                #7

                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!

                1 Reply Last reply
                1
                • BramhaB Offline
                  BramhaB Offline
                  Bramha
                  wrote on last edited by Bramha
                  #8

                  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 ☺️.

                  Q 1 Reply Last reply
                  2
                  • M MadisonTrash

                    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.

                    M Offline
                    M Offline
                    metaliuyong
                    wrote on last edited by
                    #9

                    @MadisonTrash It does work, thank you!

                    1 Reply Last reply
                    0
                    • BramhaB Bramha

                      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 ☺️.

                      Q Offline
                      Q Offline
                      Qt_Python Learner
                      wrote on last edited by Qt_Python Learner
                      #10

                      @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 luck

                      Could you please let me know incase if it worked out for you

                      BramhaB 1 Reply Last reply
                      0
                      • Q Qt_Python Learner

                        @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 luck

                        Could you please let me know incase if it worked out for you

                        BramhaB Offline
                        BramhaB Offline
                        Bramha
                        wrote on last edited by
                        #11

                        @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; }";

                        1 Reply Last reply
                        0
                        • M MadisonTrash

                          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.

                          S Offline
                          S Offline
                          Salma_Nasser
                          wrote on last edited by
                          #12

                          @MadisonTrash

                          It works thanks!!

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved