Results 1 to 3 of 3

Thread: QTextDocument Manual Line Breaks

  1. #1
    Join Date
    Aug 2012
    Posts
    13
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default QTextDocument Manual Line Breaks

    I have a QGraphicsTextItem that I am autosizing the font in, I can't let the document() line break automatically because in order to do my alignment i need to setTextWidth(boundingRect.width()) which makes it break when I don't want it to.

    The expected behavior should be to autosize as large as possible and then shrink until the minimum font size is hit, then I want to insert a linebreak.

    The way I am currently implementing this autosizing is by using fontmetrics inside of the documents contentsChange(int,int,int) handler and it causes a recursive loop by trying to insert a "\n"

    Qt Code:
    1. if((f.pointSizeF() * factor) < m_minFontSize) {
    2. f.setPointSizeF(m_minFontSize);
    3. text.append("\n");
    4. disconnect(this->document(), SIGNAL(contentsChange(int,int,int)),
    5. this, SLOT(contentsChangedHandler(int,int,int)));
    6. this->setPlainText(text);
    7. connect(this->document(), SIGNAL(contentsChange(int,int,int)),
    8. this, SLOT(contentsChangedHandler(int,int,int)));
    9. }
    To copy to clipboard, switch view to plain text mode 

    I have tried both blockSignals(true) blockSignals(false) wrapper and a disconnect and connnect wrapper and neither one stops the recursion. What is the normal way to implement manual line wrapping in a textdocument?

  2. #2
    Join Date
    Aug 2012
    Posts
    13
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QTextDocument Manual Line Breaks

    After looking into this for awhile it looks like it might be possible to do line breaking (word wrap) by reimplementing QAbstractTextDocument, however that looks like a lot of work with all of those virtual functions and not much documentation. Is there any easier way?

    I would hate to have to reimplement all that code when all I want is access to do line breaks. What is class is the default layout for a QGraphicsTextItem, there has to be something derived from QAbstractTextDocument that I could subclass and override the documentChanged() instead of the abstract interface.

  3. #3
    Join Date
    Aug 2012
    Posts
    13
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QTextDocument Manual Line Breaks

    Well I kind of got this figured out, guess I should read the documentation better. You need to use textCursor() to make modifications to the document. However now the line wrapping works but whenever you hit backspace it crashes in QTextLayout::lineCount(). Is this a bug in Qt?

    here is what shows in gdb:

    Program received signal SIGSEGV, Segmentation fault.
    QTextLayout::lineCount (this=0x0) at text/qtextlayout.cpp:841
    841 return d->lines.size();

    Qt Code:
    1. if((f.pointSizeF() * factor) < m_minFontSize)
    2. {
    3. QTextCursor c(this->document());
    4. f.setPointSizeF(m_minFontSize);
    5.  
    6. if(!c.isNull() )
    7. {
    8. c.movePosition(QTextCursor::End);
    9. c.select(QTextCursor::LineUnderCursor);
    10. QString lineText = c.selectedText();
    11. qDebug() << "lineText: " << lineText;
    12. qDebug() << "lineCount(): " << this->document()->lineCount();
    13. c.clearSelection();
    14. QFontMetrics fm2(f);
    15.  
    16. if(fm2.boundingRect(lineText).width() >= drawRect.width())
    17. {
    18. //c.insertText("\n");
    19. c.insertBlock();
    20. c.movePosition(QTextCursor::End);
    21. }
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Mac line breaks
    By timmu in forum Qt Programming
    Replies: 1
    Last Post: 9th August 2012, 05:03
  2. QTextDocument - line spacing
    By mazurekwrc in forum Qt Programming
    Replies: 5
    Last Post: 24th February 2011, 23:23
  3. Remove line breaks in QDomDocument
    By estanisgeyer in forum Qt Programming
    Replies: 0
    Last Post: 17th December 2010, 11:39
  4. Qt4 'breaks' command line program
    By kachofool in forum Newbie
    Replies: 7
    Last Post: 13th November 2009, 20:56
  5. QTextDocument line number
    By bunjee in forum Qt Programming
    Replies: 14
    Last Post: 10th June 2008, 13:49

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.