Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-63956

QComboBox does not update popup size when it is visible and amount of items changes

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P3: Somewhat important
    • None
    • 5.9.1
    • None

    Description

      By default QComboBox adjust popup size to amount of items to be shown, allowing to decrease size of the popup when there are only few items.
      But it doesn't work when amount changes while popup is already visible, showing extra scrollbars might be not that bad (unless it had like only one entry and we end up with small popup with scrollbars) but keeping plenty of empty space after removing most of the items looks very bad, we end up with large blank widget (at least in Fusion and GTK styles).

      Changing default behaviour where popup rectangle is updated only on show might be a bad idea, but there could be a new option similar to sizeAdjustPolicy() (bool might be enough, but it can be more sophisticated too, allowing it just to grow or just to shrink, both or do nothing) or at least exposing method allowing to force size update (or let to use adjustSize() to do that) that can also move popup if needed (according to docs GTK and macOS will require that due to custom handling of maximum amount of visible items).

      Note that use of setMaxVisibleItems() has no effect once popup was shown.

      Example code, buttons allow to add or remove items to one of the comboboxes:

      1. use Remove[...] button;
      2. use the second combobox to show popup;
      3. wait 5 seconds to observe oversized popup showing just five items;
      4. hide popup;
      5. use Add[...] button;
      6. wait 5 seconds to observe popup showing just five items while it would show more without dynamic change (compare with first combobox).
      #include <QtCore/QTimer>
      #include <QtWidgets/QApplication>
      #include <QtWidgets/QBoxLayout>
      #include <QtWidgets/QComboBox>
      #include <QtWidgets/QPushButton>
      
      int main(int argc, char *argv[])
      {
      	QApplication application(argc, argv);
      	QWidget widget;
      	QComboBox constantComboBox(&widget);
      	QComboBox dynamicComboBox(&widget);
      	QPushButton shrinkButton("Remove items in 5 seconds", &widget);
      	QPushButton growButton("Add items in 5 seconds", &widget);
      	QBoxLayout layout(QBoxLayout::TopToBottom, &widget);
      	layout.addWidget(&constantComboBox);
      	layout.addWidget(&dynamicComboBox);
      	layout.addWidget(&shrinkButton);
      	layout.addWidget(&growButton);
      
      	QStringList texts;
      	texts.reserve(99);
      
      	for (int i = 1; i < 100; ++i)
      	{
      		texts.append(QString::number(i));
      	}
      
      	constantComboBox.addItem("Constant amount of items");
      	constantComboBox.addItems(texts);
      
      	dynamicComboBox.addItem("Dynamic amount of items");
      	dynamicComboBox.addItems(texts);
      
      	widget.show();
      
      	QObject::connect(&shrinkButton, &QPushButton::clicked, [&]()
      	{
      		QTimer::singleShot(5000, [&]()
      		{
      			for (int i = dynamicComboBox.count(); i > 5; --i)
      			{
      				dynamicComboBox.removeItem(i);
      			}
      		});
      	});
      
      	QObject::connect(&growButton, &QPushButton::clicked, [&]()
      	{
      		QTimer::singleShot(5000, [&]()
      		{
      			dynamicComboBox.addItems(texts);
      		});
      	});
      
      	return application.exec();
      }
      

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            richard Richard Moe Gustavsen
            emdek MichaƂ Dutkiewicz
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes