“This is the seventh day of my participation in the First Challenge 2022.

The previous chapter explained how to create a text editing page and what functions should be implemented, just for presentation, not for actual click events. Today, let’s explain how to implement the corresponding requirements on the menu bar and toolbar

Function implementation

Function:

1. Action message triggering

2. Specific functions: open files, create new files, copy, cut and paste

3. Text editing function: font setting

Today we will only talk about the above three functions, as for: rotation, zooming and other functions are for pictures, and so on, the next chapter, the specific way of explanation is not the function of the text editor, wait for the subsequent more text!

Function 1: Action message

Now that the actions from the previous chapter have been created, how do you trigger them?

In Qt, actions are triggered the same way controls are triggered. For example, to get a button control to respond to an action, you need to respond to a QPushButton:: Clicked message.

This is triggered by QAction::triggered.

The slot functions corresponding to each action are as follows

Open the file

connect(m_pActionOpenFile, &QAction::triggered, this, &QtTextEditor::OnTriggeredOpenFile);
Copy the code

The new file

connect(m_pActionOpenFile, &QAction::triggered, this, &QtTextEditor::OnTriggeredNewFile);
Copy the code

Copy, cut and paste

connect(m_pActionOpenFile, &QAction::triggered, m_editContent, &QTextEdit::copy);

connect(m_pActionOpenFile, &QAction::triggered, m_editContent, &QTextEdit::cut);

connect(m_pActionOpenFile, &QAction::triggered, m_editContent, &QTextEdit::paste);
Copy the code

See the slot function, you can see, for the copy, cut, paste function is the implementation of the QTextEdit control, there is no need to do new processing, directly use the parent class message can be.

In order to handle the function of editing text class, the best way to inherit from QTextEdit for function encapsulation, let’s say: QMyTextEdit, the subsequent use of this class as editing text class

Function 2: Action event implementation

For the above two need to achieve their own slot function response, the next will be detailed about it ~

2.1: Open the file

Open the file, represents the open an existing file, here need QFileDialog class, using QFileDialog: : getOpenFileName () static function, return to select the file path the full path name.

void QtTextEditor::OnTriggeredOpenFile() { QString qsFileName = QFileDialog::getOpenFileName(this); if (qsFileName.isEmpty()) { return; } if (m_editContent->document()->isEmpty()) { this->LoadFileContent(m_editContent, qsFileName); } else { QMyTextEdit *editContent = new QMyTextEdit; editContent->show(); this->LoadFileContent(editContent, qsFileName); m_vetEditCtrls.push_back(editContent); }}Copy the code

Code thread:

Obtain the path of the open file. If the returned string is empty, no valid file is opened and no processing is performed.

Does the current Edit control have data before displaying the content? If not displayed directly in the current text edit control;

If the current text edit control has data, create a new QMyTextEdit custom class to display the new content.

LoadFileContent LoadFileContent

void QtTextEditor::LoadFileContent(QMyTextEdit* editCtrls, QString qsFileName) { QFile file(qsFileName); if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream textStream(&file); while (! textStream.atEnd()) { editCtrls->append(textStream.readLine()); }}}Copy the code

Code description:

QTextStream is used to read file stream data, which can easily read and write words, lines and numbers, and even provides options for padding, alignment and number formatting.

Why is the first argument passed in a class pointer?

Because there is a function called new, when reading the file, assuming that there is content in the text editor class QMyEdit, it needs to be created again. For the sake of unity, the first parameter is set to class pointer, convenient function call.

Either a newly created page or an existing page can write text.

2.2: Creating a file

Here the so-called new is to create a blank edit text, the function is simple, there is no more explanation here.

void QtTextEditor::OnTriggeredNewFile()
{
	QMyTextEdit *editCtrls = new QMyTextEdit;
	editCtrls->show();
	m_vetEditCtrls.push_back(editCtrls);
}

Copy the code

The only caveat is that when you create a new text class, it is important to remember to store the class pointer in case it is unsearchable in future use and memory leaks during destruction.

2.3: Copy, cut and paste

All three of these functions follow the QTextEdit built-in function, here without further explanation, directly respond to the parent class message is ok.

Feature 3: Text editing

We have used the text editor, the main function is still the operation of the text, so here is mainly about the text toolbar and text display style

3.1 Text Toolbar

As described in the previous chapter, you are familiar with the use of the toolbar. Here you need to create a font toolbar and include the following functions:

