Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, November 2007
Text Background color


Message 1 in thread

Hello

I have a question regarding the background colours of a QTextDocument Class. 
Is it possible to go through the text, character by character, and is it 
possible to retrieve the background colour of the current character?

Right now I'm marking the text with the following code:
QTextDocument * fullText;
void TextView::markText (QRegExp expr, QColor color)
{
	QTextCharFormat format;
	QTextCursor	foundCursor;
	format.setBackground (QBrush(color));

	foundCursor = fullText->find (expr);
	for (;!foundCursor.isNull();foundCursor = fullText->find (expr, foundCursor))
	{
		foundCursor.setCharFormat (format);
	}
}

but after all the marking is done i want to delete scopes that have a specific 
colour. Deleting them instead of marking would not help because some regular 
expressions are overlapping and if the text is gone the regular expression 
would not work anymore.

Regards Simon

--
 [ signature omitted ] 

Message 2 in thread

Well here I am answering my own question but still have something that looks 
like a bug or perhaps someone can tell me what I am doing wrong. I produced 
the following solution:

void TextView::removeMarkedWords ()
{
	QTextDocument * tmpText = fullText->clone();
	QTextCursor * tmp = new QTextCursor(tmpText);
	for (int i = 0; !tmp->atEnd();i++)
	{
		tmp->setPosition (i);
		if (tmp->charFormat().background () == invalidColor)
		{
			tmp->deleteChar ();
			i--;
		}
	}
	showDebugTextEdit (tmpText);
}

so every time a character with an invalid background colour is found it will 
be deleted and the position counter will reduced by one (because the char is 
gone so we have to check this position again). So far so nice.
I tested it with a few of my texts, on long texts (around ~62000 chars) it is 
working real good, but on smaller texts its not working after a small amount 
of chars the cursor stays at position x but starts deleting on position x+1. 
That is not very good because position x is invalid all the time so 
everything after position x will be deleted.
My question for now is, do I have an error in my solution above, or might it 
be a qt bug?

Regards
Simon

Am Samstag, 3. November 2007 21:58:00 schrieb Simon Schäfer:
> Hello
>
> I have a question regarding the background colours of a QTextDocument
> Class. Is it possible to go through the text, character by character, and
> is it possible to retrieve the background colour of the current character?
>
> Right now I'm marking the text with the following code:
> QTextDocument * fullText;
> void TextView::markText (QRegExp expr, QColor color)
> {
> 	QTextCharFormat format;
> 	QTextCursor	foundCursor;
> 	format.setBackground (QBrush(color));
>
> 	foundCursor = fullText->find (expr);
> 	for (;!foundCursor.isNull();foundCursor = fullText->find (expr,
> foundCursor)) {
> 		foundCursor.setCharFormat (format);
> 	}
> }
>
> but after all the marking is done i want to delete scopes that have a
> specific colour. Deleting them instead of marking would not help because
> some regular expressions are overlapping and if the text is gone the
> regular expression would not work anymore.
>
> Regards Simon
>

--
 [ signature omitted ]