Results 1 to 7 of 7

Thread: validating input to QLineEdit while seting it through "setText()" method.

  1. #1
    Join Date
    Feb 2010
    Posts
    20
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default validating input to QLineEdit while seting it through "setText()" method.

    Hi,

    I have set a validator as below for a QLineEdit, portLineedit.

    QIntValidator* intValidator = new QIntValidator(0, 9999, this);
    portLineedit->setValidator(intValidator);

    I am setting the text in portLineedit using as QLineEdit::setText() as "Port" from a different function. Now when the portLineedit line edit is being enabled it contains the text "Port". Now I want to modify the text, the validation is not working I am being able to write letters other then integers also and to any number of letters.

    The validation is working only when I am deleting the entire content of portLineedit first.

    Is it the standard behavior? If not how to make this validation work?


    Thanks and regards,

  2. #2
    Join Date
    Jul 2007
    Location
    Jundiai/SP, Brazil
    Posts
    114
    Thanks
    5
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: validating input to QLineEdit while seting it through "setText()" method.

    Hi,

    QIntValidator only permits that you type integer numbers.
    If you needs join letter and number perhaps you should use inputMask. setInputMask().
    See Qt Assistant for more details.

  3. #3
    Join Date
    Feb 2010
    Posts
    20
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: validating input to QLineEdit while seting it through "setText()" method.

    Hi,

    I want to input integer only. I dont want charcter. But the problem is that after setting the QIntValidator also its taking character input.

    I think the problem is due to the value I m setting though setText(). I am setting "Port"
    Qt Code:
    1. port_lineedit->setText("Port");
    To copy to clipboard, switch view to plain text mode 
    .
    So probably its spoiling the validator. But the validation is working once i delete all the content of the line edit.

    Can any body please provide an idea to solve such problem? For being clear I am explaning the problem once more.
    "I want a line edit to take only numeric input from the user. but when its disable or empty i want it to display the string"Port".(which i m setting through setText()). I have set QIntValidator validator for it. But its not working for the first time I am going to edit port. I am being able to type numbers,character,whitespace all. this is the problem need to be solved."

    Can we do it combining using inputMask and validator? If yes, how?

    Thanks and Regards,
    ~netmat

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: validating input to QLineEdit while seting it through "setText()" method.

    May be if you inherit line edit, you might be able to do all that you want.
    on key press event you can check whether to accept it or not... and on focus out and focus in events you can toggle the "Port" text.

  5. #5
    Join Date
    Nov 2020
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Question Re: validating input to QLineEdit while seting it through "setText()" method.

    Hi I know it's been a while but did you ever get an answer to this question. I am running into exactly the same issue.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,787
    Thanks
    263
    Thanked 773 Times in 763 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: validating input to QLineEdit while seting it through "setText()" method.

    The input mask and validator are used -only- when the user is entering text into the line edit by typing or cutting and pasting. If you are calling setText() from inside your code, then the line edit will let you put anything you want into the edit box. The OP misunderstood this, I think.

    Anyway, why would you want to display text like that in a line edit when it is forbidden for the user to type the same text? If you are trying to describe what the line edit should be used for, then either implement a tool tip on it or put a label next to it.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,690
    Thanks
    13
    Thanked 1,599 Times in 1,527 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: validating input to QLineEdit while seting it through "setText()" method.

    Quote Originally Posted by d_stranz View Post
    Anyway, why would you want to display text like that in a line edit when it is forbidden for the user to type the same text? If you are trying to describe what the line edit should be used for, then either implement a tool tip on it or put a label next to it.
    The QLineEdit already has a the placeholderText property to provide an in-widget prompt without actually putting the text in the editor's text buffer (since Qt 4.7, Sept 2010).
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  8. The following user says thank you to ChrisW67 for this useful post:

    d_stranz (5th December 2020)

Similar Threads

  1. QPushButton - setText with "\n" not working
    By gboelter in forum Newbie
    Replies: 7
    Last Post: 10th February 2008, 04:27
  2. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 20:05
  3. Replies: 11
    Last Post: 10th July 2007, 16:58
  4. Replies: 6
    Last Post: 3rd November 2006, 12:53
  5. QHttp "PUT METHOD" QT Problem File Upload. RFC 2616
    By patrik08 in forum Qt Programming
    Replies: 7
    Last Post: 25th October 2006, 23:02

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.