The QLineEdit control appears in a user interface when a word or single line of text should be entered by the user. It is common for the entry to be a word which starts with the same letter. This example will show how to implement a completer which offers a list of predefined words for auto completion.
#include <QtCore>
#include <QtGui>
class MainWindow : public QWidget
{
public:
MainWindow();
private:
QCompleter *strCompleter;
};
This declaration for MainWindow contains one data member which points to a QCompleter object. We will use this to configure a QLineEdit to implement auto completion.
MainWindow::MainWindow()
{
setMinimumSize(500, 150);
QStringList list = {
"Custard", "Carrot Cake", "Cheesecake",
"Cinnamon Rolls", "Chocolate Cake",
"Chocolate Chip Cookies", "Cherry Pie", "Cannoli",
"Coffee Cake", "Coconut Cream Pie", "Churros",
"Cranberry Muffins", "Cream Puffs" };
std::sort(list.begin(), list.end());
//
strCompleter = new QCompleter(list, this);
strCompleter->setCaseSensitivity(Qt::CaseInsensitive);
//
QLabel *label_1 = new QLabel("Dessert Name:");
QLineEdit *text_1 = new QLineEdit();
text_1->setText("");
text_1->setPlaceholderText(
"Enter a dessert name starting with C...");
text_1->setCompleter(strCompleter);
QPushButton *pb_1 = new QPushButton();
pb_1->setText("Close");
QHBoxLayout *layout1 = new QHBoxLayout();
layout1->addWidget(label_1);
layout1->addWidget(text_1);
QHBoxLayout *layout2 = new QHBoxLayout();
layout2->addStretch();
layout2->addWidget(pb_1);
layout2->addStretch();
QVBoxLayout *layoutMain = new QVBoxLayout(this);
layoutMain->setContentsMargins(75, 35, 75, 15);
layoutMain->addLayout(layout1);
layoutMain->addStretch();
layoutMain->addLayout(layout2);
connect(pb_1, &QPushButton::clicked,
this, &QWidget::close);
}
The code on line 5 initializes a list of strings which all begin with the letter C. When a user starts typing a recipe name which starts with the letter C, the QCompleter object will display a drop down list containing the various words which match. If you type “Ch” then the list will contain the five words which meet this criteria. Typing “che” will match two recipe names.
Keep in mind, the list could have contained any number of words and there is no requirement that they all start with the same letter.
Line 12 calls std::sort() to put the elements of the list in alphabetic order. This is not required but it does improve the user interface. On line 16 we call setCaseSensitivity() with an argument of insensitive, so typing upper or lower case will not affect the auto completion.
The call to setPlaceholderText() on line 23 adds a hint for the user but it is not required. When we call setCompleter() on line 25 the strCompleter object must be passed to the line edit. This is all it takes to set up auto completion.
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_17”.