function The name of the The name of the class
Font style m_ComboFontStyle QFontComboBox
Font selection box m_ComboFontSize QComboBox
bold m_btnFontBold QToolButton
Font tilt m_btnFontItalic QToolButton
Font color value m_btnFontColor QToolButton

The actual results are as follows:

For font Settings, inconsistent with the content displayed on other toolbar, no longer using actions, but using controls

The table above shows the class name for each function, but the key is to get the font style and font size

The font style

QFontCombobox is a subclass of QComboBox, which cannot be edited and can only be used to select fonts.

m_ComboFontStyle = new QFontComboBox;
m_ComboFontStyle->setFontFilters(QFontComboBox::ScalableFonts);
Copy the code

Font size

QFontDatabase, which provides information about the font available in the underlying window system, is mainly used to query the font size.

m_ComboFontSize = new QComboBox;
QFontDatabase dbFont;
foreach(int size, dbFont.standardSizes())
{
	m_ComboFontSize->addItem(QString::number(size));
}
Copy the code

Code parsing:

Foreach = foreach = foreach = foreach = foreach = foreach = foreach = foreach = foreach = foreach = foreach

StandarSizes function, which returns a list of available standard sizes and inserts the list into the QCombobox.

Note: QToolButton is used the same way as QPushButton, except that the class name is changed, and the creation method and message binding are not explained too much.

Finally, bind the controls you create to the font toolbar

m_pToolFont = addToolBar("Font");
m_pToolFont->setAllowedAreas(Qt::BottomToolBarArea );
m_pToolFont->addWidget(m_labFontStyle);
m_pToolFont->addWidget(m_ComboFontStyle);
m_pToolFont->addWidget(m_labFontSize);
m_pToolFont->addWidget(m_ComboFontSize);
m_pToolFont->addSeparator();
m_pToolFont->addWidget(m_btnFontBold);
m_pToolFont->addWidget(m_btnFontItalic);
m_pToolFont->addSeparator();
m_pToolFont->addWidget(m_btnFontColor);
Copy the code

So if you look at this, you can try out your own code and see if it works.

The important thing to note here is: Why does it run the way it does?

Does anybody have that? Why is it different from the first sketch? Instead of a new line, what if I couldn’t see anything?

In the ToolBar, if you need to tell QToolBar to start a new line if the line is not fully displayed, add the following code before addToolBar:

this->addToolBarBreak(Qt::TopToolBarArea);
Copy the code

Run it again, it will be two lines of toolbar, tread pit record, hope you do not make this mistake ~

3.2: Font style

In this case, the font style is nothing more than what is displayed on the toolbar.

The text style part uses the QTextCharFormat object to achieve

The font style

void QtTextEditor::OnComboFontStyle(QString& qsText) { QTextCharFormat fmt; fmt.setFontFamily(qsText); QTextCursor cursor = m_editContent->textCursor(); if (! cursor.hasSelection()) { cursor.select(QTextCursor::WordUnderCursor); } cursor.mergeCharFormat(fmt); m_editContent->mergeCurrentCharFormat(fmt); }Copy the code

Code parsing:

Gets the focus of the control and sets the font style style by specifying the word in the cursor area as the highlight selected word, assuming that the text on the current control is not selected

The shop name

fmt.setFontPointSize(qsText.toInt());
Copy the code

bold

fmt.setFontWeight(bCheck);
Copy the code

italics

fmt.setFontItalic(bchecked);
Copy the code

The above several functions, the usage is very similar, as I mentioned before, are using the QTextCharFormat class, but the function is different, the specific function is not to do too much description, the following, compared with the above several functions, need to explain is: color Settings.

The font color

The system file class QColorDialog is used for this color, similar to the way you open a file, but with a different class name

void QtTextEditor::OnToolFontColor(bool bchecked) { QColor color = QColorDialog::getColor(Qt::red, this); if (color.isValid()) { QTextCharFormat fmt; fmt.setForeground(color); m_editContent->mergeCurrentCharFormat(fmt); }}Copy the code

According to the above functions, the specific display effects are as follows:

conclusion

Now that the text style is done, the most important class in the whole setup is QTextCharFormat.

And as you go through this code, you’ll notice that I’m always operating on m_editContent, which is drip!

In fact, there is a new function, here new blank text word processing function to everyone, this is just to do a demonstration example, in advance, it is not very difficult, as long as you master the current active QMyEdit class pointer can be.

Today’s content is here, I am a good citizen of China st, a C++ program ape ~