PDA

View Full Version : QLineEdit and input mask



tpf80
17th May 2007, 18:12
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?

marcel
17th May 2007, 18:18
Call QLineEdit::setText("") in the constructor of your dialog.
This will clear the spaces and it will keep the input mask.

tpf80
17th May 2007, 18:55
I tried what you suggested:


ProgramMain::ProgramMain() {
ui.setupUi(this);
ui.zipField->setText("");

readSettings(); //read previously saved QSettings

//connect form item signals to slots that arent in the UI file here:
connect( ui.execButton, SIGNAL( clicked() ), this, SLOT( popupLogin() ) );
connect(ui.dataSrc, SIGNAL(activated(int)), ui.srcStack, SLOT(setCurrentIndex(int)));
connect(ui.dataDest, SIGNAL(activated(int)), ui.destStack, SLOT(setCurrentIndex(int)));
connect(ui.csvDestbrowse, SIGNAL( clicked() ), this, SLOT( setOpenFileName() ) );

}

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.

marcel
17th May 2007, 18:57
Strange.
Try setCursorPosition in code.
Maybe it works.

marcel
17th May 2007, 19:02
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.

tpf80
17th May 2007, 19:02
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.

tpf80
17th May 2007, 20:08
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.

kalmor
9th May 2014, 19:47
Might be a late answer, but

lineEdit->setCursorPosition(0)
worked fine for me.