Hi,
I am making a text editor using QGraphicTextItem and QTextDocument clases and I have a problem with text that disappears.

I have inserted my custom item at the begining of the block:

I use my CharFormat for the item:

Qt Code:
  1. ...
  2. QTextCharFormat format = cursor->charFormat();
  3.  
  4. cursor->movePosition(QTextCursor::StartOfBlock);
  5. MyItemFormat myItemFormat(format, listId(), level);
  6. cursor->insertText(QString(QChar::ObjectReplacementCharacter), myItemFormat);
  7. ...
To copy to clipboard, switch view to plain text mode 

Then I have registered handler class:

Qt Code:
  1. MyHandler* mh = new MyHandler(this);
  2.  
  3. document()->documentLayout()->registerHandler( MyItemFormat::Type, mh);
To copy to clipboard, switch view to plain text mode 

I have reimplemented intristicSize and drawObject methods:

Qt Code:
  1. QSizeF MyHandler::intrinsicSize(QTextDocument * doc, int posInDocument,
  2. const QTextFormat &format)
  3. {
  4. ...
  5. return QSizeF(30, 30);
  6. }
To copy to clipboard, switch view to plain text mode 


and a draw

Qt Code:
  1. void MyHandler::drawObject(QPainter *painter, const QRectF &rect,
  2. QTextDocument * doc, int posInDocument,
  3. const QTextFormat &format)
  4. {
  5. ....
  6. QTextCharFormat charFormat = format.toCharFormat();
  7. QBrush charBrush = charFormat.foreground();
  8. QFont font(charFormat.font());
  9. QColor charColor = charBrush.color();
  10.  
  11. QString imageText = "AA";
  12.  
  13. painter->setFont(font);
  14. painter->setPen(charColor);
  15.  
  16. qDebug() << rect << " ; " << painter->pen() << " ; " << painter->font();
  17.  
  18. painter->drawText(rect, Qt::AlignBottom, imageText);
  19.  
  20. QRectF rect1(rect);
  21. rect1.translate(-30, 0); // move new rect a little bit to the left to draw on left margin
  22.  
  23. imageText = "B";
  24.  
  25. qDebug() << rect1 << " ; " << painter->pen() << " ; " << painter->font();
  26.  
  27. painter->drawText(rect1, Qt::AlignBottom, imageText);
  28.  
  29. }
To copy to clipboard, switch view to plain text mode 

And this works fine. AA is drawn on start of text block and B is drawn on left margin.
But, when I select some text in the text block AA is still visible, but B disappears.

Debug print is not changed when text in block is selected (still show correct rect size and position, and pen and font colors) but text B is not visible.

Any idea??? I sopose that drawObject is not intended for drawing outsidee the QTextBlock rectangle or something like that but I am sure that there is a way to override this problem, just I do not see it.