Results 1 to 3 of 3

Thread: Get location in String from pixel-position

  1. #1
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Get location in String from pixel-position

    Hi,

    I paint a QString onto a widget, and the user clicks somewhere into the painted string. I have the QPoint of the mouse click, the painted string e.g. QString = "123456789", the font used and a QFontMetrics.
    How can I now find at what position / what character the user placed the mouseclick?


    I could use a monospaced font and do pos.x() / metrics.averageCharWidth().
    I could also iterate over the string, call metrics.width(char) on ever successive char, and then check when I hit pos.y().


    The first obv doesnt support non-monospaced fonts, the second looks inefficient, is there a better way? I mean, TextEdits have to do it in some way too right.


    Thanks!

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Get location in String from pixel-position

    Why are you reinventing the wheel?

  3. #3
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Get location in String from pixel-position

    Because it's a complex custom widget. I cant just use a textbox. Or what do you mean?

Similar Threads

  1. Replies: 2
    Last Post: 2nd August 2015, 18:28
  2. Replies: 0
    Last Post: 5th January 2011, 23:21
  3. Replies: 1
    Last Post: 22nd August 2008, 10:12
  4. Pixel Position and Printing text
    By nErnie in forum Qt Programming
    Replies: 1
    Last Post: 7th November 2006, 13:35
  5. how change the QListBox item position by pixel
    By roy_skyx in forum Qt Programming
    Replies: 2
    Last Post: 20th January 2006, 02:34

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.