Results 1 to 10 of 10

Thread: Change colour of QString

  1. #1
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Change colour of QString

    I need help with setting the colour of a string to be green in the beginning and red at the end.
    The problem right now is that spaces that occur between the two colours disapear.
    The code looks like this:

    Qt Code:
    1. QString input = .. what ever the user inputs
    2. QString answer = "foo bar";
    3. QString temp="<font color=\"#00FF00\">";
    4. int i=0;
    5. while (i<input.size()&& i<answer.size() && input[i]==answer[i])
    6. {
    7. temp=temp+input[i];
    8. i++;
    9. }
    10. temp=temp+"</font><font color=\"#FF0000\">"+answermid(i)+"</font>";
    To copy to clipboard, switch view to plain text mode 
    Last edited by Morea; 10th February 2006 at 13:42.

  2. #2
    Join Date
    Feb 2006
    Location
    Kirovohrad, Ukraine
    Posts
    72
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Change colour of QString

    The QString class provides a Unicode character string
    QString isn't responsible for the way it is displayed. Is is just a convinient container for the characters with a bunch of useful features. The way QString is shown depends on the widget which displays it. In your case it is QTextEdit.
    Qt Code:
    1. //The string you want to decorate
    2. QString s = "adadsfdafds";
    3. //The widget, which will display string
    4. //Format first 4 characters
    5. QString redPart = QString("<span style=" color:#ff0000;">%1</span>").arg(s.left(4));
    6. //Format the rest of the characters
    7. QString greenPart = QString("<span style=" color:#00ff00;">%1</span>").arg(s.mid(5));
    8. //Make resulting HTML appear on the QTextEdit
    9. te.setHtml(redPart + greenPart);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Change colour of QString

    Thanks for this answer Cesar, Will your code respect spaces in the text if they occur between the green and the red part?

  4. #4
    Join Date
    Feb 2006
    Location
    Kirovohrad, Ukraine
    Posts
    72
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Change colour of QString

    Quote Originally Posted by Morea
    I need help with setting the colour of a string to be green in the beginning and red at the end.
    The problem right now is that spaces that occur between the two colours disapear.
    Try this code... And ask your questions, if any
    Attached Files Attached Files

  5. #5
    Join Date
    Jan 2006
    Location
    Lincoln, NE USA
    Posts
    177
    Thanks
    3
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Change colour of QString

    Quote Originally Posted by Cesar
    Try this code... And ask your questions, if any
    It works for me if:
    "what ever I enter in the second box, which matches character for character what is in the first box, from right to left, is turned green in the display form"
    was what you intended.

  6. #6
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Change colour of QString

    But why doesn't the HTML code work with spaces?

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

    Default Re: Change colour of QString

    Because HTML trimms multiple whitespaces into one. If you want to keep them, transform each of the multiple spaces to "&nbsp;" (keep single ones as they are).

  8. #8
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Change colour of QString

    Thanks for all the help so far, but now I'm stuck. I get a crash when the program starts.
    The problem seems to be with the creation of the red/green part.
    The point of the program is to press enter, get a random text, and directly start writing the same thing (marked red/green if wrong/right).

    I attach a tgz with the code.
    It's produced with dev-c++ in windows.
    Attached Files Attached Files
    Last edited by Morea; 10th February 2006 at 21:30.

  9. #9
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Change colour of QString

    Ah, is it perhaps the infinite loop I get with all the textChanged signals that are emitted, and the KollaText() slot that receives it and then again emits a textChanged signal..
    It might be! But I doesn't seem possible to change to a keypressed event or something like that. Any ideas, or should I just use the code that cesar sent earlier?
    Last edited by Morea; 10th February 2006 at 21:39.

  10. #10
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Change colour of QString

    Now it works fine! I strongly recomend anyone using Dev-C++ to rebuild everything once in a while, otherwise you might have parts that is not properly updated.

Similar Threads

  1. Custom Model Advice Requested
    By mclark in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 16:26
  2. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59
  3. Replies: 2
    Last Post: 12th October 2006, 09:17
  4. Converting QString to unsigned char
    By salston in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 22:10

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.