[Qt-interest] Inserting a QTextBlock without inserting a newline

Tony Rietwyk tony.rietwyk at rightsoft.com.au
Mon Feb 28 13:47:41 CET 2011


HI, 
 
I think QTextBlock's correspond to paragraphs?  So each word and each space
is laid out vertically.  To unfold the words back into one line, you would
need to convert the spaces back into embedded text.  
 
Hope that helps, 
 
Tony
 

Sent: Monday, 28 February 2011 20:31

Hi all,
suppose I want to have a custom QTextEdit with foldable words. I want to
have the functionality to be able to fold every word on a single line
individually. They fold when they are clicked on and become a plus (+) or
something  similar/arbitrary (which, when clicked on again, unfolds the text
back to the original word). After looking through basetexteditor.cpp
<http://qt.gitorious.org/qt-creator/qt-creator/blobs/master/src/plugins/text
editor/basetexteditor.cpp>  (the source code that Qt Creator uses to fold
text in comments etc), it appears as if the route to go is to put whatever
text I want to fold in a QTextBlock. I should also note that I am putting
individual spaces into their own QTextBlock's to keep things organized.
However, consider the following code:

void MyCustomTextEdit::keyPressEvent ( QKeyEvent * event )

{
    if(!(event->key() == Qt::Key_Space))
    {
        m_Cursor->insertText(event->text());
    }
    else //event->key() == Qt::Key_Space
    {
        QTextBlockFormat qtbf;
        QTextFormat::PageBreakFlags pbf(QTextFormat::PageBreak_AlwaysAfter);
//I've also tried AlwaysBefore here. didn't work either.
        qtbf.setPageBreakPolicy(pbf);
        qtbf.setNonBreakableLines(true); //also tried false here. didn't
work either        

        m_Cursor->insertBlock(qtbf); //right here a newline is inserted into
my QTextEdit
        m_Cursor->insertText(" ");
        m_Cursor->insertBlock(qtbf); //and again here
    }
}

when running, this is the output:
word1

word2

word3

word4

word5

i want it to be like this:
word1 word2 word3 word4 word5

Once I get each of those words into their own QTextBlock, I should have no
problem getting them to fold/unfold individually when clicked. I am not
asking how to fold/unfold them, I'm pretty sure I can figure that part out
myself.

Thanks,
d3fault

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110228/194da23d/attachment.html 


More information about the Qt-interest-old mailing list