qtextedit:zeichen einfügen an best. pos. nach zeichenformat
Verfasst: 13. Oktober 2007 11:30
hi,
ich versuche gerade, in einer von QTextEdit abgeleiteten klasse eine methode toBB zu implementieren, die eben den formatierten text (vorerst nur fett, kursiv, unterstrichen) in bb-code umwandeln soll (fuer foren).
das ist meine methode wie sie jetzt ist.
das problem: die bb-tags ( , , , , , ) stehen immer eine Stelle zuweit im result-string.
ich habe aber keine ahnung, wieso.
hoffe, mir kann geholfen werden, danke im voraus.
mfg,
julian
ich versuche gerade, in einer von QTextEdit abgeleiteten klasse eine methode toBB zu implementieren, die eben den formatierten text (vorerst nur fett, kursiv, unterstrichen) in bb-code umwandeln soll (fuer foren).
Code: Alles auswählen
QString myEdit::toBB()
{
QString plainText = toPlainText();
if( plainText.isEmpty() )
return QString();
QString result;
QTextCursor cursor = textCursor();
cursor.movePosition( QTextCursor::Start );
cursor.movePosition( QTextCursor::NextCharacter );
QTextCharFormat tcFormat;
for( int i = 0; i < plainText.size(); i++ )
{
if( cursor.charFormat() == tcFormat )
{
result.append( plainText.at( i ) );
cursor.movePosition( QTextCursor::NextCharacter );
continue;
}
if( cursor.charFormat().fontWeight() == QFont::Bold
&& tcFormat.fontWeight() != QFont::Bold )
{
result.append( plainText.at( i ) );
result.append( "[b]" );
tcFormat = cursor.charFormat();
cursor.movePosition( QTextCursor::NextCharacter );
continue;
}
else if( ( cursor.charFormat().fontWeight() != QFont::Bold || cursor.atEnd() )
&& tcFormat.fontWeight() == QFont::Bold )
{
result.append( plainText.at( i ) );
result.append( "[/b]" );
tcFormat = cursor.charFormat();
cursor.movePosition( QTextCursor::NextCharacter );
continue;
}
if( cursor.charFormat().fontItalic() && !tcFormat.fontItalic() )
{
result.append( plainText.at( i ) );
result.append( "[i]" );
tcFormat = cursor.charFormat();
cursor.movePosition( QTextCursor::NextCharacter );
continue;
}
else if( ( !cursor.charFormat().fontItalic() || cursor.atEnd() )
&& tcFormat.fontItalic() )
{
result.append( plainText.at( i ) );
result.append( "[/i]" );
tcFormat = cursor.charFormat();
cursor.movePosition( QTextCursor::NextCharacter );
continue;
}
if( cursor.charFormat().fontUnderline() && !tcFormat.fontUnderline() )
{
result.append( plainText.at( i ) );
result.append( "[u]" );
tcFormat = cursor.charFormat();
cursor.movePosition( QTextCursor::NextCharacter );
continue;
}
else if( ( !cursor.charFormat().fontUnderline() || cursor.atEnd() )
&& tcFormat.fontUnderline() )
{
result.append( plainText.at( i ) );
result.append( "[/u]" );
tcFormat = cursor.charFormat();
cursor.movePosition( QTextCursor::NextCharacter );
continue;
}
result.append( plainText.at( i ) );
cursor.movePosition( QTextCursor::NextCharacter );
}
return result;
}das problem: die bb-tags ( , , , , , ) stehen immer eine Stelle zuweit im result-string.
ich habe aber keine ahnung, wieso.
hoffe, mir kann geholfen werden, danke im voraus.
mfg,
julian