Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] How to avoid Flickering in a QLineEdit Text.
Forum Updated to NodeBB v4.3 + New Features

[Solved] How to avoid Flickering in a QLineEdit Text.

Scheduled Pinned Locked Moved General and Desktop
22 Posts 3 Posters 8.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Macro
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tilsitt
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Code_ReaQtor
        wrote on last edited by
        #3

        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 (?).

        Please visit my open-source projects at https://github.com/Code-ReaQtor.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Macro
          wrote on last edited by
          #4

          [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()..

          1 Reply Last reply
          0
          • T Offline
            T Offline
            tilsitt
            wrote on last edited by
            #5

            I understand for the QDoubleValidator and the QValidator, but if you're using a QRegExp, then you could use a QRegExpValidator.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Code_ReaQtor
              wrote on last edited by
              #6

              [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."

              Please visit my open-source projects at https://github.com/Code-ReaQtor.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Macro
                wrote on last edited by
                #7

                [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()....

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Macro
                  wrote on last edited by
                  #8

                  [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... :-(

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    tilsitt
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      Macro
                      wrote on last edited by
                      #10

                      @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)!

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        Macro
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        0
                        • T Offline
                          T Offline
                          tilsitt
                          wrote on last edited by
                          #12

                          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;
                          }
                          }
                          @

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            Macro
                            wrote on last edited by
                            #13

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

                            1 Reply Last reply
                            0
                            • T Offline
                              T Offline
                              tilsitt
                              wrote on last edited by
                              #14

                              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.

                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                Macro
                                wrote on last edited by
                                #15

                                If i return true, Then the Dot value is not even getting appended when i press it for the first time... :-(

                                1 Reply Last reply
                                0
                                • T Offline
                                  T Offline
                                  tilsitt
                                  wrote on last edited by
                                  #16

                                  Could you please post the complete code of your event filter function? (version with and without returning true)

                                  1 Reply Last reply
                                  0
                                  • M Offline
                                    M Offline
                                    Macro
                                    wrote on last edited by
                                    #17

                                    ok.... i will post the complete coding of my Event Filter Function...

                                    1 Reply Last reply
                                    0
                                    • M Offline
                                      M Offline
                                      Macro
                                      wrote on last edited by
                                      #18

                                      [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;
                                        }
                                      

                                      }@

                                      1 Reply Last reply
                                      0
                                      • T Offline
                                        T Offline
                                        tilsitt
                                        wrote on last edited by
                                        #19

                                        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);
                                        

                                        }
                                        @

                                        1 Reply Last reply
                                        0
                                        • M Offline
                                          M Offline
                                          Macro
                                          wrote on last edited by
                                          #20

                                          Oops... Sorry.. I already included that one & the result is the same.. Missed it while posting it... :-( Sorry for the Mistake...

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved