Position of a Qt Combobox dropdown



  • My problem is that the anything I do in the QAbstractItemView is not being reflected in the combobox? I can't find out why?

        //set max visible to 3 items
        setMaxVisibleItems(3);
        //Do not insert item to combobox on pressing enter
        setInsertPolicy(QComboBox::NoInsert);
        //Set its size policy to [fixed,fixed]
        setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
        //Set height and width
        setFixedSize(133,35);
        //We change the default item delegate to styled item delegate so we can modify using stylesheets
        QStyledItemDelegate* itemDelegate = new QStyledItemDelegate();
        setItemDelegate(itemDelegate);
    
        //customize the combobox using stylesheet. QAbstractItemView is the popup view when the down arrow is clicked
        setStyleSheet("QComboBox {\
                      border: 1px solid rgb(228,228,228);\
                          border-radius: 2px;\
                      font-family: \"Helvetica Neue Med\";\
                      font-size: 11px;padding-left:25px;\
                  }\
                  \
                  QComboBox:!editable {\
                      color: rgb(72,72,72);\
                      background:  rgb(228,228,228);\
                  }\
                  \
                  QComboBox::drop-down {\
                      background:  rgb(228,228,228);\
                      width: 45px;\
                  }\
                  \
                  QComboBox::down-arrow {\
                      subcontrol-position: left;\
                      image: url(:/images/Arrow_off.png);\
                  }\
                  QComboBox::down-arrow:on { \
                      image: url(:/images/Arrow_on.png);\
                  }\
                    \
                  QComboBox QAbstractItemView {\
                      color: rgb(72,72,72);\
                      background: rgb(228,228,228);\
                      min-width:233px;\
                  }\
                  \
                  QComboBox QAbstractItemView::item {\
                      min-height: 27px;max-height:27px;\
                      border-bottom: 0px solid rgb(40,40,40);\
                      padding-right: 25px;\
                  }\
                  \
                  QComboBox QAbstractItemView::item:selected {\
                      background: rgb(72,72,72);\
                      color:rgb(28,228,228);\
                      padding-right: 25px;\
                  }\
                  ");
    
    


  • Hi @sunil-nair, can you please post the code where you make the QComboBox, and any related style changes?

    Also, maybe you can try setting a padding for the QComboBox?

    QComboBox {
            padding: 1px 0px 1px 3px;
    }
    


  • This post is deleted!


  • This post is deleted!

  • Moderators

    @sunil-nair Does it work if you donot provide the QStyledItemDelegate ? What was the purpose of using it if you haven't subclassed it ?



  • @p3c0 No if I dont use StyledItemdelegate, the entire list becomes non stylable. I have updated the mainquestion by inserting the code.


  • Moderators

    @sunil-nair I think if you want to use it QStyledItemDelegate you need to subclass it then re-implement paint then initStyleOption to initialize its defaults.

    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
      ...
      ...
      initStyleOption(&option, index);
      ...
      ...
    }
    


  • Thanks but I guess I figured something. If the QCombobox is non editable it cannot be styled (i am not able to ) using QAbstractItemView. Any styling on it requires a QListView object and we need to setView(QListView)

    void SteuerQComboBox::showPopup() {
    QComboBox::showPopup();
    QWidget popup = this->findChild<QFrame>();
    popup->move(popup->x(),popup->y()-this->height()-popup->height());
    }

    We can now position it anywhere we want. Please refer: http://stackoverflow.com/questions/10057140/how-to-make-qcombobox-popup-upwards


  • Moderators

    @sunil-nair So what was the question exactly ? Make the popup pop upwards ?



  • I apologise for the delay in response. The question was to make sure that the combobox always pops up in a given direction or position.


Log in to reply
 

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