"QT Programming" HTML special characters and regular expressions

Source: Internet
Author: User

1. Display of special HTML characters

We know that the HTML language and the C language also have some special characters, they are not normal display, must be escaped, online can find out how to display these characters, as shown in:


Given the display of the most commonly used special characters, let's experiment with this: first write a sentence in Notepad:
<font color=blue> Program Examples </font> #include <stdio.h>
then change the text suffix name to. html, opened in a browser, appears as follows:


We can find that the include is not shown later, but when we write in the text:

<font color=blue> Program Examples </font> #include <stdio.h>   

can be displayed normally. Display results such as:


We know that QT can support HTML language, but I am writing can query the C language function (there must be a display problem with the special characters of <> after # # #), and I find that using the above method still cannot display special characters, later Brainwave, decided Use full-width <> instead of half-width <>. This will not be considered a special character, it can be displayed normally. Half-width and full-width conversions require only the 3rd button in a click:


Although it seems that the use of full-width is not very coordinated, but also see the past, if the reader has any good method, please advise.
2. Regular ExpressionsThen the above question goes on, for example, I have a C dictionary library, and I take one for example:
<font color=blue> Program Example </font>:<br> #include <span style= "color: #ff0000;" ><</span>stdio.h <span style= "color: #ff0000;" >></span><br> #include <span style= "color: #ff0000;" ><</span> stdlib.h <span style= "color: #ff0000;" >></span><br>int main (void) <br>
Because we need to convert the <> of the half angle to the full-width <>, and just replace the half corner in the header file <>that is, the red part of the string。 Of course, when the file is small, you can manually find the replacement, but since the dictionary has half more than 10 m, then you must use regular expressions.
In Qt, Qregexp is used to support regular expressions. On the syntax of regular expressions, there are a lot of tutorials on the web, I don't elaborate. From the contents of the above instance string, we know that there are several header files, so we match each header file to match the half-width <> that we're going to use non-greedy mode, otherwise the > of the first header file will be < matched to the last header file. We know that in regular expressions, the non-greedy pattern is made up of '? ' To specify, but for the Qregexp class,? is not legal. in Qregexp, you use the Setminimal function to specify greedy modeSetminimal (TRUE) is a non-greedy mode, and Setminimal (false) is greedy mode.
Through the above two parts of the explanation, you can complete the C Language function library dictionary display. The following code implementation, the new QT GUI application, select the base class for Qwidget, which only needs to modify the contents of the Widget.cpp (need to add two labels in the Widget.ui interface, respectively, named label and Label1). Widget.cpp file:
#include "widget.h" #include "ui_widget.h" #include <QString> #include <QRegExp> #include <qdebug>#     Include<qlabel>widget::widget (Qwidget *parent): Qwidget (parent), UI (new Ui::widget) {ui->setupui (this); QString str= "<font color=blue> program examples </font>:<br> #include <stdio.h ><br> #include <    Stdlib.h ><br>int Main (void) <br> ";  Ui->label->settext (str); Since QT also recognizes HTML, < > is a special character in HTML, so if you do not process it, you will see an error qregexp Rx ("#include (< (. *.h) >)");//        The parentheses in the regular expression are to get the matching content rx.setminimal (true);//non-greedy mode int pos=0;//from the beginning of the string for (int i=0;i<str.size (); i++) { Pos=rx.indexin (str,0);//Get match string position while (POS!=-1)//Match Success {str.replace (Rx.cap (1), "" "+rx.cap (2) +" > "); Here is the replacement, if you do not understand what rx.cap is, you can qdebug display the content pos=rx.indexin (Str,pos+rx.matchedlength ());//from the current position, continue to match}}ui-& Gt;label1->settext (str);} Widget::~widget () {Delete UI;}
The results of the program run as follows:



Here is a look at the word software used to display and use the special characters after the regular expression:



Note: I use the platform for QT5, if the transfer to QT4 on the wrong, you can reverse reference http://qt-project.org/wiki/Transition_from_Qt_4.x_to_Qt5






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.