topic logo
Qt ⇢ Unit 16

QLineEdit

completers and attributes

Summary

Shows how to enable auto-complete and the remaining attributes of the QLineEdit class.

Background

This unit wraps up the QLineEdit class by introducing it’s remaining features that are interesting and distinctive.

New Classes

  • QCompleter: Used to add auto-complete to an input widget. In it’s simple form, this class is initialized with a QStringList that is used to suggest the user completion options based on already entered text. There is an advanced initialization form that uses the Qt model-view-framework.

Example Application

The example application has a line edit and a set of controls to toggle or switch it’s attributes and apply a completer.

Toggle Attributes
Toggle Attributes
mainwindow.cpp - switching echo modes
connect(radNormal, &QRadioButton::toggled, [this](bool checked){
       if(checked){
           lineEdit->setEchoMode(QLineEdit::Normal);
       }
    });

[..]

The controls in this unit use very few lines of code and putting them in a separate slot each would just make the code unsightly. Instead, the slots are implemented as lambda functions that are declared right where the signals are connected.

The echo mode switch has four states: QLineEdit::Normal, QLineEdit::NoEcho, QLineEdit::Password and QLineEdit::PasswordEchoOnEdit. It is set with the setEchoMode() method.

Normal: The default behaviour. Text is shown normally as it is entered and stays visible afterwards. Useful for non-sensitive data entry.

NoEcho: The line edit shows no feedback of the entered text whatsoever. This is the most secure mode, as even the length of the input is hidden along with it’s value, but it is awkward to use.

Password: The entered characters are blacked out, but there is a visual feedback when they are entered. This is a user friendly way to avoid security breaches by shoulder surfing when entering passwords.

PasswordEchoOnEdit: The password is shown when entered, but is blacked out when the line edit looses focus. This is rarely used.

mainwindow.cpp - align text
connect(radLeft, &QRadioButton::toggled, [this](bool checked){
       if(checked){
           lineEdit->setAlignment(Qt::AlignLeft);
       }
    });

Text alignment is set with the setAlignment() method and has three possible values: Qt::AlignLeft, Qt::AlignCenter and Qt::AlignRight.

This works the same as introduced with the QLabel in unit 11.

mainwindow.cpp - set placeholder
connect(cbPlaceholder, &QCheckBox::toggled, [this](bool checked){
        if(checked){
            lineEdit->setPlaceholderText("Enter a fish..");
        } else {
            lineEdit->setPlaceholderText("");
        }
    });

A placeholder text in a line edit is shown when it is empty. It has a different style than normal input to make it stand apart. It’s enabled with the setPlaceholderText() method, that takes a string argument containing the placeholder text to be displayed. Passing an empty string removes the placeholder text.

mainwindow.cpp - read only and clear
connect(cbReadOnly, &QCheckBox::toggled, [this](bool checked){
        lineEdit->setReadOnly(checked);
    });

connect(cbClearEnabled, &QCheckBox::toggled, [this](bool checked){
        lineEdit->setClearButtonEnabled(checked);
    });

The readOnly and clearButtonEnabled attributes are boolean types and are simply toggled on/off. Read only disables the text editing in the line edit, while clearButtonEnabled shows an embedded button in the line edit that clears the line edit’s text when activated.

mainwindow.h/.cpp - auto-complete
QCheckBox *cbCompleter;
QCompleter *compFish;

[..]

QStringList fishies;
fishies << "salmon" << "catfish" << "cod" << "herring"
        << "tuna" << "shark";
compFish = new QCompleter(fishies, cbCompleter);
compFish->setCaseSensitivity(Qt::CaseInsensitive);

[..]

connect(cbCompleter, &QCheckBox::toggled, [this](bool checked){
    if(checked){
        lineEdit->setCompleter(compFish);
    } else {
        lineEdit->setCompleter(0);
    }
});

The QCompleter class is used to apply auto-complete to an input widget like the line edit. Here, it is initialized with a string list. It’s applied to the line edit with the setCompleter() method that takes a QCompleter pointer as argument. It is removed by passing it the value zero.

Conclusion

The QLineEdit class has been introduced in detail over four units, as it’s an essential input widget used in a multitude of scenarios. The units demonstrated it’s signals and slots, how to apply an input mask, validator and completer. Additionally a few specific attributes were explained. This unit concludes the QLineEdit class.