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. How to make lineEdit only accept string?
QtWS25 Last Chance

How to make lineEdit only accept string?

Scheduled Pinned Locked Moved Unsolved General and Desktop
lineeditline editqregularexpress
7 Posts 3 Posters 735 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.
  • T Offline
    T Offline
    tjktak1002
    wrote on 24 Jun 2022, 07:22 last edited by tjktak1002
    #1

    Hello everyone,
    I have been trying to make my line edits only accept alphabet characters(no numbers, no special characters) using QRegularExpression but I have not been succeed so far.
    This is my code:

    QRegularExpression re("[A-Z][a-z]");
    QRegularExpressionValidator v(re, this);
    ui->lineEditFirstName->setValidator(&v);
    ui->lineEditLastName->setValidator(&v);
    

    But somehow I can still type numbers and special characters in my line edit which is not what I want.
    Thank you for reading and have a good day!

    J 1 Reply Last reply 24 Jun 2022, 07:25
    0
    • T tjktak1002
      24 Jun 2022, 07:22

      Hello everyone,
      I have been trying to make my line edits only accept alphabet characters(no numbers, no special characters) using QRegularExpression but I have not been succeed so far.
      This is my code:

      QRegularExpression re("[A-Z][a-z]");
      QRegularExpressionValidator v(re, this);
      ui->lineEditFirstName->setValidator(&v);
      ui->lineEditLastName->setValidator(&v);
      

      But somehow I can still type numbers and special characters in my line edit which is not what I want.
      Thank you for reading and have a good day!

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 24 Jun 2022, 07:25 last edited by
      #2

      @tjktak1002 said in How to make lineEdit only accept string?:

      QRegularExpressionValidator v(re, this);

      Your validator is a local variable and is destroyed as soon as it gets out of scope...

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • T Offline
        T Offline
        tjktak1002
        wrote on 24 Jun 2022, 07:39 last edited by
        #3

        ok one solution to my problem that I have found (allow all alphabet characters and whitespace)

        QRegularExpressionValidator* v = new QRegularExpressionValidator(QRegularExpression(R"(^[a-zA-Z- ]*)"));
            ui->lineEditFirstName->setValidator(v);
            ui->lineEditLastName->setValidator(v);
        
        C 1 Reply Last reply 24 Jun 2022, 07:44
        0
        • T tjktak1002
          24 Jun 2022, 07:39

          ok one solution to my problem that I have found (allow all alphabet characters and whitespace)

          QRegularExpressionValidator* v = new QRegularExpressionValidator(QRegularExpression(R"(^[a-zA-Z- ]*)"));
              ui->lineEditFirstName->setValidator(v);
              ui->lineEditLastName->setValidator(v);
          
          C Online
          C Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 24 Jun 2022, 07:44 last edited by
          #4

          @tjktak1002 said in How to make lineEdit only accept string?:

          all alphabet characters

          My alphabet has umlauts... -> \w

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          T 1 Reply Last reply 24 Jun 2022, 07:49
          1
          • C Christian Ehrlicher
            24 Jun 2022, 07:44

            @tjktak1002 said in How to make lineEdit only accept string?:

            all alphabet characters

            My alphabet has umlauts... -> \w

            T Offline
            T Offline
            tjktak1002
            wrote on 24 Jun 2022, 07:49 last edited by
            #5

            @Christian-Ehrlicher said in How to make lineEdit only accept string?:

            umlauts.

            hey this one works for my french keyboard so maybe it can help you a bit

            QRegularExpressionValidator* v = new QRegularExpressionValidator(QRegularExpression(R"(^[a-zA-ZÀ-ÿ-. ]*$)"));
            
            C 1 Reply Last reply 24 Jun 2022, 07:57
            0
            • T tjktak1002
              24 Jun 2022, 07:49

              @Christian-Ehrlicher said in How to make lineEdit only accept string?:

              umlauts.

              hey this one works for my french keyboard so maybe it can help you a bit

              QRegularExpressionValidator* v = new QRegularExpressionValidator(QRegularExpression(R"(^[a-zA-ZÀ-ÿ-. ]*$)"));
              
              C Online
              C Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 24 Jun 2022, 07:57 last edited by
              #6

              @tjktak1002 said in How to make lineEdit only accept string?:

              hey this one works for my french keyboard so maybe it can help you a bit

              I know what to do - I just wanted to tell you that your regex doesn't accept all characters but only a few.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              T 1 Reply Last reply 24 Jun 2022, 08:01
              0
              • C Christian Ehrlicher
                24 Jun 2022, 07:57

                @tjktak1002 said in How to make lineEdit only accept string?:

                hey this one works for my french keyboard so maybe it can help you a bit

                I know what to do - I just wanted to tell you that your regex doesn't accept all characters but only a few.

                T Offline
                T Offline
                tjktak1002
                wrote on 24 Jun 2022, 08:01 last edited by
                #7

                @Christian-Ehrlicher oh ok haha :D Thanks for reminding me tho

                1 Reply Last reply
                0

                6/7

                24 Jun 2022, 07:57

                • Login

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