[Solved] How to avoid Flickering in a QLineEdit Text.
-
Hi..
I have a QLineEdit in that I am entering some values in it, Using QRegExp i can enter only integers and "." dot symbol in it. By Using the Event Filters i restricted the (.) dot symbol to be placed not more than once in the QLineEdit for easy entering of the values. But the problem is, If I enter (key press) the dot symbol (.) for the second time i can see a flickering of the dot symbol there in QLineEdit. When that dot key is released that value is gone.
Will make u clear once again...
Suppose if am entering 123. and again if i press the dot key the dot value is entered in the line edit when the key is pressed and it fades out when the key is released. How to avoid this kinda flickering?
Here's my Coding,
@if (keyEvent->key() == Qt::Key_Period)
{
QString tempString = q_LineEdit->text();
if(tempString.indexOf(".") != tempString.lastIndexOf("."))
{
int index = tempString.lastIndexOf(".");
tempString = tempString.mid(0, index);
q_LineEdit->setText(tempString);
}
}@Have a look at the image added below. the second dot value appears while pressing of the Dot key.
![URL=http://imgur.com/d73Ws1r][IMG]http://i.imgur.com/d73Ws1r.png[/IMG][/URL](Flickering of Dot Value Pressing @ 2nd time...)!
Thanks & Regards
-
Hi,
Why don't you use "input masks":http://qt-project.org/doc/qt-4.8/qlineedit.html#inputMask-prop and/or "validators":http://qt-project.org/doc/qt-4.8/qlineedit.html#setValidator?
-
This might be a case of "autoRepeat":http://doc.qt.digia.com/qt/qkeyevent.html#isAutoRepeat
AFAIK, key presses repeat at a certain interval except some special keys like SHIFT, ALT, CTRL (?). -
[quote author="tilsitt" date="1358759573"]Hi,
Why don't you use "input masks":http://qt-project.org/doc/qt-4.8/qlineedit.html#inputMask-prop and/or "validators":http://qt-project.org/doc/qt-4.8/qlineedit.html#setValidator?[/quote]
In Some cases i dont want to use any special characters in my QLineEdit and in some cases i want to use it. so i can't make use of QDoubleValidator or a QValidator. That's why i chose to use a QRegExp()..
-
I understand for the QDoubleValidator and the QValidator, but if you're using a QRegExp, then you could use a QRegExpValidator.
-
[quote author="Rochi" date="1358762491"]
In Some cases i dont want to use any special characters in my QLineEdit and in some cases i want to use it. so i can't make use of QDoubleValidator or a QValidator. That's why i chose to use a QRegExp()..
[/quote]I guess you just need to incorporate my link into your control structure (if)
@//didn't test it myself
if (keyEvent->key() == Qt::Key_Period && !keyEvent->isAutoRepeat())
@BTW: as the doc says, "Note that if the event is a multiple-key compressed event that is partly due to auto-repeat, this function could return either true or false indeterminately."
-
[quote author="tilsitt" date="1358762972"]I understand for the QDoubleValidator and the QValidator, but if you're using a QRegExp, then you could use a QRegExpValidator.[/quote]
Yup.. Am using a QRegExpValidator too...
@q_LineEdit->setValidator(new QRegExpValidator(Rx, this));@
where Rx is my QRegExp()....
-
[quote author="Code_ReaQtor" date="1358763113"]
I guess you just need to incorporate my link into your control structure (if)
@//didn't test it myself
if (keyEvent->key() == Qt::Key_Period && !keyEvent->isAutoRepeat())
@[/quote]Thanks for your Suggestion.... I Already tried it, But i couldn't get the solution.. Moreover, The dot value gets appended (when the key is pressed continously) to the QLineEdit... :-(
-
Ok. One solution could be to sub-class QLineEdit, re-implement keyPressEvent() and ignore the event if the key is the dot symbol and the text already contains one.
-
@Code_ReaQtor
Here's the image of the QLineEdit when i tried with,@if (keyEvent->key() == Qt::Key_Period && !keyEvent->isAutoRepeat())@
Dot Key Value gets appended when the key is pressed continously....
![IMG]http://i.imgur.com/c7YQkdF.png[/IMG](Using Key Event is Auto Repeat)!
-
[quote author="tilsitt" date="1358764433"]Ok. One solution could be to sub-class QLineEdit, re-implement keyPressEvent() and ignore the event if the key is the dot symbol and the text already contains one.[/quote]
Here i don't want to sub class my QLineEdit... Thats why i am using a Event Filter...
-
And in your event filter, what's the result with something like that:
@
if (keyEvent->key() == Qt::Key_Period)
{
if(q_LineEdit->text().contains('.'))
{
event->ignore();
}
else
{
event->accept();
}
}
@or
@
if (keyEvent->key() == Qt::Key_Period)
{
if(q_LineEdit->text().contains('.'))
{
return true;
}
}
@ -
tilsitt ....
This is my Code...
@if (keyEvent->key() == Qt::Key_Period)
{
QString tempString = q_LineEdit->text();
if(tempString.indexOf(".") != tempString.lastIndexOf("."))
{
int index = tempString.lastIndexOf(".");
tempString = tempString.mid(0, index);
q_LineEdit->setText(tempString);
}
}@This is working fine. The Dot key Value doesn't gets appended when it is pressed continously. The only problem is, as i mentioned earlier, When the Dot Value is pressed for the second time the Value is shown in the QLineEdit until the Dot Key is Released. Once the Key is released it does not gets appended.. what i want is when i am pressing the Dot Key for the second time the value should not be shown in the QLineEdit...
-
and my question is: in your event filter, do you return true or not?
if not, after your event filter where you didn't append the dot, the event will still be computed by the QLineEdit, so the dot will be still added, before being removed by the QRegExp.
-
If i return true, Then the Dot value is not even getting appended when i press it for the first time... :-(
-
Could you please post the complete code of your event filter function? (version with and without returning true)
-
ok.... i will post the complete coding of my Event Filter Function...
-
[quote author="tilsitt" date="1358766947"]Could you please post the complete code of your event filter function? (version with and without returning true)[/quote]
Here's my complete code of my event filter function...
@bool MyWidget::eventFilter(QObject *target, QEvent *event)
{
if (target == q_LineEdit)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);if (keyEvent->key() == Qt::Key_Period) { QString tempString = q_LineEdit->text(); if(tempString.indexOf(".") != tempString.lastIndexOf(".")) { int index = tempString.lastIndexOf("."); tempString = tempString.mid(0, index); q_LineEdit->setText(tempString); } else { q_LineEdit->clear(); if(tempString == ".") { tempString.insert(0, "0"); } q_LineEdit->setText(tempString); } return true; }
}@
-
Try this:
@
bool MyWidget::eventFilter(QObject *target, QEvent *event)
{
if (target == q_LineEdit)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);if (keyEvent->key() == Qt::Key_Period) { QString tempString = q_LineEdit->text(); if(tempString.indexOf(".") != tempString.lastIndexOf(".")) { int index = tempString.lastIndexOf("."); tempString = tempString.mid(0, index); q_LineEdit->setText(tempString); return true; } else { q_LineEdit->clear(); if(tempString == ".") { tempString.insert(0, "0"); } q_LineEdit->setText(tempString); } } } return MyWidget::eventFilter(target, event);
}
@ -
Oops... Sorry.. I already included that one & the result is the same.. Missed it while posting it... :-( Sorry for the Mistake...