Results 1 to 2 of 2

Thread: QTextEdit colored text, again...

  1. #1
    Join Date
    Aug 2013
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default QTextEdit colored text, again...

    Hello,

    I'm looking for a solution to append single characters to QTextEdit with different colors.
    I tried the following:

    Qt Code:
    1. QTextCursor curs(textCursor());
    2. fmt.setForeground(QBrush(Qt::red));
    3. curs.setCharFormat(fmt);
    4. curs.movePosition(QTextCursor::End);
    5. curs.insertText(QString(chr));
    To copy to clipboard, switch view to plain text mode 
    does not work

    Qt Code:
    1. setTextColor(kolor1);
    2. append(QString(chr));
    To copy to clipboard, switch view to plain text mode 
    does not work either

    The only thing that actually works, is the <font> tag
    Qt Code:
    1. append(QString("<font color=red>%1</font>").arg(chr));
    To copy to clipboard, switch view to plain text mode 

    The problem is, I'm getting the input char by char, and cannot combine it into QString easily; every new character could have a different color. I would like to find a way other than tagging every single character (which would yield tons of garbage text not seen by the user, and would be probably not speed efficient).

    It would be nice, if QTextEdit remembers the last color setting, and appends new characters with that color...

  2. #2
    Join Date
    Sep 2011
    Posts
    86
    Thanks
    4
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QTextEdit colored text, again...

    As far as I know it's not possible. You can either set background/text color for entire QTextEdit or use html as You have done. It is possible to create function that will wrap Your char into html and set random color and function that will take back only char from html. The second is easy. You can take entire text from QTextEdit, put it in QTextDocument object and use function toPlainText(), then you have Your text as QString without any html.

Similar Threads

  1. Get text from QTextEdit with CR LF
    By arcull in forum Qt Programming
    Replies: 11
    Last Post: 10th September 2015, 12:48
  2. how to get the text from QTextEdit
    By qt_user in forum Qt Programming
    Replies: 5
    Last Post: 22nd August 2011, 00:22
  3. Write colored text
    By wirasto in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2010, 12:55
  4. QTextEdit::text()
    By hazardpeter in forum Newbie
    Replies: 2
    Last Post: 5th August 2009, 21:13
  5. QTextEdit + paste rich text as plain text only
    By Yong in forum Qt Programming
    Replies: 2
    Last Post: 6th February 2008, 16:45

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.