“Live up to the time, the creation of non-stop, this article is participating in 2021 year-end summary essay competition”

How many edit box types are there in Qt?

QLineEdit, QTextEdit, QPlainTextEdit, QTextBrowser

So how do we use all these edit box controls? And what do they have in common?

1: universal Settings

Because these four controls are edit box type, so for the same function of the control there must be a certain similarity, first of all, let’s explain this general function!

1.1 Text display mode

The “setAlignment” function is used to set the display of text in Qt, of course, if you want to use QSS mode is also available.

For example:

ui.edit->setAlignment(Qt::AlignLeft); Edit ->setAlignment(Qt::AlignRight); Edit ->setAlignment(Qt::AlignCenter); // Center displayCopy the code

Note !!!! When using this feature, keep in mind that QPlainEdit is not supported

1.2 Can I edit it

This function is mainly readable

For example:

ui.edit->setReadOnly(true); / / read-only UI. Edit - > setReadOnlu (false); / / editableCopy the code

Note !!!!

When there is too much text, the scroll bar cannot be operated in read-only mode

QTextBrowser is a read-only function and does not need this function

1.3 Deselect the text

Setting this feature does not make the Edit control selected

ui.edit->setTextInteractionFlags(Qt::NoTextInteraction);
Copy the code

1.4: pay attention to

No matter which edit control, getting the number of lines in the edit box is invalid, I have tried this feature!

2: QLineEdit

QLineEdit is a single-line text editing control

2.1 style

2.2 Display Mode

QLineEdit has four ways to display text:

Normal, Password, PasswordEchoOnEdit, NoEcho

Note: PasswordEchoOnEdit

When the focus is on the control, the input text is displayed. When the control is out of focus, the text becomes the same as the Password display.

ui.edit->setEchoMode(QLineEdit::Normal);
Copy the code

2.3 Prompt Text

This feature is used to display the prompt text content displayed on Edit

Ui. edit->setPlaceholderText(QStringLiteral(test example! ));Copy the code

2.4 String mask

Using the inputMast() method, you can make the Edit control allow you to enter only custom format strings

The four Settings are as follows:

Year Settings:

UI. EditLine - > setInputMask (" 0000-00-00 ");Copy the code

Mask patterns

UI. EditLine - > setInputMask (" 000.000.000.000; _ ");Copy the code

Time pattern

UI. EditLine - > setInputMask (" HH: HH: HH: HH: HH: HH. _ ");Copy the code

Pattern 4

UI. EditLine - > setInputMask (" > AAAAA AAAAA AAAAA AAAAA AAAAA - - - -; # ");Copy the code

2.5 Adding ICONS

In general, ICONS are only added to controls like QPushButton, but the same applies to edit box controls.

Assumption: The icon is displayed on the left

QAction *searchAction = new QAction(ui.editLine);
searchAction->setIcon(QIcon(":/QtControl/image/2wm_p.png"));
ui.editLine->addAction(searchAction, QLineEdit::LeadingPosition);
Copy the code

Left: the LeadingPosition

Right: TrailingPosition

Display effect

2.6 Adding a Button

QPushButton *btn = new QPushButton(); QHBoxLayout *layout = new QHBoxLayout(); btn->setCursor(Qt::ArrowCursor); layout->addStretch(); layout->addWidget(btn); BTN - > setContentsMargins (0,0,0,0); ui.editLine->setlayout(layout);Copy the code

Display effect

If you want a specific style of button, just need to customize the Settings, here is just a simple function, provide ideas oh ~

2.7 Placeholder text color Settings

Setting Mode:

Here are some things to note:

Using the above method, you can change the color of the placeholder. If the placeholder is set to yellow, the color of the text will change to yellow instead of #333333.

In QLineEdit’s source code, the placeholder has the alpha property set, so you always see the placeholder appear darker.

3: QTextEdit

QTextEdit is used for multi-line text display and can also display HTML text

3.1 style

Unlike QLineEdit, QTextiEdit sets text background colors not to work in read-only mode!

3.2 Prompt Text

Unlike QLineEdit, there are no interface Settings that can be called directly in QTextEdit. You need the signal textChanged to monitor the input in real time to control the input

Signal:

Practical usage:

Where TextEdit_MAX_SIZE is the restricted input size

3.3 Multi-line color values

Code implementation:

Display effect

3.4 Adding a Picture

There are three ways to add images here

The first type: add directly through the URL

Second: add using QTextImageFormat

Third: use QTextDocumentFragment to add

4: QPlainTextEdit

Plain text edit box

4.1 style

5: QTextBrowser

The control cannot be edited for display only

1: there is no edit or not function, because it is a read-only property

2: Text alignment cannot be set and can only be displayed on the left

5.1 style

5.2 Text adaptation

5.3 Maximum number of lines to be displayed

5.4 The scroll bar is always displayed at the low end

5.5 the hyperlink

Case 1: Access local files

Case 2: Visit the web page

Note: file:/// is required at the beginning of the connection address to access the local file path

Used message

Practical application of this function

Click “Show Text” on the QTextBrower control to access the 001.mp4 video directly.

At this point, there is a problem, when the video is closed, the page will have a state of suspended animation, some garbled characters appear in the QTextBrower control.

The solution to this problem is to clean up the local information directly after accessing the local path.

conclusion

This is the end of QT controls for editing text, if you need more functionality, I will continue to follow