Results 1 to 13 of 13

Thread: how to color certain words in a textedit

  1. #1
    Join Date
    Feb 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default how to color certain words in a textedit

    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.
    Last edited by chandrabose.s; 8th March 2006 at 14:18.

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to color certain words in a textedit

    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

  3. #3
    Join Date
    Feb 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: how to color certain words in a textedit

    hi munna
    thanks for your reply.could you please give me a sample coding.
    thanks
    bose.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to color certain words in a textedit

    You should take a look at QSyntaxHighlighter class.

  5. The following user says thank you to wysota for this useful post:

    chandrabose.s (11th March 2006)

  6. #5
    Join Date
    Feb 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: how to color certain words in a textedit

    hi wysota sir,
    the main objective of my application is to check the keywords,please help me regarding
    that part.
    thank you.

  7. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: how to color certain words in a textedit

    Quote Originally Posted by chandrabose.s
    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 of the intended usage.

    Inherit your syntax highlighter from QSyntaxHighlighter and implement:
    void QSyntaxHighlighter::highlightBlock ( const QString & text ) [pure virtual protected]

  8. #7
    Join Date
    Feb 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: how to color certain words in a textedit

    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.

  9. #8
    Join Date
    Feb 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: how to color certain words in a textedit

    hi
    can anyone help me how to instantiate the QSyntaxHighlighter() class,and for coloring the words.
    please reply, i need someones help.

  10. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: how to color certain words in a textedit

    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?

  11. #10
    Join Date
    Feb 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: how to color certain words in a textedit

    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.
    Last edited by chandrabose.s; 15th March 2006 at 08:04.

  12. #11
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to color certain words in a textedit

    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

  13. The following user says thank you to munna for this useful post:

    chandrabose.s (15th March 2006)

  14. #12
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: how to color certain words in a textedit

    Quote Originally Posted by chandrabose.s
    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

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

  15. #13
    Join Date
    Feb 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: how to color certain words in a textedit

    hi
    fine munna, thats working.i'll try using that to implement my code.

Similar Threads

  1. visible text of textedit
    By regix in forum Qt Programming
    Replies: 3
    Last Post: 26th June 2006, 10:02

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.