Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Forcing uppercase only into QLineEdit
Forum Updated to NodeBB v4.3 + New Features

Forcing uppercase only into QLineEdit

Scheduled Pinned Locked Moved QML and Qt Quick
18 Posts 5 Posters 14.6k Views 1 Watching
  • 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.
  • C Offline
    C Offline
    crystal_ice
    wrote on last edited by
    #1

    How can i set a line edit (named textbox1) to uppercase only?

    I tried InputMethodHints:

    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    ui->textbox1->setMaxLength(50);
    ui->textbox1->setInputMethodHints(Qt::ImhUppercaseOnly);
    }
    @

    I tried it via the Design Mode as well, going to properties, expanding the InputMethodHints list and selecting ImhUppercaseonly checkbox.

    I also tried this:
    @
    void MainWindow::on_textbox1_textChanged(const QString &arg1)
    {
    QFont.setCapitalization(QFont::AllUppercase);
    }
    @

    and this:
    @
    QValidator::State pqUpcaseValidator::validate(QString& Text, int& Pos) const
    {
    Text=Text.Upper();
    return Acceptable;
    }
    @

    Any help would be appreciated. Thank you.

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Try this.

      @ QRegExp reg("[A-Z]{1,20}");
      QRegExpValidator val(reg,0);
      edit.setValidator(&val);@

      Helps you enter only 20 characters. It will only accept the capital letters.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • C Offline
        C Offline
        crystal_ice
        wrote on last edited by
        #3

        One error message says 'edit' wasn't declared in the scope.

        @
        void MainWindow::on_textbox1_textEdited(const QString &arg1)
        {
        QRegExp reg("[A-Z]{1,50}");
        QRegExpValidator val(reg,0);
        edit.setValidator(&val);
        }
        @

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          edit is object of QLineEdit. Please pass object of QLineEdit you have.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          0
          • C Offline
            C Offline
            crystal_ice
            wrote on last edited by
            #5

            No error message! But it still won't output capital letters.

            @
            void MainWindow::on_textbox1_textEdited(const QString &arg1)
            {
            QRegExp reg("[A-Z]{1,50}");
            QRegExpValidator val(reg,0);
            ui->textbox1->setValidator(&val);
            }
            @

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              You are setting the validator too late, do it in the constructor. Also, it will be destroyed at the end of on_textbox1_textEdited since it's allocated on the stack.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • C Offline
                C Offline
                crystal_ice
                wrote on last edited by
                #7

                Like this? It's still not working.

                @
                #include "mainwindow.h"
                #include "ui_mainwindow.h"

                MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
                {
                // setStyleSheet("background-image:url(':/step1/step2/step4.jpg');");
                ui->setupUi(this);

                QRegExp reg("[A-Z]{1,50}");
                QRegExpValidator val(reg,0);
                ui->textbox1->setValidator(&val);
                ui->textbox2->setValidator(&val);
                ui->textbox3->setValidator(&val);
                    
                ui->textbox1->setMaxLength(50);
                ui->textbox3->setMaxLength(40);
                ui->textbox4->setMaxLength(3);
                ui->textbox4->setValidator(new QIntValidator(0,999, this));
                

                }
                @

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  QRegExpValidor will still be destroyed at the end of the constructor.

                  Please have a look at it's usage in the documentation

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    Mortaneous
                    wrote on last edited by
                    #9

                    This is what I've been doing.
                    Using your example
                    @ void MainWindow::on_textbox1_textChanged(const QString &arg1)
                    {
                    // QFont.setCapitalization(QFont::AllUppercase);
                    ui->textbox1.setText(arg1.toUpper());
                    }
                    @

                    I was hoping there was a better way to do it. But it seems to me like there isn't.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      Mortaneous
                      wrote on last edited by
                      #10

                      This is what I've been doing.
                      Using your example
                      @ void MainWindow::on_textbox1_textChanged(const QString &arg1)
                      {
                      // QFont.setCapitalization(QFont::AllUppercase);
                      ui->textbox1.setText(arg1.toUpper());
                      }
                      @

                      I was hoping there was a better way to do it. But it seems to me like there isn't.

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        Hi and welcome to devnet,

                        Why not use QRegExpValidator ?

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          Hi and welcome to devnet,

                          Why not use QRegExpValidator ?

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            Mortaneous
                            wrote on last edited by
                            #13

                            Thank you for the warm welcome.

                            I do use QRegExpValidator quite frequently, but I feel like this restriction reduces usability. From my experience, a validator will force exact input only, and reject anything else. Unless I'm not using it correctly.

                            What I usually need is a qtextbox or qlineedit that accepts all input, then transforms the input to be valid and acceptable (eg. uppercase) and reflects the changes immediately while the user is typing. Hence the @SLOT (textChanged(QString))@.

                            Is there a better way of doing this?

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              Mortaneous
                              wrote on last edited by
                              #14

                              Thank you for the warm welcome.

                              I do use QRegExpValidator quite frequently, but I feel like this restriction reduces usability. From my experience, a validator will force exact input only, and reject anything else. Unless I'm not using it correctly.

                              What I usually need is a qtextbox or qlineedit that accepts all input, then transforms the input to be valid and acceptable (eg. uppercase) and reflects the changes immediately while the user is typing. Hence the @SLOT (textChanged(QString))@.

                              Is there a better way of doing this?

                              1 Reply Last reply
                              0
                              • C Offline
                                C Offline
                                crystal_ice
                                wrote on last edited by
                                #15

                                Try using this:

                                @ui->textbox1->setText(QString::number(whatever you need to output,16).toUpper());@

                                1 Reply Last reply
                                0
                                • C Offline
                                  C Offline
                                  crystal_ice
                                  wrote on last edited by
                                  #16

                                  Try using this:

                                  @ui->textbox1->setText(QString::number(whatever you need to output,16).toUpper());@

                                  1 Reply Last reply
                                  0
                                  • A Offline
                                    A Offline
                                    andre
                                    wrote on last edited by
                                    #17

                                    You can also start capturing and modifying your key events.

                                    1 Reply Last reply
                                    0
                                    • A Offline
                                      A Offline
                                      andre
                                      wrote on last edited by
                                      #18

                                      You can also start capturing and modifying your key events.

                                      1 Reply Last reply
                                      0

                                      • Login

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