PDA

View Full Version : how to color certain words in a textedit



chandrabose.s
8th March 2006, 14:13
hi
i'm using qt3.0.i have to color certain strings/keywords in a textedit while opening the file as well as adding characters in a newly opened file which is similar to qteditor environment.
And also i have to parse the file for the words which should match the predefined words.
for eg.: ScanIn,ScanOut
i have to color the above two words and also check for predefined words,if it is not in the list i have to give an error message.
Please help me,its very urgent.
Give some example coding to do the task.
thanks in advance,
bose.

munna
8th March 2006, 15:27
Set the text format of your text edit to rich text and then add html tags for changing the color of the text around your keywords.

cheers

chandrabose.s
9th March 2006, 05:18
hi munna
thanks for your reply.could you please give me a sample coding.
thanks
bose.

wysota
9th March 2006, 09:37
You should take a look at QSyntaxHighlighter class.

chandrabose.s
11th March 2006, 11:22
hi wysota sir,
the main objective of my application is to check the keywords,please help me regarding
that part.
thank you.

jpn
11th March 2006, 15:53
hi wysota sir,
the main objective of my application is to check the keywords,please help me regarding
that part.
thank you.
Did you read QSyntaxHighlighter documentation?
There is even an example (http://doc.trolltech.com/4.1/qsyntaxhighlighter.html#details) of the intended usage.

Inherit your syntax highlighter from QSyntaxHighlighter and implement:
void QSyntaxHighlighter::highlightBlock ( const QString & text ) [pure virtual protected] (http://doc.trolltech.com/4.1/qsyntaxhighlighter.html#highlightBlock)

chandrabose.s
13th March 2006, 12:39
hi jpn
actually i'm using Qt3.0.3 and in that version there is no any member named highlightblock() in QSyntaxhighlighter class.Anyway i will try using the highlightparagraph() in Qt3.0.3 version.

chandrabose.s
14th March 2006, 14:05
hi
can anyone help me how to instantiate the QSyntaxHighlighter() class,and for coloring the words.
please reply, i need someones help.

jpn
14th March 2006, 14:20
Sorry for missing the point that you were using Qt3.. ;)

But hmm, it seems that QSyntaxHighlighter wasn't introduced until in Qt 3.1.0?

chandrabose.s
15th March 2006, 07:14
hi jpn
help me how can i instantiate the QSyntaxhighlighter class.
In Qt3.0.3 how can i color the text,is it possible if so please give me a sample coding.
i'm hanging with this problem please any one help me.

munna
15th March 2006, 07:30
Hi,

In your document search for every instance of your keyword and then around that word add a HTML tag which will give a different color to it.

Example :

QtCenter is the ultimate forum.

If you want a different color for 'ultimate', then search for ' ultimate ' (inculde space so that you get whole words) and replace it with '<font color = 'your color'> ultimate </font>.

Now the line is :

QtCenter is the <font color = 'your color'> ultimate </font> forum.

Set your text edit's format as RICH TEXT.

cheers

jpn
15th March 2006, 09:16
help me how can i instantiate the QSyntaxhighlighter class
Unfortunately it seems to be impossible. As I mentioned in my previous post, QSyntaxHighlighter wasn't introduced until in a later Qt version, 3.1.0.
Changes 3.1.0 (http://www.trolltech.com/developer/changes/changes-3.1.0.html)

So I guess you will have to simply manually search for the occurences of the keywords, as munna suggested.

chandrabose.s
15th March 2006, 10:24
hi
fine munna, thats working.i'll try using that to implement my code.