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. Wonky behavior of QLineEdit with inputmask -- cursor in wrong position
Forum Updated to NodeBB v4.3 + New Features

Wonky behavior of QLineEdit with inputmask -- cursor in wrong position

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 930 Views 2 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.
  • HowardHarknessH Offline
    HowardHarknessH Offline
    HowardHarkness
    wrote on last edited by HowardHarkness
    #1

    I'm using Qt 5.14 (customer requirement).

    I posted a similar question, but never really got a resolution other than "that's the way Qt does it."

    I have a QLineEdit with an inputmask of HHHHHHHH. The cursor is in the wrong position. Here is a detailed example.
    Initial state:
    d8abc403-d84e-43fd-9537-dc9ba130331d-image.png
    Here is the state after pressing the backspace key once.
    8051b0b3-c180-46c4-8a4f-129176caa55e-image.png
    The expected behavior would be to delete the character "B", and not the character "D".
    The next screenshot is after input of the letter "A"
    aab08fb0-f18b-4e33-94b8-3db049777d3d-image.png

    This is not going to be OK with my customer. How do I fix it?

    Howard Lee Harkness
    https://howardleeharkness.com/resume
    Current contract ends on March 11th, 2022 -- Looking for C++ Real-time Embedded Systems work

    1 Reply Last reply
    0
    • HowardHarknessH HowardHarkness

      @SGaist Since you asked about the font, I tried some other fonts. Same result.

      If, however, I remove the input mask (no mask), the wonky behavior goes away. But then, so does the check for a hex character on each keystroke. BTW, the same problem occurs with an input mask for binary input ("BBBBBBBB....").

      That may be the only practical solution. Although I'd rather not have to tell the customer that Qt's inputmask feature doesn't work.

      HowardHarknessH Offline
      HowardHarknessH Offline
      HowardHarkness
      wrote on last edited by HowardHarkness
      #5

      I think I have an acceptable solution using a QValidator.

      In the #include:

      #include <QRegularExpression>
      
      class HexValidator : public QValidator
      {
          QValidator::State validate(QString &input, int & /*pos*/) const;
      };
      

      In the cpp file:

      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
          , hv(new HexValidator)
      {
          ui->setupUi(this);
          ui->lineEdit->setValidator(hv);
      }
      
      QValidator::State HexValidator::validate(QString &input, int & /*pos*/) const
      {
          QValidator::State valid(QValidator::Invalid);
          QRegularExpression re(QRegularExpression::anchoredPattern("[0-9A-F]*")); //hex digit, uppercase only
          QRegularExpressionMatch match = re.match(input);
          bool matched = match.hasMatch();
          if(matched)
          {
              valid = QValidator::Acceptable;
          }
          return valid;
      }
      

      Similar solution for binary input validation.

      Probably will need some tweaking, but that appears to work in my simplified example project. Plus, the inputmask wonkiness is gone.

      Howard Lee Harkness
      https://howardleeharkness.com/resume
      Current contract ends on March 11th, 2022 -- Looking for C++ Real-time Embedded Systems work

      1 Reply Last reply
      4
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        Can you provide a minimal compilable example that shows this behaviour ?

        Also which font are you using ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        HowardHarknessH 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          Can you provide a minimal compilable example that shows this behaviour ?

          Also which font are you using ?

          HowardHarknessH Offline
          HowardHarknessH Offline
          HowardHarkness
          wrote on last edited by
          #3

          @SGaist Ok, here goes: New project with C++. Added one QLineEdit to mainwindow.

          #include "mainwindow.h"
          #include "ui_mainwindow.h"
          
          MainWindow::MainWindow(QWidget *parent)
              : QMainWindow(parent)
              , ui(new Ui::MainWindow)
          {
              ui->setupUi(this);
          }
          
          MainWindow::~MainWindow()
          {
              delete ui;
          }
          
          

          Main window with one QLineEdit
          f33eb57c-444d-4c91-b3ff-8d54dcb3edfc-image.png
          Font: MS Shell Dlg 2, 8
          Kit: Desktop Qt 5.14.2 MinGW 32-bit (same results with 64 bit)
          6063a7ea-e6ad-48cf-a396-ce7426316aa1-image.png

          <?xml version="1.0" encoding="UTF-8"?>
          <ui version="4.0">
           <class>MainWindow</class>
           <widget class="QMainWindow" name="MainWindow">
            <property name="geometry">
             <rect>
              <x>0</x>
              <y>0</y>
              <width>263</width>
              <height>160</height>
             </rect>
            </property>
            <property name="windowTitle">
             <string>MainWindow</string>
            </property>
            <widget class="QWidget" name="centralwidget">
             <widget class="QLineEdit" name="lineEdit">
              <property name="geometry">
               <rect>
                <x>70</x>
                <y>50</y>
                <width>113</width>
                <height>21</height>
               </rect>
              </property>
              <property name="inputMask">
               <string>HHHHHHHH</string>
              </property>
              <property name="text">
               <string>DEADBEEF</string>
              </property>
             </widget>
            </widget>
            <widget class="QMenuBar" name="menubar">
             <property name="geometry">
              <rect>
               <x>0</x>
               <y>0</y>
               <width>263</width>
               <height>21</height>
              </rect>
             </property>
            </widget>
            <widget class="QStatusBar" name="statusbar"/>
           </widget>
           <resources/>
           <connections/>
          </ui>
          

          Run:
          713f9ac2-8e89-4cd9-9001-b6caf942de75-image.png

          Position Cursor:
          7174e18a-c8ea-47e3-bc5f-5ab882504dcd-image.png
          Press Backspace Key:
          e3272b85-b6cb-450c-95ac-a2ec289072c2-image.png
          Enter "A"
          d3dc3095-b8c9-484e-80f4-4a27c51a2131-image.png

          Howard Lee Harkness
          https://howardleeharkness.com/resume
          Current contract ends on March 11th, 2022 -- Looking for C++ Real-time Embedded Systems work

          HowardHarknessH 1 Reply Last reply
          0
          • HowardHarknessH HowardHarkness

            @SGaist Ok, here goes: New project with C++. Added one QLineEdit to mainwindow.

            #include "mainwindow.h"
            #include "ui_mainwindow.h"
            
            MainWindow::MainWindow(QWidget *parent)
                : QMainWindow(parent)
                , ui(new Ui::MainWindow)
            {
                ui->setupUi(this);
            }
            
            MainWindow::~MainWindow()
            {
                delete ui;
            }
            
            

            Main window with one QLineEdit
            f33eb57c-444d-4c91-b3ff-8d54dcb3edfc-image.png
            Font: MS Shell Dlg 2, 8
            Kit: Desktop Qt 5.14.2 MinGW 32-bit (same results with 64 bit)
            6063a7ea-e6ad-48cf-a396-ce7426316aa1-image.png

            <?xml version="1.0" encoding="UTF-8"?>
            <ui version="4.0">
             <class>MainWindow</class>
             <widget class="QMainWindow" name="MainWindow">
              <property name="geometry">
               <rect>
                <x>0</x>
                <y>0</y>
                <width>263</width>
                <height>160</height>
               </rect>
              </property>
              <property name="windowTitle">
               <string>MainWindow</string>
              </property>
              <widget class="QWidget" name="centralwidget">
               <widget class="QLineEdit" name="lineEdit">
                <property name="geometry">
                 <rect>
                  <x>70</x>
                  <y>50</y>
                  <width>113</width>
                  <height>21</height>
                 </rect>
                </property>
                <property name="inputMask">
                 <string>HHHHHHHH</string>
                </property>
                <property name="text">
                 <string>DEADBEEF</string>
                </property>
               </widget>
              </widget>
              <widget class="QMenuBar" name="menubar">
               <property name="geometry">
                <rect>
                 <x>0</x>
                 <y>0</y>
                 <width>263</width>
                 <height>21</height>
                </rect>
               </property>
              </widget>
              <widget class="QStatusBar" name="statusbar"/>
             </widget>
             <resources/>
             <connections/>
            </ui>
            

            Run:
            713f9ac2-8e89-4cd9-9001-b6caf942de75-image.png

            Position Cursor:
            7174e18a-c8ea-47e3-bc5f-5ab882504dcd-image.png
            Press Backspace Key:
            e3272b85-b6cb-450c-95ac-a2ec289072c2-image.png
            Enter "A"
            d3dc3095-b8c9-484e-80f4-4a27c51a2131-image.png

            HowardHarknessH Offline
            HowardHarknessH Offline
            HowardHarkness
            wrote on last edited by HowardHarkness
            #4

            @SGaist Since you asked about the font, I tried some other fonts. Same result.

            If, however, I remove the input mask (no mask), the wonky behavior goes away. But then, so does the check for a hex character on each keystroke. BTW, the same problem occurs with an input mask for binary input ("BBBBBBBB....").

            That may be the only practical solution. Although I'd rather not have to tell the customer that Qt's inputmask feature doesn't work.

            Howard Lee Harkness
            https://howardleeharkness.com/resume
            Current contract ends on March 11th, 2022 -- Looking for C++ Real-time Embedded Systems work

            HowardHarknessH 1 Reply Last reply
            0
            • HowardHarknessH HowardHarkness

              @SGaist Since you asked about the font, I tried some other fonts. Same result.

              If, however, I remove the input mask (no mask), the wonky behavior goes away. But then, so does the check for a hex character on each keystroke. BTW, the same problem occurs with an input mask for binary input ("BBBBBBBB....").

              That may be the only practical solution. Although I'd rather not have to tell the customer that Qt's inputmask feature doesn't work.

              HowardHarknessH Offline
              HowardHarknessH Offline
              HowardHarkness
              wrote on last edited by HowardHarkness
              #5

              I think I have an acceptable solution using a QValidator.

              In the #include:

              #include <QRegularExpression>
              
              class HexValidator : public QValidator
              {
                  QValidator::State validate(QString &input, int & /*pos*/) const;
              };
              

              In the cpp file:

              MainWindow::MainWindow(QWidget *parent)
                  : QMainWindow(parent)
                  , ui(new Ui::MainWindow)
                  , hv(new HexValidator)
              {
                  ui->setupUi(this);
                  ui->lineEdit->setValidator(hv);
              }
              
              QValidator::State HexValidator::validate(QString &input, int & /*pos*/) const
              {
                  QValidator::State valid(QValidator::Invalid);
                  QRegularExpression re(QRegularExpression::anchoredPattern("[0-9A-F]*")); //hex digit, uppercase only
                  QRegularExpressionMatch match = re.match(input);
                  bool matched = match.hasMatch();
                  if(matched)
                  {
                      valid = QValidator::Acceptable;
                  }
                  return valid;
              }
              

              Similar solution for binary input validation.

              Probably will need some tweaking, but that appears to work in my simplified example project. Plus, the inputmask wonkiness is gone.

              Howard Lee Harkness
              https://howardleeharkness.com/resume
              Current contract ends on March 11th, 2022 -- Looking for C++ Real-time Embedded Systems work

              1 Reply Last reply
              4
              • hskoglundH Online
                hskoglundH Online
                hskoglund
                wrote on last edited by hskoglund
                #6

                Hi, I have the same problem (wonky behavior) with a QLineEdit with a mask for inputting social security numbers.
                Using a ->setValidator() seems like the better stuff, thank you for posting the code!

                HowardHarknessH 1 Reply Last reply
                0
                • hskoglundH hskoglund

                  Hi, I have the same problem (wonky behavior) with a QLineEdit with a mask for inputting social security numbers.
                  Using a ->setValidator() seems like the better stuff, thank you for posting the code!

                  HowardHarknessH Offline
                  HowardHarknessH Offline
                  HowardHarkness
                  wrote on last edited by
                  #7

                  @hskoglund I'm glad I could help!

                  It definitely appears to me that the inputmask "feature" is broken, and custom validators is the way to go.

                  Howard Lee Harkness
                  https://howardleeharkness.com/resume
                  Current contract ends on March 11th, 2022 -- Looking for C++ Real-time Embedded Systems work

                  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