Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Trying to make line edit text to uppercase always

    General and Desktop
    5
    7
    1836
    Loading More Posts
    • 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
      WillyZ last edited by

      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 Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

        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 Reply Quote 4
        • W
          WillyZ last edited by

          @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_1980 1 Reply Last reply Reply Quote 0
          • aha_1980
            aha_1980 Lifetime Qt Champion @WillyZ last edited by

            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.

            VRonin 1 Reply Last reply Reply Quote 3
            • R
              Rondog last edited by Rondog

              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 Reply Quote 0
              • W
                WillyZ last edited by

                thank you s much, Guys :)

                1 Reply Last reply Reply Quote 0
                • VRonin
                  VRonin @aha_1980 last edited by

                  @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 Reply Quote 2
                  • First post
                    Last post