A widget which provides a defined list of values is referred to as a combo box and contains a line edit field and a drop down list. The idea of a combo box is to provide a graphical interface for users to select an item from a restricted set of options. The purpose of this example is to explain how to display a subset of the values based on some criteria.
#include <QtCore>
#include <QtGui>
class MainWindow : public QWidget
{
public:
MainWindow();
};
The declaration for MainWindow only needs a public constructor.
MainWindow::MainWindow()
{
setMinimumSize(400, 200);
QStringList wordList = {
"Blueberry Muffin", "Apple Strudel",
"Cherry Pie", "Cheesecake", "Apple Pie",
"Almond Croissants", "Blueberry Scones",
"Chocolate Mousse", "Lemon Tart", "Apricot Cobbler",
"Banana Bread", "Chocolate Cookies", "Almond Cookies",
"Chocolate Cake", "Macaroon Cookies"
};
QLabel *label = new QLabel();
label->setText("Pastries and Desserts:");
QComboBox *comboBox = new QComboBox();
comboBox->setEditable(true);
comboBox->setInsertPolicy(QComboBox::NoInsert);
comboBox->setMinimumSize(200, 0);
comboBox->addItems(wordList);
comboBox->model()->sort(0);
comboBox->setCurrentIndex(-1);
QCompleter *complete = new QCompleter(wordList, this);
complete->setCaseSensitivity(Qt::CaseInsensitive);
// complete->setFilterMode(Qt::MatchContains);
comboBox->setCompleter(complete);
QPushButton *close_pb = new QPushButton();
close_pb->setText("Close");
QHBoxLayout *layout = new QHBoxLayout();
layout->addWidget(label);
layout->addSpacing(20);
layout->addWidget(comboBox);
layout->addStretch();
QHBoxLayout *layout_cb = new QHBoxLayout();
layout_cb->addStretch();
layout_cb->addWidget(close_pb);
layout_cb->addStretch();
QVBoxLayout *layoutMain = new QVBoxLayout(this);
layoutMain->setContentsMargins(45, 40, 45, 25);
layoutMain->addLayout(layout);
layoutMain->addStretch();
layoutMain->addLayout(layout_cb);
connect(close_pb, &QPushButton::clicked,
this, &QWidget::close);
}
Our graphical interface displays a QLabel and a QComboBox. The values for the combo box are defined on lines 5 through 12 using list initialization. The following section explains how to limit or filter the drop down list to display only certain values.
The QComboBox is created on line 17. The next line of code calls setEditable(true) and configures the combo box so the user can enter text in the line edit. This method does not provide functionality for the user to add or edit values displayed in the drop down list.
The list of values for this combo box are added to the drop down list by calling addItems() as shown on line 22. Passing minus one on line 24 will set the line edit display of the combo box to an empty string.
On line 26 a QCompleter object is created and then passed to the QComboBox widget on line 30. The remaining code sets up the user interface.
How to Display a Subset of Items
When the application starts the combo box is empty. To view the entire drop down list click the down arrow at any time.
If you want to display items which begin with the letter B type either an upper or lower case “b” in the combo box. Three items will be displayed in the drop down list. As you type the remaining letters for the word “blueberry”, only the two items which start with the given text will be visible.
As another example, delete the current text and then type “ch”. The drop down list will show five items. If you change the combo box to “cho”, the drop down list will change again to display the three items which happen to begin with the word “chocolate”.
Important Information
For this code to work, you must call the setEditable() method before calling setCompleter(). The setEditable() method creates an internal widget which is used by the QCompleter object. If you call setCompleter() first, the internal widget will not exist and the completer is ignored and never used.
Match Contains
When you enable the commented out source code on line 28 the behavior of this example changes.
If you type “cookies” in the combo box the drop down list will show every item which contains the given text, no matter where the search string appears in the values. The items no longer need to start with the user entered text of “cookies”. For this example three items will be displayed.
Main Function
Since the source code for main() has not changed there is no need to show it again. Refer to example 3 or download the full source for this example.
Running the Example
To build and run this example use the same CMake build file and commands as we showed for the first example. The only suggested modification is on line 2 of the CMakeLists.txt file, change “example_1” to “example_45”.