Results 1 to 2 of 2

Thread: QTextDocument layout problem with text disappears

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

    Default QTextDocument layout problem with text disappears

    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.

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

    Default Re: QTextDocument layout problem with text disappears

    No idea why text is not visible???
    At least, I would like to understand what happend, eaven if there is no solution.

Similar Threads

  1. Prevent text from selecting in QTextDocument
    By 'milimoj in forum Qt Programming
    Replies: 0
    Last Post: 27th May 2015, 21:41
  2. Replies: 0
    Last Post: 1st December 2014, 17:51
  3. Qt Printing QTextDocument problem (truncated text)
    By giowck in forum Qt Programming
    Replies: 3
    Last Post: 20th April 2011, 08:37
  4. How to draw columns text in QTextDocument?
    By nifei in forum Qt Programming
    Replies: 0
    Last Post: 23rd March 2009, 03:17
  5. Vertical text in a QTextDocument
    By Angelo Moriconi in forum Qt Programming
    Replies: 6
    Last Post: 7th February 2008, 06:30

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.