Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

    Hi
    If you only want the case to be uppercase when searching could you not just use
    https://doc.qt.io/qt-5/qstring.html#toUpper
    ?



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

    https://doc.qt.io/qt-5/qstring.html#toUpper

    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++):

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

    You'll have to transform to Python, though.

    Regards



  • 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{
        Q_DISABLE_COPY(UpperValidator)
    public:
        using QValidator::QValidator;
        QValidator::State validate(QString &input, int &pos) const override{
            Q_UNUSED(pos)
            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));
        wid.show();
        return app.exec();
    }
    

    or reimplementing the event

    class UpperEdit : public QLineEdit{
        Q_DISABLE_COPY(UpperEdit)
    public:
        using QLineEdit::QLineEdit;
    protected:
        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());
            QLineEdit::keyPressEvent(&subEvent);
            if(subEvent.isAccepted())
                event->accept();
        }
    };
    

Log in to reply