Map QTextCursor to item coordinates in QGraphicsTextItem

  • 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?

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.