[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





  • 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...



  • To block the second dot using your event filter, the following works for me:

    @
    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.contains('.'))
            {
                return true; // QLineEdit::keyPressEvent will not be executed, the key press will just vanish
            }
        }
    }
    return MyWidget::eventFilter(target, event);
    

    }
    @

    I don't understand why you're using this:

    @
    int index = tempString.lastIndexOf(".");
    tempString = tempString.mid(0, index);
    q_LineEdit->setText(tempString);
    @

    with this code, if you press some keys, a dot, some other keys and a second dot, the "some other keys" will be deleted. If that's what you want, why not.

    one last thing: you don't check the type of your event, so if your QLineEdit send an event which is not a key press your program will crash. you should do something like that:

    @
    bool MyWidget::eventFilter(QObject *target, QEvent *event)
    {
    if (target == q_LineEdit && event->type()==QEvent::KeyPress)
    {
    QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);

        if (keyEvent->key() == Qt::Key_Period)
        {
            QString tempString = q_LineEdit->text();
            if(tempString.contains('.'))
            {
                return true;
            }
        }
    }
    return MyWidget::eventFilter(target, event);
    

    }
    @

    so with your initial code:

    @
    bool MyWidget::eventFilter(QObject *target, QEvent *event)
    {
    if (target == q_LineEdit && event->type()==QEvent::KeyPress)
    {
    QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);

        if (keyEvent->key() == Qt::Key_Period)
        {
            QString tempString = q_LineEdit->text();
            if(tempString.contains('.'))
            {
                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);
    

    }
    @



  • Wooww.. It works Fine... Thanks a lot tilsitt... :-)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.