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. StyleSheet & QComboBox, height trouble
QtWS25 Last Chance

StyleSheet & QComboBox, height trouble

Scheduled Pinned Locked Moved Solved General and Desktop
qstylesheetqcomboboxqabstractitemvimin-height
10 Posts 4 Posters 11.6k Views
  • 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.
  • J.HilkJ Offline
    J.HilkJ Offline
    J.Hilk
    Moderators
    wrote on last edited by
    #1

    Hi everyone,

    this feels like I had the situation before, but for the life of me, I can't remember how to fix it.

    I want to applay custom Stylesheet to a standart QComboBox, but I find myself unable to change the height of the items inside the popup QAbstractItemView.

    I would like them to be at least 50 px tall.
    Here's my stylesheet:

                       "QComboBox{"
                        "border: 0px;"
                        "color:rgb(87,117,131);"
                        "background-color: white;"
                      "}"
                      "QComboBox::drop-down{"
                        "border: 0px;"
                      "}"
                      "QComboBox::down-arrow {"
                        "image: url(:/data/GenericIcons/SpinDown.png);"
                        "width:50px;"
                        "height:50px;"
                        "padding-right:38px;"
                      "}"
                      "QComboBox QAbstractItemView {"
                        "border: 2px solid darkgray;"
                        "selection-background-color: lightgray;"
                        "color:rgb(87,117,131);"
                      "}"
                      "QComboBox QAbstractItemView::item {"
                        "min-height: 50px;"
                      "}"
    

    I assumed, that QComboBox QAbstractItemView::item would adress the items of the popup, but no matter what I write there, it does not seem to have any effect.

    any tip is appreciated.


    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


    Q: What's that?
    A: It's blue light.
    Q: What does it do?
    A: It turns blue.

    raven-worxR D 2 Replies Last reply
    0
    • J.HilkJ J.Hilk

      Hi everyone,

      this feels like I had the situation before, but for the life of me, I can't remember how to fix it.

      I want to applay custom Stylesheet to a standart QComboBox, but I find myself unable to change the height of the items inside the popup QAbstractItemView.

      I would like them to be at least 50 px tall.
      Here's my stylesheet:

                         "QComboBox{"
                          "border: 0px;"
                          "color:rgb(87,117,131);"
                          "background-color: white;"
                        "}"
                        "QComboBox::drop-down{"
                          "border: 0px;"
                        "}"
                        "QComboBox::down-arrow {"
                          "image: url(:/data/GenericIcons/SpinDown.png);"
                          "width:50px;"
                          "height:50px;"
                          "padding-right:38px;"
                        "}"
                        "QComboBox QAbstractItemView {"
                          "border: 2px solid darkgray;"
                          "selection-background-color: lightgray;"
                          "color:rgb(87,117,131);"
                        "}"
                        "QComboBox QAbstractItemView::item {"
                          "min-height: 50px;"
                        "}"
      

      I assumed, that QComboBox QAbstractItemView::item would adress the items of the popup, but no matter what I write there, it does not seem to have any effect.

      any tip is appreciated.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @J.Hilk
      the reason why no stylesheet gets applied is, that the combobox's list view has a special delegate assigned (QComboBoxDelegate ) which just inherits QItemDelegate but not QStyledItemDelegate.

      I don't know if it works, but you can try to assign your custom delegate to the combobox's list view. But IIRC that also comes with some visual side effects, so you would need to style the whole list view i guess.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      2
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        It does respond to Qt::SizeHintRole
        cb.model()->setData(cb.model()->index(0, 0), QSize(100, 100), Qt::SizeHintRole);

        1 Reply Last reply
        1
        • J.HilkJ J.Hilk

          Hi everyone,

          this feels like I had the situation before, but for the life of me, I can't remember how to fix it.

          I want to applay custom Stylesheet to a standart QComboBox, but I find myself unable to change the height of the items inside the popup QAbstractItemView.

          I would like them to be at least 50 px tall.
          Here's my stylesheet:

                             "QComboBox{"
                              "border: 0px;"
                              "color:rgb(87,117,131);"
                              "background-color: white;"
                            "}"
                            "QComboBox::drop-down{"
                              "border: 0px;"
                            "}"
                            "QComboBox::down-arrow {"
                              "image: url(:/data/GenericIcons/SpinDown.png);"
                              "width:50px;"
                              "height:50px;"
                              "padding-right:38px;"
                            "}"
                            "QComboBox QAbstractItemView {"
                              "border: 2px solid darkgray;"
                              "selection-background-color: lightgray;"
                              "color:rgb(87,117,131);"
                            "}"
                            "QComboBox QAbstractItemView::item {"
                              "min-height: 50px;"
                            "}"
          

          I assumed, that QComboBox QAbstractItemView::item would adress the items of the popup, but no matter what I write there, it does not seem to have any effect.

          any tip is appreciated.

          D Offline
          D Offline
          Devopia53
          wrote on last edited by Devopia53
          #4

          @J.Hilk

          like this:

          comboBox->setView(new QListView);
          

          or

          Set QComboBox QAbstractItemView {} first.

          1 Reply Last reply
          2
          • J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            Well, thanks all for the help,

            @raven-worx said in StyleSheet & QComboBox, height trouble:

            the reason why no stylesheet gets applied is, that the combobox's list view has a special delegate assigned (QComboBoxDelegate) which just inherits QItemDelegate but not QStyledItemDelegate.

            Well, it's not that no stylesheet is applied, this part

            "QComboBox QAbstractItemView {"
                                "border: 2px solid darkgray;"
                                "selection-background-color: lightgray;"
                                "color:rgb(87,117,131);"
                              "}"
            

            works absolutely fine, and if I give it a min-height it is applied, but it is applied to the whole view. The min-height of the items stays the same.

            @mrjj said in StyleSheet & QComboBox, height trouble:

            It does respond to Qt::SizeHintRole
            cb.model()->setData(cb.model()->index(0, 0), QSize(100, 100), Qt::SizeHintRole);

            I tried it (just after the creation) but it does not seem to have any effect, I would have to look further into that.

            @Devopia53 said in StyleSheet & QComboBox, height trouble:

            like this:
            comboBox->setView(new QListView);

            Well, color me surprised this is actually the solution! By giving the QCombobox a new QListView after creation I'm now able to access the Items of the QAbstractItemView, via StyleSheet.

            The only question is, why is it not the default behaviour!?

            Thank you very much @Devopia53 !

            Btw, I kown now, how I solved this before. It's been a while that I worked on this project and I finally remembered. I made a complete custom Widget with from the bottom up sliding, tumbler-esc selection method.

            It's horrible implemented on my part, but to be consitent with the rest of the program, I'll probably end up using it.

            Thanks everyone for your time and help!


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            mrjjM raven-worxR 2 Replies Last reply
            1
            • J.HilkJ J.Hilk

              Well, thanks all for the help,

              @raven-worx said in StyleSheet & QComboBox, height trouble:

              the reason why no stylesheet gets applied is, that the combobox's list view has a special delegate assigned (QComboBoxDelegate) which just inherits QItemDelegate but not QStyledItemDelegate.

              Well, it's not that no stylesheet is applied, this part

              "QComboBox QAbstractItemView {"
                                  "border: 2px solid darkgray;"
                                  "selection-background-color: lightgray;"
                                  "color:rgb(87,117,131);"
                                "}"
              

              works absolutely fine, and if I give it a min-height it is applied, but it is applied to the whole view. The min-height of the items stays the same.

              @mrjj said in StyleSheet & QComboBox, height trouble:

              It does respond to Qt::SizeHintRole
              cb.model()->setData(cb.model()->index(0, 0), QSize(100, 100), Qt::SizeHintRole);

              I tried it (just after the creation) but it does not seem to have any effect, I would have to look further into that.

              @Devopia53 said in StyleSheet & QComboBox, height trouble:

              like this:
              comboBox->setView(new QListView);

              Well, color me surprised this is actually the solution! By giving the QCombobox a new QListView after creation I'm now able to access the Items of the QAbstractItemView, via StyleSheet.

              The only question is, why is it not the default behaviour!?

              Thank you very much @Devopia53 !

              Btw, I kown now, how I solved this before. It's been a while that I worked on this project and I finally remembered. I made a complete custom Widget with from the bottom up sliding, tumbler-esc selection method.

              It's horrible implemented on my part, but to be consitent with the rest of the program, I'll probably end up using it.

              Thanks everyone for your time and help!

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @J.Hilk

              -I tried it (just after the creation) but it does not seem to have any effect, I would have to look further into that.

              Well it has to have items and you need to set on each.
              alt text

              QAbstractItemModel* model = ui->comboBox->model();
              auto rows = model->rowCount(QModelIndex());
              for (int i = 0; i < rows; ++i) {
              QModelIndex index = model->index(i, 0);
              model->setData(index, QSize(50, 50), Qt::SizeHintRole);
              }

              J.HilkJ 1 Reply Last reply
              2
              • mrjjM mrjj

                @J.Hilk

                -I tried it (just after the creation) but it does not seem to have any effect, I would have to look further into that.

                Well it has to have items and you need to set on each.
                alt text

                QAbstractItemModel* model = ui->comboBox->model();
                auto rows = model->rowCount(QModelIndex());
                for (int i = 0; i < rows; ++i) {
                QModelIndex index = model->index(i, 0);
                model->setData(index, QSize(50, 50), Qt::SizeHintRole);
                }

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @mrjj
                well, like I said, I would have to look more into it 0_1533101509748_0fbf2689-82b0-4b51-a3b0-21a1742689a8-image.png

                Thanks for the example code. It's defenitely the more flexible way to change the item height. Especially if one allows for resizing and different resolutions!


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                0
                • J.HilkJ J.Hilk

                  Well, thanks all for the help,

                  @raven-worx said in StyleSheet & QComboBox, height trouble:

                  the reason why no stylesheet gets applied is, that the combobox's list view has a special delegate assigned (QComboBoxDelegate) which just inherits QItemDelegate but not QStyledItemDelegate.

                  Well, it's not that no stylesheet is applied, this part

                  "QComboBox QAbstractItemView {"
                                      "border: 2px solid darkgray;"
                                      "selection-background-color: lightgray;"
                                      "color:rgb(87,117,131);"
                                    "}"
                  

                  works absolutely fine, and if I give it a min-height it is applied, but it is applied to the whole view. The min-height of the items stays the same.

                  @mrjj said in StyleSheet & QComboBox, height trouble:

                  It does respond to Qt::SizeHintRole
                  cb.model()->setData(cb.model()->index(0, 0), QSize(100, 100), Qt::SizeHintRole);

                  I tried it (just after the creation) but it does not seem to have any effect, I would have to look further into that.

                  @Devopia53 said in StyleSheet & QComboBox, height trouble:

                  like this:
                  comboBox->setView(new QListView);

                  Well, color me surprised this is actually the solution! By giving the QCombobox a new QListView after creation I'm now able to access the Items of the QAbstractItemView, via StyleSheet.

                  The only question is, why is it not the default behaviour!?

                  Thank you very much @Devopia53 !

                  Btw, I kown now, how I solved this before. It's been a while that I worked on this project and I finally remembered. I made a complete custom Widget with from the bottom up sliding, tumbler-esc selection method.

                  It's horrible implemented on my part, but to be consitent with the rest of the program, I'll probably end up using it.

                  Thanks everyone for your time and help!

                  raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by
                  #8

                  @J.Hilk said in StyleSheet & QComboBox, height trouble:

                  Well, it's not that no stylesheet is applied, this part
                  "QComboBox QAbstractItemView {"
                  "border: 2px solid darkgray;"
                  "selection-background-color: lightgray;"
                  "color:rgb(87,117,131);"
                  "}"

                  works absolutely fine, and if I give it a min-height it is applied, but it is applied to the whole view. The min-height of the items stays the same.

                  Sure you can style the view itself.
                  I said the delegate (=> items) do not get a stylesheet applied.

                  How do you add items to your combobox exactly?

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  J.HilkJ 1 Reply Last reply
                  1
                  • raven-worxR raven-worx

                    @J.Hilk said in StyleSheet & QComboBox, height trouble:

                    Well, it's not that no stylesheet is applied, this part
                    "QComboBox QAbstractItemView {"
                    "border: 2px solid darkgray;"
                    "selection-background-color: lightgray;"
                    "color:rgb(87,117,131);"
                    "}"

                    works absolutely fine, and if I give it a min-height it is applied, but it is applied to the whole view. The min-height of the items stays the same.

                    Sure you can style the view itself.
                    I said the delegate (=> items) do not get a stylesheet applied.

                    How do you add items to your combobox exactly?

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by J.Hilk
                    #9

                    @raven-worx said in StyleSheet & QComboBox, height trouble:

                    How do you add items to your combobox exactly?

                    very simply with

                    void QComboBox::addItems(const QStringList &texts)
                    

                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    raven-worxR 1 Reply Last reply
                    0
                    • J.HilkJ J.Hilk

                      @raven-worx said in StyleSheet & QComboBox, height trouble:

                      How do you add items to your combobox exactly?

                      very simply with

                      void QComboBox::addItems(const QStringList &texts)
                      
                      raven-worxR Offline
                      raven-worxR Offline
                      raven-worx
                      Moderators
                      wrote on last edited by
                      #10

                      @J.Hilk
                      ok then you can follow the suggestion from @mrjj (in case you would have set a custom model you would have need to handle the Qt::SizeHintRole in your data() method)
                      or try to set a QStyledItemDelegate to the view and use your stylesheet.

                      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                      If you have a question please use the forum so others can benefit from the solution in the future

                      1 Reply Last reply
                      1

                      • Login

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