Trying to make line edit text to uppercase always

  • I'm new in Qt so i created a line Edit to search with it in a DB. it works fine but i would like to make the text to uppercase. I found a older Topic but because i'm new i'm not sure about the syntax to do it i'm workin with PyQt 5.7.1 and python 3.7.6. I tried to do it with QValidator but i'm not sure how

  • Lifetime Qt Champion

    If you only want the case to be uppercase when searching could you not just use

  • @mrjj said in Trying to make line edit text to uppercase always:

    i want that the Line Edit input text be in captila letters, when the user write into it, the input always be in capital letters. before the search.

  • Lifetime Qt Champion

    Hi @WillyZ,

    you can do that by connecting to the textEdited signal (Example snippet in C++):

      connect(ui->lineEdit, &QLineEdit::textEdited, this, &onTextEdited);
    void MainWindow::onTextEdited(const QString &newText)

    You'll have to transform to Python, though.


  • You could use the uppercase version of the input text and not try to force (enforce) the user to only input upper case characters. From personal experience this can be really annoying for the user.

    What do you picture happening if the user enters a lower case letter? If nothing appears in the edit box this will be confusing for sure. You could offset this behaviour by some sort of help text (tooltip perhaps) that explains what is required but I would still go with the version that accepts mixed case text and converts to upper case.

  • thank you s much, Guys :)

  • @aha_1980 said in Trying to make line edit text to uppercase always:

    you can do that by connecting to the textEdited signal

    This basically disables typing in the middle of the linedit

    The best solution I can think of atm is using a validator:

    #include <QApplication>
    #include <QLineEdit>
    #include <QValidator>
    class UpperValidator : public QValidator{
        using QValidator::QValidator;
        QValidator::State validate(QString &input, int &pos) const override{
            for(const QChar& ch : qAsConst(input)){
                if(ch.isLetter() && ch.isLower())
                    return QValidator::Intermediate;
            return QValidator::Acceptable;
        void fixup(QString &input) const override{
             input = input.toUpper();
    int main(int argc, char *argv[])
        QApplication app(argc,argv);
        QLineEdit wid;
        wid.setValidator(new UpperValidator(&wid));;
        return app.exec();

    or reimplementing the event

    class UpperEdit : public QLineEdit{
        using QLineEdit::QLineEdit;
        void keyPressEvent(QKeyEvent *event) override{
            QKeyEvent subEvent(event->type(),event->key(),event->modifiers(),event->nativeScanCode(),event->nativeVirtualKey(),event->nativeModifiers(),event->text().toUpper(),event->isAutoRepeat(),event->count());

Log in to reply