Results 1 to 5 of 5

Thread: mask line edit

  1. #1
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default mask line edit

    Hi,

    I was wondering if it is possible to have a line edit which only allows you to enter only maximum of 4 hex digits and in the range 0 - FFFF. I know this can be done with a QValidator, but this seems to be only after you have entered the data in the line edit. For instance, I could type in FEFEFEFEFE and it would only be when I press enter or something that I can validate the data. Do I need to override the keyPressEvent and subclass QLineEdit or just simply use setInputMask?

    Regards,
    Steve

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: mask line edit

    Hi, QLineEdit::inputMask should be sufficient:
    H Hexadecimal character required. A-F, a-f, 0-9.
    h Hexadecimal character permitted but not required.
    So try "Hhhh;"
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    steg90 (3rd July 2007)

  4. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: mask line edit

    Hmm, actually there is one problem with input masks (at least with the aforementioned one). User is able to enter for example "F FF" (notice the space in between). So QRegExpValidator might actually be a better choice. Try something like this:
    Qt Code:
    1. QRegExp rx("(0(x|X))?(\\d|[a-f]|[A-F]){1,4}");
    2. QValidator* validator = new QRegExpValidator(rx, lineEdit);
    3. lineEdit->setValidator(validator);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: mask line edit

    You can use QwwLineEdit from wwWidgets which allows entering a regexp for a validator as a property of the widget. This way you can save a few lines of code

  6. The following user says thank you to wysota for this useful post:

    steg90 (3rd July 2007)

  7. #5
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: mask line edit

    Many thanks.

    JPN - that works a treat

Similar Threads

  1. Slot to color background of line edit on textedited
    By tpf80 in forum Qt Programming
    Replies: 5
    Last Post: 21st June 2007, 09:02
  2. Replies: 8
    Last Post: 15th May 2007, 09:21
  3. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  4. qt 4.2.2 install in tru64 cxx
    By try to remember in forum Installation and Deployment
    Replies: 0
    Last Post: 30th March 2007, 07:43
  5. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42

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.