[SOLVED] Single Clicking and selecting 1 char in a QLineEdit
-
Hi
Goal:
Single click 1 letter/char/digit in a QLineEdit and it will select that one.
FontSize is raised a lot so big letters.Sort of what it does with doubleclick and words but only for 1 char and with single click.
I tried
void mouseReleaseEvent(QMouseEvent* e) {
if (e->button() == Qt::LeftButton) {
cursorForward(true, 1);
}
}which works most of the time but sometimes
it select the previous letter or the next even it seems
you click directly on it. (in center)So I tried to look at source of what dblclick does but sadly that is all in private and
uses only private stuff so not able to reuse.So any good idea to avoid to create list of the width of all letters and calculating what
hitbox we click on and then somehow make it select that. -
When clicking on a letter it will place the cursor before it if you clicked on the left half of the letter or after it if you clicked on the right half. So the above code will either select the letter under cursor or the next one, depending on the clicked half.
A quick workaround is to "shift" the position detection half a letter back so that the cursor is always placed before the letter:
auto halfWidth = fontMetrics().width("a") / 2; auto cursorPos = cursorPositionAt(e->pos() - QPoint(halfWidth, 0)); setCursorPosition(cursorPos); cursorForward(true, 1);
The downside is the above will only work with monospace font (which is better for this task anyway). With non-monospace font you'd have to figure out which letter is under cursor and get half of its width.
-
Hi
Good workaround.
Works pretty well even for proportional fonts when its 0-9
as they are almost same size width wise.
of course "iiiWiiii" is another story :)
Thank you.