Results 1 to 8 of 8

Thread: QLineEdit and input mask

  1. #1
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QLineEdit and input mask

    Hi,

    I am making a QlineEdit in Qt designer. Within the designer, I am using an input mask of "99999" (which designer adds a ";" character to the end of by itself) so that it forces the user to enter a zip code with only numbers. The input mask works perfectly, except for one thing: when I compile the program, run it, and click on the field that I put the mask on, the cursor always starts at the end of the field, as if there were 5 space characters inserted in the field from the start. If I use backspace or delete, I can delete these space characters and then type in the field, and the input mask works. If I take off the input mask, the cursor starts at the correct position, but then I can not control what the user enters.

    Is there any way to have the input mask present but not have it put these extra space characters in the field?

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit and input mask

    Call QLineEdit::setText("") in the constructor of your dialog.
    This will clear the spaces and it will keep the input mask.

  3. #3
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit and input mask

    I tried what you suggested:

    Qt Code:
    1. ProgramMain::ProgramMain() {
    2. ui.setupUi(this);
    3. ui.zipField->setText("");
    4.  
    5. readSettings(); //read previously saved QSettings
    6.  
    7. //connect form item signals to slots that arent in the UI file here:
    8. connect( ui.execButton, SIGNAL( clicked() ), this, SLOT( popupLogin() ) );
    9. connect(ui.dataSrc, SIGNAL(activated(int)), ui.srcStack, SLOT(setCurrentIndex(int)));
    10. connect(ui.dataDest, SIGNAL(activated(int)), ui.destStack, SLOT(setCurrentIndex(int)));
    11. connect(ui.csvDestbrowse, SIGNAL( clicked() ), this, SLOT( setOpenFileName() ) );
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 

    However the same thing happens, when I go to the field, there are still 5 spaces in it and the cursor starts on the right side. Also, the readSettings(); does not load anything into the zip field, so I know that isn't the problem. Also, although it appears that there are 5 spaces and I can "delete" them, they might not be actual spaces, since I can use the back arrow key to go to the left hand side and begin typing even though the field is limited to 5 characters. It might be that the cursor is set to start at the right side of the field, however "layout direction" is set to "left to right" and "cursor position" is set to "0" in my ui.

    --- update---

    I also found that if i "tab" to the zip field, the cursor does end up on the left side like its supposed to. So this only happens when I click in the field with the mouse.
    Last edited by tpf80; 17th May 2007 at 18:59. Reason: to add other info

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit and input mask

    Strange.
    Try setCursorPosition in code.
    Maybe it works.

  5. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit and input mask

    The mask consists of a string of mask characters and separators, optionally followed by a semicolon and the character used for blanks: the blank characters are always removed from the text after editing. The default blank character is space.
    This is why. So try moving the cursor to the left.

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

    tpf80 (17th May 2007)

  7. #6
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit and input mask

    It only seems to happen when I click on the field with the mouse and there is nothing in the field. If I have at least 1 character in the field then the cursor will be exactly to the right of it, but if I have none when I click, the cursor will be 5 spaces to the right. Also If I tab to the field when its empty it correctly positions the cursor to the left.

    This issue seems to only happen when there is an input mask, my field is empty, and I click with the mouse.
    Last edited by tpf80; 17th May 2007 at 20:12. Reason: accidently typed validator instead of input mask

  8. #7
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit and input mask

    I see whats happening now, when I put the mask as "99999;_" I could see the "_" character where the spaces were, so when theres an input mask, it positions the cursor wherever you click on the field, rather than at the right as if it was an empty field.

    So I will try a validator on the field instead.

  9. #8
    Join Date
    May 2014
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QLineEdit and input mask

    Might be a late answer, but
    Qt Code:
    1. lineEdit->setCursorPosition(0)
    To copy to clipboard, switch view to plain text mode 
    worked fine for me.

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.