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 prevent space bar event from the QLineEdit
Forum Update on Monday, May 27th 2025

How to prevent space bar event from the QLineEdit

Scheduled Pinned Locked Moved Unsolved General and Desktop
29 Posts 4 Posters 5.1k 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.
  • ManiRonM Offline
    ManiRonM Offline
    ManiRon
    wrote on last edited by ManiRon
    #1

    I am creating a lineedit which accepts Ip address , I have set input mask for the line edit as,

    ui->lineEdit->setInputMask("000.000.000.000");
    

    Now problem is that in the line edit it accepts the space bar also , How to prevent the line edit from not accepting space key press event ?

    jsulmJ 1 Reply Last reply
    0
    • ManiRonM ManiRon

      I am creating a lineedit which accepts Ip address , I have set input mask for the line edit as,

      ui->lineEdit->setInputMask("000.000.000.000");
      

      Now problem is that in the line edit it accepts the space bar also , How to prevent the line edit from not accepting space key press event ?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @maniron See documentation(https://doc.qt.io/qt-5/qlineedit.html#inputMask-prop):
      "A space character, the default character for a blank, is needed for cases where a character is permitted but not required."
      You can use 9 instead of 0, but then you will need to fill with 0 like: 127.000.000.001

      Another possibility is https://doc.qt.io/qt-5/qlineedit.html#setValidator and https://doc.qt.io/qt-5/qregularexpressionvalidator.html

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

      ManiRonM 1 Reply Last reply
      3
      • jsulmJ jsulm

        @maniron See documentation(https://doc.qt.io/qt-5/qlineedit.html#inputMask-prop):
        "A space character, the default character for a blank, is needed for cases where a character is permitted but not required."
        You can use 9 instead of 0, but then you will need to fill with 0 like: 127.000.000.001

        Another possibility is https://doc.qt.io/qt-5/qlineedit.html#setValidator and https://doc.qt.io/qt-5/qregularexpressionvalidator.html

        ManiRonM Offline
        ManiRonM Offline
        ManiRon
        wrote on last edited by
        #3

        @jsulm Any sample example will be availabe ?

        jsulmJ 1 Reply Last reply
        0
        • ManiRonM ManiRon

          @jsulm Any sample example will be availabe ?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @maniron Example for what exactly?

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

          ManiRonM 1 Reply Last reply
          0
          • jsulmJ jsulm

            @maniron Example for what exactly?

            ManiRonM Offline
            ManiRonM Offline
            ManiRon
            wrote on last edited by
            #5

            @jsulm I dont want space when spacce key is pressed , it should be rejected when space key is pressed in lineedit

            jsulmJ 1 Reply Last reply
            0
            • ManiRonM ManiRon

              @jsulm I dont want space when spacce key is pressed , it should be rejected when space key is pressed in lineedit

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @maniron I already suggested to use a validator (setValidator) based on regular expression.

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

              ManiRonM 2 Replies Last reply
              1
              • jsulmJ jsulm

                @maniron I already suggested to use a validator (setValidator) based on regular expression.

                ManiRonM Offline
                ManiRonM Offline
                ManiRon
                wrote on last edited by
                #7

                @jsulm ok i will see through it

                1 Reply Last reply
                0
                • jsulmJ jsulm

                  @maniron I already suggested to use a validator (setValidator) based on regular expression.

                  ManiRonM Offline
                  ManiRonM Offline
                  ManiRon
                  wrote on last edited by ManiRon
                  #8

                  @jsulm I tried to understand QRegExpValidator But donno whats wrong in this code

                     QRegExp rx("[0-9]\\d{1,255}");
                  // the validator treats the regexp as "^[1-9]\\d{0,3}$"
                  QRegExpValidator *ipValidator = new QRegExpValidator(rx, this);
                  //QRegExpValidator v(rx, 0);
                  
                  
                  ui->lineEdit->setValidator(ipValidator);
                  

                  Can any one help me out in understanding the QRegExp . If I set this validator , Its not accepting any numbers or letters in the lineedit

                  jsulmJ 1 Reply Last reply
                  1
                  • ManiRonM ManiRon

                    @jsulm I tried to understand QRegExpValidator But donno whats wrong in this code

                       QRegExp rx("[0-9]\\d{1,255}");
                    // the validator treats the regexp as "^[1-9]\\d{0,3}$"
                    QRegExpValidator *ipValidator = new QRegExpValidator(rx, this);
                    //QRegExpValidator v(rx, 0);
                    
                    
                    ui->lineEdit->setValidator(ipValidator);
                    

                    Can any one help me out in understanding the QRegExp . If I set this validator , Its not accepting any numbers or letters in the lineedit

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @maniron said in How to prevent space bar event from the QLineEdit:

                    [0-9]\d{1,255}

                    This is wrong: {1,255} means between one and 255 occurrences of the character before.
                    This is your reg exp:

                    ([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])
                    

                    Take a look at
                    http://gamon.webfactional.com/regexnumericrangegenerator/
                    https://regex101.com

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

                    ManiRonM 1 Reply Last reply
                    3
                    • jsulmJ jsulm

                      @maniron said in How to prevent space bar event from the QLineEdit:

                      [0-9]\d{1,255}

                      This is wrong: {1,255} means between one and 255 occurrences of the character before.
                      This is your reg exp:

                      ([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])
                      

                      Take a look at
                      http://gamon.webfactional.com/regexnumericrangegenerator/
                      https://regex101.com

                      ManiRonM Offline
                      ManiRonM Offline
                      ManiRon
                      wrote on last edited by ManiRon
                      #10

                      @jsulm

                        QRegExp rx("([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])");
                      

                      Is this right ? cause same problem arises the line edit is not accepting any numbers

                      jsulmJ 1 Reply Last reply
                      0
                      • ManiRonM ManiRon

                        @jsulm

                          QRegExp rx("([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])");
                        

                        Is this right ? cause same problem arises the line edit is not accepting any numbers

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @maniron You know that you have to escape special characters in C++ string literals?
                        If you have C++11 compiler you can simply do

                        R"(([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))"
                        

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

                        ManiRonM 1 Reply Last reply
                        1
                        • jsulmJ jsulm

                          @maniron You know that you have to escape special characters in C++ string literals?
                          If you have C++11 compiler you can simply do

                          R"(([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))"
                          
                          ManiRonM Offline
                          ManiRonM Offline
                          ManiRon
                          wrote on last edited by
                          #12

                          @jsulm I am using QT 5.5.0

                          JonBJ 1 Reply Last reply
                          0
                          • ManiRonM ManiRon

                            @jsulm I am using QT 5.5.0

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #13

                            @maniron
                            You have to tell @jsulm which compiler version you are using, not which Qt version.

                            ManiRonM 1 Reply Last reply
                            3
                            • mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              Hi
                              try to add
                              CONFIG += c++11
                              to your .pro file and run qmake
                              it should work.
                              Then you can use the handy R" syntax and avoid
                              having to escape the exp string.

                              1 Reply Last reply
                              0
                              • JonBJ JonB

                                @maniron
                                You have to tell @jsulm which compiler version you are using, not which Qt version.

                                ManiRonM Offline
                                ManiRonM Offline
                                ManiRon
                                wrote on last edited by
                                #15

                                @jonb it doesnt show any co mpiler version

                                JonBJ jsulmJ 2 Replies Last reply
                                0
                                • ManiRonM ManiRon

                                  @jonb it doesnt show any co mpiler version

                                  JonBJ Offline
                                  JonBJ Offline
                                  JonB
                                  wrote on last edited by JonB
                                  #16

                                  @maniron
                                  What doesn't show any compiler version? If you are developing with Qt you should know which compiler you chose and what version it is at, that was your choice, not Qt's.

                                  1 Reply Last reply
                                  0
                                  • ManiRonM ManiRon

                                    @jonb it doesnt show any co mpiler version

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    @maniron The compiler is configured in the Kit.
                                    But actually you could simply copy what I posted and see whether it works...

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

                                    ManiRonM 1 Reply Last reply
                                    2
                                    • jsulmJ jsulm

                                      @maniron The compiler is configured in the Kit.
                                      But actually you could simply copy what I posted and see whether it works...

                                      ManiRonM Offline
                                      ManiRonM Offline
                                      ManiRon
                                      wrote on last edited by
                                      #18

                                      @jsulm
                                      I tried adding this but still the line edit is not getting any input
                                      ui->lineEdit->setInputMask("000.000.000.000");

                                        QRegExp rx(R"(([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))");
                                      // the validator treats the regexp as "^[1-9]\\d{0,3}$"
                                      QRegExpValidator *ipValidator = new QRegExpValidator(rx, this);
                                      ui->lineEdit->setValidator(ipValidator);
                                      
                                      jsulmJ 1 Reply Last reply
                                      0
                                      • ManiRonM ManiRon

                                        @jsulm
                                        I tried adding this but still the line edit is not getting any input
                                        ui->lineEdit->setInputMask("000.000.000.000");

                                          QRegExp rx(R"(([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))");
                                        // the validator treats the regexp as "^[1-9]\\d{0,3}$"
                                        QRegExpValidator *ipValidator = new QRegExpValidator(rx, this);
                                        ui->lineEdit->setValidator(ipValidator);
                                        
                                        jsulmJ Offline
                                        jsulmJ Offline
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #19

                                        @maniron said in How to prevent space bar event from the QLineEdit:

                                        line edit is not getting any input

                                        Do you mean you can't input anything?

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

                                        ManiRonM 1 Reply Last reply
                                        0
                                        • jsulmJ jsulm

                                          @maniron said in How to prevent space bar event from the QLineEdit:

                                          line edit is not getting any input

                                          Do you mean you can't input anything?

                                          ManiRonM Offline
                                          ManiRonM Offline
                                          ManiRon
                                          wrote on last edited by
                                          #20

                                          @jsulm yes

                                          jsulmJ 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