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
    Thanked 1 Time in 1 Post
    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
    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 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
    Thanked 1 Time in 1 Post
    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.

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

    Hossein (24th October 2015)

Similar Threads

  1. Replies: 12
    Last Post: 30th May 2009, 15:29
  2. Problem pasting text into a QTextEdit
    By Spockmeat in forum Qt Programming
    Replies: 8
    Last Post: 14th March 2009, 15:36
  3. Replies: 2
    Last Post: 24th January 2009, 19:30
  4. QTextEdit and font family
    By giusepped in forum Qt Programming
    Replies: 6
    Last Post: 4th June 2008, 11:17
  5. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 07: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.