QT QHorizontalSpacer spring control

        This article introduces various operations of the QHorizontalSpacer control in detail, such as: new interface, control layout, hidden control , setting width and height , adding layout , other parameters , .h source files, cpp source files , other articles and other operations.

        In actual development, an interface may contain more than a dozen controls, and manually adjusting their positions is time-consuming and laborious. The layout manager can do two things: automatically adjust the position of the controls, including the spacing between controls, alignment, etc.; when the user resizes the window, the controls located in the layout manager will also be resized accordingly, thus maintaining the entire interface beautiful.

        There are currently 27 articles in this series of QT comprehensive and detailed articles. This series of articles describes the basic operation and use of QT controls in more detail. Thank you for your attention, likes, and collections.

 The author of this article is original, please attach the source of the article and the link of this article for reprinting.

1 New interface

2 Control Layout

#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QSpacerItem>

    Title = "QT QHorizontalSpacer  CSDN 双子座断点 ";
    Version = "V 1.0 ";
    BlogText = "https://blog.csdn.net/qq_37529913?type=lately/";
    setWindowTitle(Title + Version + BlogText);

    hBoxLayout_2 = new QHBoxLayout;
    pushButton = new QPushButton("QPushButton");
    pushButton_2 = new QPushButton("QPushButton");

3 Hide controls

ui->verticalSpacer->changeSize(0, 0);
//ui->horizontalLayout->update(); //更新弹簧控件所在的布局器

4 Set width and height

ui->horizontalSpacer->changeSize(60, 20);

5 Add layout

    hBoxLayout_2->addSpacerItem(new QSpacerItem(40, 20));
    hBoxLayout_2->addWidget(pushButton);

    hBoxLayout_2->addWidget(pushButton_2);
    hBoxLayout_2->addSpacerItem(new QSpacerItem(20, 20));
    ui->verticalLayout->addLayout(hBoxLayout_2);

6 Other parameters

  • QSizePolicy::Fixed, hold this Policy, then the window (widget) only accepts the default size as the size selection, so it cannot be enlarged or reduced.
  • QSizePolicy::Minimum, if you hold this Policy, the window will use the minimum size as the default size. On this basis (based on the minimum size), you can zoom in, but the zoom in will not be prioritized by default, only in other layouts When some parts will not be enlarged, they will be enlarged "as a last resort" or "reluctantly".
  • QSizePolicy::Maximum, hold this Policy, then the window (widget) will use the maximum size as the default size, on this basis (based on the maximum size), it can be reduced for other controls that need space in the layout. In short, shrinking can be done without being "inactive".
  • QSizePolicy::Preferred, hold this Policy, then the window will use a "so-called optimal size" as the default size (generally a size between the minimum and maximum), and can be "passive" for the layout Other controls in the control can shrink to make room for controls that need space, and can also be enlarged to take up excess space.
  • QSizePolicy::Expanding, hold this Policy, then the window will use a "so-called optimal size" as the default size in the layout, and then "" "as much as possible" and "actively" occupy the excess Space.
  • QSizePolicy::MinimumExpanding, which holds the Policy, is very similar to the above Expanding. The only difference is that here, the default size is the minimum size. Therefore, based on the default size (that is, the minimum size), it can only be enlarged. And it is "actively", "as much as possible" takes up the extra space.
  • QSizePolicy::Ignored, holds the Policy, ignores the default size ( sizeHint( ) ), "actively" occupies the extra space "as much as possible".

7.h source file

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QSpacerItem>


#pragma execution_character_set("utf-8")

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

    QString Title;
    QString Version;
    QString BlogText;

    QPushButton* pushButton;
    QPushButton* pushButton_2;

    QHBoxLayout* hBoxLayout_2;

};
#endif // MAINWINDOW_H

8 .cpp files

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    Title = "QT QHorizontalSpacer  CSDN 双子座断点 ";
    Version = "V 1.0 ";
    BlogText = "https://blog.csdn.net/qq_37529913?type=lately/";
    setWindowTitle(Title + Version + BlogText);

    hBoxLayout_2 = new QHBoxLayout;
    pushButton = new QPushButton("QPushButton");
    pushButton_2 = new QPushButton("QPushButton");

    //隐藏控件
    //ui->horizontalSpacer->changeSize(0, 0);

    //设置宽高
    ui->horizontalSpacer->changeSize(60, 20);


    hBoxLayout_2->addSpacerItem(new QSpacerItem(40, 20));
    hBoxLayout_2->addWidget(pushButton);

    hBoxLayout_2->addWidget(pushButton_2);
    hBoxLayout_2->addSpacerItem(new QSpacerItem(20, 20));
    ui->verticalLayout->addLayout(hBoxLayout_2);

}

MainWindow::~MainWindow()
{
    delete ui;
}

9 other articles

QT TextEdit Control_Gemini Breakpoint Blog-CSDN Blog_qt textedit

Detailed explanation of the use of QT QComboBox - Gemini Breakpoint Blog - CSDN Blog

Detailed explanation of QT QtableView operation

Qt QStandardItemModel (1. Super detailed usage)_ Gemini Breakpoint Blog-CSDN Blog_qstandardmodel

Qt QStandardItemModel (2. Super detailed function)_Gemini breakpoint blog-CSDN blog_qstandarditemmodel click event

Detailed use of QT QRadioButton - Gemini Breakpoint Blog - CSDN Blog - qt radiobutton

Detailed use of QT QLineEdit_ Gemini Breakpoint Blog-CSDN Blog_qt qlineedit

Detailed explanation of Qt QMessageBox use - Gemini Breakpoint Blog - CSDN Blog - qt message

QChart Line Chart, Pie Chart, Bar Chart, Curve Chart_ Gemini Breakpoint Blog-CSDN Blog_qchart Style

Detailed explanation of QChart properties_ Gemini Breakpoint Blog-CSDN Blog_setanimationoptions

Use of QCharts QValueAxis_Gemini Breakpoint Blog-CSDN Blog_qvalueaxis

Qt 5 wait prompt box (open source dynamic graph)_ Gemini Breakpoint Blog-CSDN Blog_qt wait dialog box

QtDataVisualization Data 3D Visualization_Gemini Breakpoint Blog-CSDN Blog_qtdatavisualizatio

QT QSpinBox Integer Counter Control Use Detailed Explanation_Gemini Breakpoint Blog-CSDN BlogQT
QDoubleSpinBox Floating Point Counter Control (Detailed Use)_Gemini Breakpoint Blog-CSDN Blog_qdoublespinbox Signal Slot
QT QSlider, QHorizontalSlider, QVerticalSlider Control Use Detailed Explanation_Gemini Breakpoint blog-CSDN blog_qslider set step size

Detailed explanation of the use of QT QTabWidget control - Gemini Breakpoint Blog - CSDN Blog

Detailed explanation of the use of QT QCalendarWidget control - Gemini Breakpoint Blog - CSDN Blog

Detailed use of QT QStackedWidget control - Gemini Breakpoint Blog - CSDN Blog

QT QVBoxLayout vertical layout control - Gemini Breakpoint Blog - CSDN Blog

QT QHBoxLayout Horizontal Layout Control_Gemini Breakpoint Blog-CSDN BlogQT
QGridLayout Grid Layout Control_Gemini Breakpoint Blog-CSDN Blog

QT QVerticalSpacer Spring Control - Gemini Breakpoint Blog - CSDN Blog

Guess you like

Origin blog.csdn.net/qq_37529913/article/details/131059168