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. Map QTextCursor to item coordinates in QGraphicsTextItem
Qt 6.11 is out! See what's new in the release blog

Map QTextCursor to item coordinates in QGraphicsTextItem

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 2.6k Views 1 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.
  • M Offline
    M Offline
    mkuettler
    wrote on last edited by
    #1

    Hello.
    I am using a QGraphicsScene with multiple QGraphicsTextItems. Sometimes I need to know where a QTextCursor is in terms of item coordinates. I use the following code to get the document coordinates.

    @
    QTextCursor cursor = textCursor();
    QTextBlock block = cursor.block();
    int p = cursor.position();
    QTextLine line = block.layout()->lineForTextPosition(p);
    if (!line.isValid())
    return block.layout()->position();
    return QPointF(line.cursorToX(p), line.y() + line.height());
    @

    Now my problem is that the document is not exactly aligned with the QGraphicsTextItem. There are a couple of pixels of margin around the document, and I do not know how many. I could of course try to measure them, but that would only give me the values for my particular setup, and I would prefer the program to look nice on every system. Is there a way to find out the position of the document in the item?

    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