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 restrict the user from entering alphabets in LineEdit
QtWS25 Last Chance

How to restrict the user from entering alphabets in LineEdit

Scheduled Pinned Locked Moved General and Desktop
8 Posts 6 Posters 4.6k 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.
  • I Offline
    I Offline
    Indrajeet
    wrote on last edited by
    #1

    Hi All

    I have one LineEdit I want to restrict the user to enter only numeric values in that line edit how to do this?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      KA51O
      wrote on last edited by
      #2

      install an EventFilter in which you check if the event is coming from the lineEdit. If this is true check for every key press event if the pressed key is not part of the alphabet, and if this is true you forward the event to the parents eventFilter.

      like so:

      @
      bool YourWidgetClass::eventFilter(QObject* obj, QEvent* event)
      {
      if(obj == yourLineEdit)
      {
      QKeyEvent* keyEvent;
      switch(event->type())
      {
      case QEvent::KeyEvent: keyEvent = dynamic_cast<QKeyEvent*>(event);
      if(keyEvent->key() == (Qt::Key_1 || Qt::Key_2)
      {
      return QObject::eventFilter(obj, event);
      }
      break;

         default:              break;
      

      }
      }
      }
      @

      beware this code will most likely not compile its just pseudo code to help you get the idea.

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

        The text can be arbitrarily constrained using a "validator":http://doc.qt.nokia.com/stable/qvalidator.html or an "inputMask":http://doc.qt.nokia.com/stable/qlineedit.html#inputMask-prop

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          [quote author="cincirin" date="1319102685"]The text can be arbitrarily constrained using a "validator":http://doc.qt.nokia.com/stable/qvalidator.html or an "inputMask":http://doc.qt.nokia.com/stable/qlineedit.html#inputMask-prop[/quote]

          Which should be the preferred way. There already exist validators for that

          • "QIntValidator":http://doc.qt.nokia.com/stable/qintvalidator.html
          • "QDoubleValidator":http://doc.qt.nokia.com/stable/qdoublevalidator.html

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • I Offline
            I Offline
            Indrajeet
            wrote on last edited by
            #5

            Hi

            I am calling a function in my constructor like this

            mylineedit->installEventFilter();

            installEventFilter(QObject*) takes QObject* as a parameter in my case what should i pass as argument.

            1 Reply Last reply
            0
            • R Offline
              R Offline
              Rahul Das
              wrote on last edited by
              #6

              @ mylineEdit->setValidator(new QDoubleValidator(mylineEdit));

              mylineEdit->setValidator(new QIntValidator(mylineEdit));@
              should do..


              Declaration of (Platform) independence.

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

                [quote author="Rajveer" date="1320815229"]Hi

                I am calling a function in my constructor like this

                mylineedit->installEventFilter();

                installEventFilter(QObject*) takes QObject* as a parameter in my case what should i pass as argument.

                [/quote]
                Whatever QObject derived class you reimplemented the eventFilter() method in, of course. But, why doesn't the QValidator based solution satisfy you?

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  KA51O
                  wrote on last edited by
                  #8

                  I think its related to "this":http://developer.qt.nokia.com/forums/viewthread/11376/ thread Andre.

                  We're in a loop. ^^

                  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