ariya.io About Talks Articles

Resize Pop-up List of A Combo Box

2 min read

You want to be able to resize the pop-up list of a combo box. Normally, this pop-up list has always the same width as the combo box. However, in certain cases the width of the combo box needs to be fixed. But if the pop-up list does not expand itself, then only part of the items on the list is visible as illustrated here:

In this particular situation, the look of the pop-up list will be better if it can be resized independently from the combo box itself.

QComboBox unfortunately does not provide public functions to access its pop-up list. Also, the pop-up list widget itself is not accessible. It is possible to create a subclass of QComboBox and reuse its private members (in header file qcombobox_p.h), but this would have required a quite amount of effort.

By looking at the source of QComboBox, we find out that the pop-up widget is QComboBoxPrivateContainer. It is also constructed as a child widget of the combo box. Using run-time introspection feature of Qt, it is very easy to find this widget and change its size:

void resizeComboBoxPopup(QComboBox* comboBox, int width, int height)
{
  if(!comboBox) return;
  
  // force the construction of QComboBoxViewContainer
  comboBox->view();  

  // iterate to find QComboBoxPrivateContainer
  QWidget* popup = 0;
  QObjectList objects = comboBox->children();
  for(int i = 0; i < objects.size(); ++i)
  {
      QObject* obj = objects.at(i);
      if(obj->inherits("QComboBoxPrivateContainer")) 
          popup = qobject_cast<qwidget>(obj);
  }
  
  if(popup)
    popup->resize(width, height);
}    
</qwidget>

Of course, this cheap trick is only a workaround and only works as long as the implementation of QComboBox does not change too much. It can also be considered as a crime to Qt. So, kids, don’t try this at home…

♡ this article? Explore more articles and follow me Twitter.

Share this on Twitter Facebook