“This is the 12th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

Near the New Year, recently in learning some new knowledge about Qt, today to tell about the window segmentation I learned, if there is incorrect, we can correct oh ~

First, let’s take a look at the simple implementation. The province said the hype, but we do not know which part is said.

Function implementation

The core of the whole demo is the split window class: QSplitter.

In Qt, QSplitter can split the layout of Windows flexibly, and is often used in window design similar to file Explorer.

Horizontal partitioning

Create a QSplitter class object and use it as the main split window. Set the split mode of this window to horizontal

QSplitter *splitterMain = new QSplitter(Qt::Horizontal, 0);
Copy the code

Code parsing: the horizontal and vertical Settings here refer to the horizontal or vertical display of the two separated parts.

Such as:

Qt::Horizontal: divided by vertical axis, the two parts of content are separated left and right.

Qt::Vertical: the horizontal axis is used as the boundary, and the two parts are separated up and down.

Once you’ve split the two parts, you can fill them separately, assuming that, in this case, the left split window fills a QTextEdit edit box

QTextEdit *textLeft = new QTextEdit(QStringLiteral(" left window "), splitterMain);Copy the code

Once you create a new edit box control object, all you need to do is pass the parent pointer to the split window into the QTextEdit object.

In QSplitter, after the split display property is set, the inserted objects default from left to right.

Suppose a QTextEdit object is displayed on the left and a QTextEdit object needs to be displayed on the right.

QTextEdit *textLeft = new QTextEdit(QStringLiteral(" left window "), splitterMain); QTextEdit *textRight = new QTextEdit(QStringLiteral(" right window "), splitterMain);Copy the code

Show the effect as follows:

Multiple segmentation

The previous function introduced how to segment the whole window, so now there is a new requirement, in the right side of the window has been divided, divided again, according to the vertical way.

So, the right split window implementation:

QSplitter *splitterRight = new QSplitter(Qt::Vertical, splitterMain);
Copy the code

Code analysis: Create a QSplitter object as the right split window, and set the split window to vertical split mode, the current parent window is the main split window.

QTextEdit: QTextEdit: QTextEdit: QTextEdit

QTextEdit *textUp = new QTextEdit(QStringLiteral(" right window "), splitterRight); QTextEdit *textBottom = new QTextEdit(QStringLiteral(" lower right window "), splitterRight);Copy the code

At this point, the style is consistent with that shown in the GIF rendering.

Now, how do I drag?

In QSplitter there is a setStretchFactor function that sets scalable controls with two parameters.

Parameter 1: Specifies the control sequence number to set. The current control sequence number increases from 0 in order of insertion.

Parameter 2: If the current value is greater than 0, the control is a scalable control.

splitterMain->setStretchFactor(1, 1);
Copy the code

Optimized Settings: How about letting Windows drag while updating the display in real time?

QSplitter: : setOpaqueResize function.

Parameter true: the display can be updated in real time.

Parameter false: indicates that only a thick gray line will be displayed when dragged. The latest segmentation will be displayed only after dragged to the specified position. The display effect is as follows:

Note that if the QTextEdit object is set to a fixed size, it does not take effect, regardless of whether the drag property is set.

This function to achieve super simple answer, here on the end of the explanation.

I am a Chinese good citizen st, a C++ program ape ~