Qt5 development and case study 13 Qt5 text editing function

preface   Qt classes often used when writing text editing programs containing formatting include QTextEdit, QTextDo...

preface

  Qt classes often used when writing text editing programs containing formatting include QTextEdit, QTextDocument, QTextBlock, QTextList, QTextFrame, QTextTable, QTextCharFormat, QTextBlockFormat, QTextListFormat, QTextFrameFormat and QTextTableFormat.
  the division and relationship between various types of text editor are shown in the following figure:

   any text editor program should use QTextEdit as the container for inputting text. The editable text is input in it, and QTextDocument is used as the carrier. QTextBolck, QTextList, QTextFrame, etc. used to represent the elements of QTextDocument are different manifestations of QTextDocument, which can be expressed as string, paragraph, list Tables or pictures, etc.
  each element has its own format, which is described and implemented by QTextCharFormat, QTextBlockFormat, QTextListFormat, QTextFrameFormat and other classes. For example, the QTextBlockFormat class corresponds to the QTextBlock class. The QTextBlock class is used to represent a piece of text, which can generally be understood as a paragraph, but it does not only refer to a paragraph; QTextBlockFormat class represents the format of this piece of text, such as indented value, margin with four sides, etc.
  as can be seen from the above figure, the QTextCursor class used to represent the cursor in editing text is a very important and frequently used class. It provides an interface for modifying QTextDocument documents. All modifications to document formats are related to the cursor in the final analysis. For example, changing the format of a character actually means changing the format of the character at the cursor. For another example, changing the format of a paragraph actually means changing the format of the paragraph where the cursor is located. Therefore, all modifications to QTextDocument can be implemented through QTextCursor class, which plays an important role in document editing programs.

  call the setFontFilters interface of QFontComboBox to filter. Only a certain type of font is displayed in the drop-down list box. By default, all fonts are listed for QFontComboBox::AllFonts.
   use QFontDatabase to fill various font size entries in the font size drop-down list box. QFontDatabase class is used to represent all available format information in the current system, mainly font and font size.
  call the standardSizes() function to return a list of available standard font sizes and insert them into the font size drop-down list box.

   foreach is a keyword provided by Qt to replace the for loop in C + +. Its usage is as follows:
  foreach(variable, container): where the parameter variable is used to represent the variable of each element; The parameter container indicates a list that needs to be read circularly in the program; For example:

foreach(int , QList<int>) { //process }

Loop to end of list.

1, Set font

   the function to complete the setting of the selected text font is as follows:

void ImgProcessor::showFontComboBox(QString comboStr) //Set font { QTextCharFormat fmt; //Create a QTextCharFormat object fmt.setFontFamily(comboStr); //The selected font name is set to the QTextCharFormat object mergeFormat(fmt); //Applies the new format to the characters in the cursor selection }

  all modifications to QTextDocument are completed through QTextCursor class. The code is as follows:

void ImgProcessor::mergeFormat(QTextCharFormat format) { QTextCursor cursor = showWidget->text->textCursor();//Gets the cursor in the edit box if(!cursor.hasSelection()) //If the cursor does not highlight the selected area, take the word where the cursor is located as the selected area, and distinguish the word by the front and back spaces or punctuation marks such as "," " cursor.select(QTextCursor::WordUnderCursor); cursor.mergeCharFormat(format); //Call the mergeCharFormat() function of QTextCursor to apply the format represented by the parameter format to the character where the cursor is located showWidget->text->mergeCurrentCharFormat(format); //Call the mergeCurrentCharFormat() function of QTextEdit to apply the format to all characters in the selection }
2, Set font size

  the function code for setting the font size of the selected text is as follows:

void ImgProcessor::showSizeSpinBox(QString spinValue) //Set font size { QTextCharFormat fmt; fmt.setFontPointSize(spinValue.toFloat()); showWidget->text->mergeCurrentCharFormat(fmt); }
3, Set text bold

   set the selected text to bold, and the function code is as follows:

void ImgProcessor::showBoldBtn() //Set text display bold { QTextCharFormat fmt; fmt.setFontWeight(boldBtn->isChecked()?QFont::Bold:QFont::Normal); showWidget->text->mergeCurrentCharFormat(fmt); }

The setFontWeight() function that calls QTextCharFormat sets the thickness value. If the "bold" button is pressed, the Weight value of the character set is QFont::Bold, which can be set to 75 directly. Otherwise, it is set to QFont::Normal.
The thickness value of     text is represented by QFont::Weight. It is an integer value with a value range of 0 ~ 99. There are five preset values: QFont::Light(25), QFont::Normal(50), QFont::DemiBold(63), QFont::Bold(75) and QFont::Black(87). It is usually converted between QFont::Normal and QFont::Bold.

4, Set Text Italic

   set the selected text to be displayed in italics. The function code is as follows:

void ImgProcessor::showItalicBtn() //Set text display Italic { QTextCharFormat fmt; fmt.setFontItalic(italicBtn->isChecked()); showWidget->text->mergeCurrentCharFormat(fmt); }
5, Set text underline

   the function code of adding a line below the selected text is as follows:

void ImgProcessor::showUnderlineBtn() //Underline text { QTextCharFormat fmt; fmt.setFontUnderline(underlineBtn->isChecked()); showWidget->text->mergeCurrentCharFormat(fmt); }
6, Set text color

  the function code for setting the selected text color is as follows:

void ImgProcessor::showColorBtn() //Set text color { QColor color = QColorDialog::getColor(Qt::red, this); //(a) if(color.isValid()) { QTextCharFormat fmt; fmt.setForeground(color); showWidget->text->mergeCurrentCharFormat(fmt); } }

(a) The standard color dialog box is used. When you click the color button, select the color in the pop-up standard color dialog box.
  use of QColorDialog class of standard color dialog box:

QColor getColor ( const QColor& initial=Qt::white, QWidget* parent=0 );

   the first parameter specifies the selected color, which is white by default. You can judge whether the color selected by the user is valid through QColor::isValid(); If the user clicks the Cancel button, QColor::isValid() returns false.
  the second parameter defines the parent window of the standard color dialog box.

7, Format character

   call this slot function when the character format at the cursor changes. The function updates the display of each format control on the toolbar according to the new character format.

void ImgProcessor::showCurrentFormatChanged(const QTextCharFormat &fmt) { fontComboBox->setCurrentIndex(fontComboBox->findText(fmt.fontFamily())); sizeComboBox->setCurrentIndex(sizeComboBox->findText(QString::number(fmt.fontPointSize()))); boldBtn->setChecked(fmt.font().bold()); italicBtn->setChecked(fmt.fontItalic()); underlineBtn->setChecked(fmt.fontUnderline()); }

4 November 2021, 02:12 | Views: 4576

Add new comment

For adding a comment, please log in
or create account

0 comments