Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Trying to make line edit text to uppercase always
QtWS25 Last Chance

Trying to make line edit text to uppercase always

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 5 Posters 3.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    WillyZ
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      ?

      1 Reply Last reply
      4
      • W Offline
        W Offline
        WillyZ
        wrote on last edited by
        #3

        @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.

        aha_1980A 1 Reply Last reply
        0
        • W WillyZ

          @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.

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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

          Qt has to stay free or it will die.

          VRoninV 1 Reply Last reply
          3
          • R Offline
            R Offline
            Rondog
            wrote on last edited by Rondog
            #5

            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.

            1 Reply Last reply
            0
            • W Offline
              W Offline
              WillyZ
              wrote on last edited by
              #6

              thank you s much, Guys :)

              1 Reply Last reply
              0
              • aha_1980A aha_1980

                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

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                @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();
                    }
                };
                

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                2

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved