QT---Set a character format in a TextEdit text box

Source: Internet
Author: User

It's been a long time, and it's finally out. Qt comes with the help system is really good, the online search for so long data, also did not find specific or vague solution. Just say with HTML or with Qtextformat and various derived classes to change the character format in the text box, nnd, brother also know can use this to achieve. There is no more concrete. Cut the crap, record it, and you'll need it later. The format is used here. The specific function is to modify the line of the cursor in the first POS character format, where the format is to give the word multibyte underline. Other formats, such as character color and background color, are the same, so change format.
voidWidget:: Setcharcolor (unsigned intPOS)
{
if (Pos<=0) return ;
Qtextcursorcursor=UI,view1->textcursor ();
Cursor.moveposition (qtextcursor::startofline); //Beginning of the line
    Cursor.moveposition (qtextcursor::Right,qtextcursor::  Moveanchor,pos-1); //Move right to POS
    Cursor.moveposition (qtextcursor::nextcharacter,qtextcursor ::keepanchor);
    UI,view1->settextcursor (cursor); //added     
    Qtextcharformatdefcharfmt=UI,view1- Currentcharformat ();
    Qtextcharformatnewcharfmt=defcharfmt;
    Newcharfmt.setfontunderline (true);
    Newcharfmt.setunderlinecolor (qcolor(Qt::red) );
    Newcharfmt.setunderlinestyle (qtextcharformat::singleunderline);
    UI,view1->setcurrentcharformat (newcharfmt);
    Cursor.moveposition (qtextcursor::previouscharacter); //Add this sentence to remove the cursor selected
    UI,view1->settextcursor (cursor);  //added
 ui->view1->setcurrentcharformat (defcharfmt)       ;
    UI,view1->setfocus ();
}
Common formats:
"Newcharfmt.setbackground (qcolor(" #EEEE00 "));"
"Newcharfmt.setfontpointsize (fontSize);"
"Newcharfmt.setfontweight (qfont::Bold);"
"Highlightedformat.setbackground (Qt::yellow);"
"Newcharfmt.setforeground (Qt::red);"
Note:"The above operation will promote the TextChanged slot function, so use the slot function when the attention, add a sign to judge the better"
Description:"curso.movepositon,,,//plus this sentence ... ed"
When not added, the effect is as follows:
Add the effect as follows:
Report:

Copyright NOTICE: This article for Bo Master [original] article, without BO Master permission can be reproduced, annotated blog Source: [Http://blog.csdn.net/FreeApe]

QT---Set a character format in a TextEdit text box

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.