+ Reply to Thread
Results 1 to 3 of 3

Thread: How to set text and font in qTextEdit?

  1. #1
    Join Date
    Sep 2009
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default How to set text and font in qTextEdit?

    Hi,

    My very first Qt application calls for a text display, built by the application. Parts of it needs to be in color.

    How can I:

    -set the color or font of text in qTextEdit, by line&char? For example, start from line 5, character 4 and make next 10 characters bold with gray background?

    -replace said characters with some other text, keeping the formatting?

    And finally, am I using the right widget in the first place?

    Thank you!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    31,199
    Thanks
    3
    Thanked 4,694 Times in 4,495 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: How to set text and font in qTextEdit?

    Look at QTextCursor and QTextCharFormat classes.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    JuKu (28th September 2009)

  4. #3
    Join Date
    Sep 2009
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to set text and font in qTextEdit?

    Thank you! The Qt documentation is very good, but there is lots of it. Your keywords pointed me to the right direction.

    To save other noobies an hour or two, here is where the keys were found:
    All Overviews and HOWTOs in general and especially, Supported HTML Subset. I hadn't realized the qTextEdit used HTML inside!

    Here is some example code:
    Qt Code:
    1. ui->TextEdit->setText("1234567890<br>1234567890<br>line3");
    2. QTextCursor cursor(ui->TextEdit->document());
    3. cursor.setPosition(15, QTextCursor::MoveAnchor); // Go to 4th character of second line; the <br> counts as one character.
    4. cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 3); // select three charaters
    5. QString s=cursor.selectedText();
    6. cursor.insertHtml("<FONT color=green style=\"BACKGROUND-COLOR: yellow\">"+s+"</FONT>"); // make them ugly
    To copy to clipboard, switch view to plain text mode 
    The more familiar I get with Qt, the more I like it.

+ Reply to Thread

Similar Threads

  1. Replies: 12
    Last Post: 30th May 2009, 14:29
  2. Problem pasting text into a QTextEdit
    By Spockmeat in forum Qt Programming
    Replies: 8
    Last Post: 14th March 2009, 14:36
  3. Replies: 2
    Last Post: 24th January 2009, 18:30
  4. QTextEdit and font family
    By giusepped in forum Qt Programming
    Replies: 6
    Last Post: 4th June 2008, 10:17
  5. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 06:03

Tags for this Thread

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