PDA

View Full Version : How to make LineEdit show all text in uppercase



roseicollis
2nd March 2015, 09:38
Hi all!

I'm making a gui app which contains some lineedits to let the user introduce some information. For that I set some QRegExp and QValidator and it works fine except for 2 cases that I hope that you can help me :)

Case 1: a Lineedit that has to accept 3 letters which are ABC and max 5 numbers. For example... valid inputs: ABC12345 / ABC00000 / ABC1234 / ABC89865.... invalid inputs: ABC 1234 / AAA12345 / AB / ABC / A / ...
I have this code but it acepts too A / AA / ... and I want to force the user to write ABC and at least 2 numbers


QRegExp rx3("[A][B][C]\\d{5}");
QValidator *validator3 = new QRegExpValidator(rx3, this);
lineedit_case1->setValidator(validator3);

Another thing is that I want it to acept the user to put abc12345 BUT the line edit shows ABC12345. I mean: If the user write letters with caps lock or not, the line edits should always shows the text in uppercase. And with my code it only accepts that the user has the caps lock on.

Case 2: A line edit which accept a normal string BUT as the last example, if the user write it in downcase, the line edit has to show it in uppercase. I tried with a inputmask but it still shows in down case, the cursor its extrange and it doent acept spaces :(


lineedit_case2->setInputMask(">AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");


I tried it with:


connect(lineedit_Num1, SIGNAL(textChanged(QString)), SLOT(toUpper(QString)));

void Test::toUpper(QString text)
{
QLineEdit *le = qobject_cast<QLineEdit *>(sender());
if (!le)
return;
le->setText(text.toUpper()); // found this looking on internet but not sure about how it works
lineedit_Num1->setText(text.toUpper()); //trying if that works
}
Thank you so much

anda_skoa
2nd March 2015, 13:49
If you only want the user to be able to type upper case letters, you can derive from QLineEdit and implement key event handling methods or install an event filter that converst lower case key events into respective upper case key events.

Cheers,
_

roseicollis
2nd March 2015, 15:54
Hi anda_skoa,


converst lower case key events into respective upper case key events.
Thank you! I solved it with that. I let here the code for the case someone need it in the future :)

if(key->key() >= Qt::Key_A && key->key() <= Qt::Key_Z &&
((key->modifiers() & Qt::ShiftModifier) == false))
{
QApplication::sendEvent(obj, new QKeyEvent(QEvent::KeyPress,
key->key(), key->modifiers() | Qt::ShiftModifier,
key->text().toUpper()));
QApplication::sendEvent(obj, new QKeyEvent(QEvent::KeyRelease,
key->key(), key->modifiers() | Qt::ShiftModifier,
key->text().toUpper()));
return true;
}


Any idea/help/example about the other problem of the validators?

Lesiok
2nd March 2015, 16:10
Hi anda_skoa,


Thank you! I solved it with that. I let here the code for the case someone need it in the future :)

if(key->key() >= Qt::Key_A && key->key() <= Qt::Key_Z &&
((key->modifiers() & Qt::ShiftModifier) == false))
{
QApplication::sendEvent(obj, new QKeyEvent(QEvent::KeyPress,
key->key(), key->modifiers() | Qt::ShiftModifier,
key->text().toUpper()));
QApplication::sendEvent(obj, new QKeyEvent(QEvent::KeyRelease,
key->key(), key->modifiers() | Qt::ShiftModifier,
key->text().toUpper()));
return true;
}


Any idea/help/example about the other problem of the validators?

Are You sure it is working ? Try to press CapsLock and....

roseicollis
2nd March 2015, 16:18
Are You sure it is working ? Try to press CapsLock and....

Well nice point... with CapsLock it works perfect (on adn off) BUT it doesn't if I press shift.... wtf O.o Now I'm totally lost... how can I make it work for both cases capslock and shit key?

Lesiok
2nd March 2015, 16:49
Solution from first post with slot toUpper() is very good. Why You don't use it ?

roseicollis
2nd March 2015, 16:59
I did but if I write: abcde that is what lineedit shows O.o, in downcase. and in fact, if I put a bp in the SLOT(toUpper(QString) it never gets there dunno why :S

Edit: tee only case where the program shows downcase is if capslock is on but you press shift while write.

Kryzon
2nd March 2015, 21:33
Try the following: just create a copy of the event with the Text property in uppercase version and pass it to the base implementation.


void MyLineEdit::keyPressEvent( QKeyEvent* event )
{
event.accept();

QKeyEvent* modifiedEvent = new QKeyEvent( QEvent::KeyPress, event->key(), event->modifiers(), event->text().toUpper() );
QLineEdit::keyPressEvent( modifiedEvent );
}
EDIT: the same is needed for the release event handler.

Lesiok
3rd March 2015, 08:03
I did but if I write: abcde that is what lineedit shows O.o, in downcase. and in fact, if I put a bp in the SLOT(toUpper(QString) it never gets there dunno why :S

Edit: tee only case where the program shows downcase is if capslock is on but you press shift while write.You have something wrong in code. This solution works for sure - I use it myself.

roseicollis
3rd March 2015, 17:48
The mayus issue its solved :D Thank you so much! Any Idea about how to a RegExp which allows ABC0000 but not A or AB or ABC?

Kryzon
4th March 2015, 06:58
Try the following: just create a copy of the event with the Text property in uppercase version and pass it to the base implementation.


void MyLineEdit::keyPressEvent( QKeyEvent* event )
{
event.accept();

QKeyEvent* modifiedEvent = new QKeyEvent( QEvent::KeyPress, event->key(), event->modifiers(), event->text().toUpper() );
QLineEdit::keyPressEvent( modifiedEvent );
}
To quote myself as I can't edit that post, instead of creating the event in the heap like that (which is flawed in that it should lead to a leak), creating it in the stack is the right way.


QKeyEvent modifiedEvent( QEvent::KeyPress, event->key(), event->modifiers(), event->text().toUpper() );
QLineEdit::keyPressEvent( &modifiedEvent );
The temporary event will be destroyed after the base implementation processes it.

Lesiok
4th March 2015, 07:40
Try the following: just create a copy of the event with the Text property in uppercase version and pass it to the base implementation.


void MyLineEdit::keyPressEvent( QKeyEvent* event )
{
event.accept();

QKeyEvent* modifiedEvent = new QKeyEvent( QEvent::KeyPress, event->key(), event->modifiers(), event->text().toUpper() );
QLineEdit::keyPressEvent( modifiedEvent );
}
EDIT: the same is needed for the release event handler.

This solution is not complete. What with copy/paste ?

wysota
4th March 2015, 08:02
The mayus issue its solved :D Thank you so much! Any Idea about how to a RegExp which allows ABC0000 but not A or AB or ABC?

Why not simply reimplement fixup() in the validator? Or even validate() itself. Just convert the text to upper case before exiting the function.

As for your ABC thing, if the user can't enter anything else why make him input those letters at all? The easiest solution I can see is to replace the line edit with a spinbox, set a prefix and let the user enter numbers.

roseicollis
4th March 2015, 16:29
Solved guys! Thank you so much! I just connected to the textChanged signal and look there for exactmatch() :)