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] Single Clicking and selecting 1 char in a QLineEdit
Qt 6.11 is out! See what's new in the release blog

[SOLVED] Single Clicking and selecting 1 char in a QLineEdit

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.4k 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by mrjj
    #1

    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.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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.

        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