PDA

View Full Version : Problem with QTextFragment



Zygyzy
30th August 2012, 12:56
Hi,

i took a QTextEdit and a QSyntaxHighlighter to change the font color of a part of the textedit's first line, for example:

redblue

Now I would like to get the textline back from the textedit, but it's very important to keep the difference between both text fragments ("red" and "blue"). The Qt-manual (http://doc.qt.nokia.com/4.7-snapshot/qtextblock-iterator.html) says I have to do it this way:



QTextBlock currentBlock = mytextEdit->document()->findBlockByLineNumber(0); //first line

QTextBlock::iterator it;
for (it = doc->begin().begin(); !(it.atEnd()); ++it)
{
QTextFragment currentFragment = it.fragment();
if (currentFragment.isValid())
{
QTextCharFormat f = currentFragment.charFormat(); //font of fragment
QString txt = currentFragment.text(); //text of fragment
}
}


Usually I should retrieve two QTextFragments, one for "red" and another for "blue", both automatically seperated by their different fonts (look here (http://doc.qt.nokia.com/4.7-snapshot/qtextfragment.html)). But I only retrieve one QTextFragment conatining the whole string "redblue". Also the correspondent QTextCharFormat seems to be empty.

Does anyone know a solution? I use Qt 4.7.4 + VS2010.


With kind regards,
Zygyzy