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