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.2k 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.
  • 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
                                • T Offline
                                  T Offline
                                  tilsitt
                                  wrote on last edited by
                                  #21

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

                                  }
                                  @

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

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

                                    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