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?
Forum Updated to NodeBB v4.3 + New Features

How to make lineEdit only accept string?

Scheduled Pinned Locked Moved Unsolved General and Desktop
lineeditline editqregularexpress
7 Posts 3 Posters 783 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.
  • tjktak1002T Offline
    tjktak1002T Offline
    tjktak1002
    wrote on 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!

    jsulmJ 1 Reply Last reply
    0
    • tjktak1002T tjktak1002

      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!

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 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
      • tjktak1002T Offline
        tjktak1002T Offline
        tjktak1002
        wrote on 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);
        
        Christian EhrlicherC 1 Reply Last reply
        0
        • tjktak1002T tjktak1002

          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);
          
          Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 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

          tjktak1002T 1 Reply Last reply
          1
          • Christian EhrlicherC Christian Ehrlicher

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

            all alphabet characters

            My alphabet has umlauts... -> \w

            tjktak1002T Offline
            tjktak1002T Offline
            tjktak1002
            wrote on 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À-ÿ-. ]*$)"));
            
            Christian EhrlicherC 1 Reply Last reply
            0
            • tjktak1002T tjktak1002

              @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À-ÿ-. ]*$)"));
              
              Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 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

              tjktak1002T 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

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

                tjktak1002T Offline
                tjktak1002T Offline
                tjktak1002
                wrote on last edited by
                #7

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

                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