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. How do I fix strange cursor behavior in QLineEdit using inputMask containing spaces?

How do I fix strange cursor behavior in QLineEdit using inputMask containing spaces?

Scheduled Pinned Locked Moved Solved General and Desktop
qlineeditinputmask
2 Posts 2 Posters 622 Views
  • 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.
  • H Offline
    H Offline
    HowardHarkness
    wrote on 23 Dec 2021, 18:39 last edited by HowardHarkness
    #1

    I noticed some strange behavior in QLineEdit after adding an inputMask. What I wanted was a binary display of a value, in groups of 4 for readability, and a hex display, also grouped by 4 hex chars. Then I added code to keep them in synch.

    I added inputMasks as follows:

    const QString hexDisplayMask("HHHH HHHH");
    const QString binDisplayMask("BBBB BBBB BBBB BBBB BBBB BBBB BBBB BBBB");
    ...
    ui->scratchPadRegisterLineEdit->setInputMask(binDisplayMask);
    ui->scratchPadRegisterHexLineEdit->setInputMask(hexDisplayMask);

    The input mask with spaces had the desired effect on the display:
    26afbd26-4ad8-4417-94e3-83fe70c0f7fb-image.png
    However, the cursor position is one position off to the right of the character to edit in the binary display (shown), and also in the hex display when selected.

    I have not used Qt for a while (last time was over 10 years ago), so I may have missed something important in the evolution of how the QLineEdit is handled. I think I can come up with a workaround, but I have not thought of any that I would consider elegant.

    I can just leave the spaces out of the inputMask, and the cursor behaves -- but the output is much harder to read.

    I hope I can find a solution to this that will not confuse my client/customer.

    I am using Qt 5.14 (I am stuck with that due to client contraints).

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

    P 1 Reply Last reply 26 Dec 2021, 04:08
    0
    • H HowardHarkness
      23 Dec 2021, 18:39

      I noticed some strange behavior in QLineEdit after adding an inputMask. What I wanted was a binary display of a value, in groups of 4 for readability, and a hex display, also grouped by 4 hex chars. Then I added code to keep them in synch.

      I added inputMasks as follows:

      const QString hexDisplayMask("HHHH HHHH");
      const QString binDisplayMask("BBBB BBBB BBBB BBBB BBBB BBBB BBBB BBBB");
      ...
      ui->scratchPadRegisterLineEdit->setInputMask(binDisplayMask);
      ui->scratchPadRegisterHexLineEdit->setInputMask(hexDisplayMask);

      The input mask with spaces had the desired effect on the display:
      26afbd26-4ad8-4417-94e3-83fe70c0f7fb-image.png
      However, the cursor position is one position off to the right of the character to edit in the binary display (shown), and also in the hex display when selected.

      I have not used Qt for a while (last time was over 10 years ago), so I may have missed something important in the evolution of how the QLineEdit is handled. I think I can come up with a workaround, but I have not thought of any that I would consider elegant.

      I can just leave the spaces out of the inputMask, and the cursor behaves -- but the output is much harder to read.

      I hope I can find a solution to this that will not confuse my client/customer.

      I am using Qt 5.14 (I am stuck with that due to client contraints).

      P Offline
      P Offline
      Pl45m4
      wrote on 26 Dec 2021, 04:08 last edited by Pl45m4
      #2

      @HowardHarkness

      Hi there was a similar topic / question few years ago.
      @SGaist explained why there is one additional space after every mask -> to allow text selection by keyboard.
      Of course Ctrl + A kinda "hacks" it (to select all, at least) and if you dont need it, you could implement a workaround, but unfortunately there is no "Qt-way" to switch it off.

      • https://forum.qt.io/topic/60633/qlineedit-using-input-mask-with-blank-space-replacement-cursor-select-replacement-char

      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      0

      1/2

      23 Dec 2021, 18:39

      • Login

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