How can I Increase the size of the Drop Down List of a QComboBox?
-
Hi..
I have a QComboBox, in that i add few items. (eg. item 1, item 2, combo box item 3, combo box item number 4, combo box item number is 5). Now i want to change the width size of the drop down list of the combo box that fits for the largest item string without changing the actual size of the QComboBox. How can i change the width size of the drop down list without changing the actual size of the QComboBox. Please give your suggestions.....
@#include <QtGui>
#include <QApplication>
#include <QComboBox>int main(int argc, char **argv)
{
QApplication app(argc, argv);
QComboBox cb;
cb.addItem("item 1");
cb.addItem("item 2");
cb.addItem("combo box item 3");
cb.addItem("combo box item number 4");
cb.addItem("combo box item number is 5");
cb.show();
return app.exec();
}@Thanks & Regards
-
Hi,
Did you try to modify the "sizeAdjustPolicy":http://qt-project.org/doc/qt-4.8/qcombobox.html#sizeAdjustPolicy-prop ?
Hope it helps
-
This seemed to be the default behavior on Qt4.8 (and maybe older?) (Windows).
If you had a QComboBox with a fixed size and added items which were longer than it, when the popup listview opened it would automatically resize to the longest string content.
I migrated apps from Qt4.8 to 5.0.2, and to my dismay, it does not work anymore.
What should work then was to set the QComboBox view()->sizePolicy()->horizontalPolicy() to QSizePolicy::MinimumExpanding, but doing that simply adjusts the popup listview to whatever is set on the view's MinimumWidth. It does not expand!
The default value for the horizontalPolicy() is already 13 (Ignore) which should expand too.
I've tried both doing it before and after adding the items, and also tried reimplementing the QComboBox showPopup() to set that when the popup was opening (and tried both before and after calling QComboBox::showPopup() in the reimplemented method):
@class ComboBox : public QComboBox
{
public:
ComboBox(QWidget*);
void showPopup();
};ComboBox::ComboBox(QWidget * p) : QComboBox(p)
{
}void ComboBox::showPopup()
{
QComboBox::showPopup();
QSizePolicy sp = view()->sizePolicy();
sp.setHorizontalPolicy(QSizePolicy::MinimumExpanding);
view()->setMinimumWidth(100); //change this to see that it alone sets the width
view()->setSizePolicy(sp);
}
@Now, is this a bug on Qt5 QComboBox (bugreports related to this are old and unresolved, although it worked on 4.8), or am I missing something?
Apart from possibly reimplementing showPopup() to check the items one by one and setting the MinimumWidth "by hand" is there a way to get the 4.8 functionality back?
tks
Joao S Veiga -
I'm having exactly the same problem as Joao Veiga above. I've tried all the proposed solutions, searched the web for new ideas, and no matter what I do the list size does not change!
Has this been reported as a bug? Is there a workaround?
Thank you,
Pedro Tabacof -
That happens to me too.
I have a defined a delegate, if i use it with a QListView it adjusts perfectly the size indicated on 'sizehint' but if I use the delegate on a 'QComboBox' the dropdown list does not match the size of the elements.
Does anyone know anything more? -
Having the same issue here. Looks like it might be this ticket. https://bugreports.qt.io/browse/QTBUG-3097
-
this is the best solution
combobox->setStyleSheet("QComboBox QAbstractItemView::item {min-width: 60px; }");