Results 1 to 1 of 1

Thread: TextDocumentLayout problem with registerHandler

  1. #1
    Join Date
    May 2015
    Posts
    21
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default TextDocumentLayout problem with registerHandler

    Hi,
    I have a problem with handler for custom item in my QTextDocument. I created a handler which should draw one item with some text "XX" at the begining of the block. When block has only one line or more then one words in first line it is OK like this:

    XX some text

    but when block has only one long word broken into multiple lines XX item does not stay inline with text. Like this:

    XX
    somelargertextthatisb
    brokeninmorethanone
    lineinblock


    I wanth it to look something like this:

    XX somelargertextth
    atisbrokeninmoretha
    nonelineinblock


    or XX can be on left margin of the block inline with first block line.

    My handler code is:

    Qt Code:
    1. QSizeF MyHandler::intrinsicSize(QTextDocument * doc, int posInDocument,
    2. const QTextFormat &format)
    3. {
    4. QTextCharFormat charFormat = format.toCharFormat();
    5. QFont font(charFormat.font());
    6.  
    7. QTextBlock blk = doc->findBlock(posInDocument);
    8.  
    9. if(!blk.isValid()){
    10. qDebug() << "IS Block Not Valid";
    11. return QSize();
    12. } else {
    13. QTextLayout* layout = blk.layout();
    14. if(layout->lineCount()==0){
    15. qDebug() << "IS lineCount: " << layout->lineCount();
    16. return QSize();
    17. }
    18. }
    19.  
    20. QFontMetrics fontMetrics(font);
    21.  
    22. QSize size;
    23. size.setHeight(fontMetrics.height());
    24. size.setWidth(fontMetrics.width(QLatin1Char('0'))*3);
    25. return QSizeF(size);
    26. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. void MyHandler::drawObject(QPainter *painter, const QRectF &rect,
    2. QTextDocument * doc, int posInDocument,
    3. const QTextFormat &format)
    4. {
    5. QTextCursor* cursor = new QTextCursor(doc);
    6. cursor->setPosition(posInDocument);
    7.  
    8. if(posInDocument<0){
    9. qDebug() << "DO posInDocument: " << posInDocument;
    10. return;
    11. } else {
    12. blk = doc->findBlock(posInDocument);
    13. if(!blk.isValid()){
    14. qDebug() << "DO Block Not Valid";
    15. return;
    16. }
    17. }
    18.  
    19. if(blk.blockNumber() < 0)
    20. return;
    21.  
    22. QString imageText = "XX "; // I put just XX for simlicity , but in fact this two characters are created here with call to function createText();
    23.  
    24. if(imageText.isEmpty())
    25. return;
    26.  
    27. QTextCharFormat charFormat = format.toCharFormat();
    28. QFont font(charFormat.font());
    29.  
    30. QTextLayout *layout = blk.layout();
    31. if (layout->lineCount() == 0)
    32. return;
    33.  
    34. QTextLine firstLine = layout->lineAt(0);
    35. QRectF lineRect = firstLine.rect();
    36. QRectF naturalRect = firstLine.naturalTextRect();
    37. QRectF blockRect = doc->documentLayout()->blockBoundingRect(blk);
    38.  
    39. QRectF rect1(rect);
    40. rect1.moveBottom(lineRect.bottom()+blockRect.y());
    41. rect1.moveLeft(naturalRect.x());
    42.  
    43. painter->setFont(font);
    44. painter->drawText(rect1, Qt::AlignBottom, imageText);
    45. }
    To copy to clipboard, switch view to plain text mode 



    I used QChar::ObjectReplacementCharacter to insert item:


    Qt Code:
    1. ...
    2. QTextCharFormat crFormat = charFormat;
    3. cFormat.setObjectType(MyItem::Type);
    4. cFormat.setProperty(HeaderItemProperty, QString(""));
    5.  
    6. cursor->insertText(QString(QChar::ObjectReplacementCharacter), svgCharFormat);
    7.  
    8. ...
    To copy to clipboard, switch view to plain text mode 


    And registered handler in my QGraphicTextItemwith:

    Qt Code:
    1. ...
    2. document()->documentLayout()->registerHandler( MyItem::Type, myHandlerObject);
    3. ...
    To copy to clipboard, switch view to plain text mode 



    What I did wrong?
    Last edited by 'milimoj; 9th June 2015 at 21:55.

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.