QT 基本布局QLayout

Qt基本布局管理类:QHBoxLayout、QVBoxLayout、QGridLayout等类型,继承关系:
在这里插入图片描述
布局中常用的方法有addWidget()和addLayout();

// addWidget()函数原型:
void addWidget
(
	QWidget *widget, 	//要插入的控件对象
	int fromRow,		//插入的行
	int fromColumn,		//插入的列
	int rowSpan,		//表示占用的行数
	int columnSpan,		//表示占用的列数
	Qt::Alignment alignment=0 //描述各个控件的对齐方式
)

//addLayout()函数原型
void addLayout
(
	QLayout *layout,		//表示需要插入的子布局对象
	int row,				//插入的起始行
	int column,				//插入的起始列
	int rowSpan,			//表示占用的行数
	int columnSpan,			//表示占用的列数
	Qt::Alignment alignment=0 //指定对齐方式
)

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QComboBox>
#include <QTextEdit>
#include <QGridLayout>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>

class Dialog : public QDialog
{
    
    
    Q_OBJECT

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

private:
    //左侧
    QLabel *UserNameLabel;
    QLabel *NameLabel;
    QLabel *SexLabel;
    QLabel *DepartmentLabel;
    QLabel *AgeLabel;
    QLabel *OtherLabel;
    QLineEdit *UserNameLineEdit;
    QLineEdit *NameLineEdit;
    QComboBox *SexComboBox;
    QTextEdit *DepartmentTextEdit;
    QLineEdit *AgeLineEdit;
    QGridLayout *LeftLayout;

    //右侧
    QLabel *HeadLabel;
    QLabel *HeadIconLabel;
    QPushButton *UpdateHeadBtn;
    QHBoxLayout *TopRightLayout;

    QLabel *IntroductionLabel;
    QTextEdit *IntroductionTextEdit;
    QVBoxLayout *RightLayout;

    //底部
    QPushButton *OkBtn;
    QPushButton *CancelBtn;
    QHBoxLayout *ButtomLayout;

};
#endif // DIALOG_H

dialog.cpp

#include "dialog.h"


Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    
    
    setWindowTitle("UserInfo");
    /*************** 左侧 **************/
    UserNameLabel = new QLabel("用户名: ");
    UserNameLineEdit = new QLineEdit();
    NameLabel = new QLabel("姓名: ");
    NameLineEdit = new QLineEdit();
    SexLabel = new QLabel("性别: ");
    SexComboBox = new QComboBox();
    SexComboBox->addItem("女");
    SexComboBox->addItem("男");
    DepartmentLabel = new QLabel("部门: ");
    DepartmentTextEdit = new QTextEdit();
    AgeLabel = new QLabel("年龄: ");
    AgeLineEdit = new QLineEdit();
    OtherLabel = new QLabel("备注: ");
    OtherLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);

    LeftLayout = new QGridLayout();
    LeftLayout->addWidget(UserNameLabel, 0, 0);     //用户名
    LeftLayout->addWidget(UserNameLineEdit, 0, 1);

    LeftLayout->addWidget(NameLabel, 1, 0);         //姓名
    LeftLayout->addWidget(NameLineEdit, 1, 1);

    LeftLayout->addWidget(SexLabel, 2, 0);          //性别
    LeftLayout->addWidget(SexComboBox, 2, 1);

    LeftLayout->addWidget(DepartmentLabel, 3, 0);   //部门
    LeftLayout->addWidget(DepartmentTextEdit, 3, 1);

    LeftLayout->addWidget(AgeLabel, 4, 0);          //年龄
    LeftLayout->addWidget(AgeLineEdit, 4, 1);

    LeftLayout->addWidget(OtherLabel, 5, 0, 1, 2);  //其他

    LeftLayout->setColumnStretch(0, 1);
    LeftLayout->setColumnStretch(1, 3);


    /*****************右侧*****************/
    HeadLabel = new QLabel("头像: ");         //右上角部分
    HeadIconLabel = new QLabel;

    QPixmap icon("123.png");
    HeadIconLabel->setPixmap(icon);
    HeadIconLabel->resize(icon.width(), icon.height());
    UpdateHeadBtn = new QPushButton("更新");

    TopRightLayout = new QHBoxLayout();
    TopRightLayout->setSpacing(20);
    TopRightLayout->addWidget(HeadLabel);
    TopRightLayout->addWidget(HeadIconLabel);
    TopRightLayout->addWidget(UpdateHeadBtn);

    IntroductionLabel = new QLabel("个人说明: "); //右下角部分
    IntroductionTextEdit = new QTextEdit();

    RightLayout = new QVBoxLayout();
    RightLayout->setMargin(10);
    RightLayout->addLayout(TopRightLayout);
    RightLayout->addWidget(IntroductionLabel);
    RightLayout->addWidget(IntroductionTextEdit);

    /*****************底部******************/
    OkBtn = new QPushButton("确定");
    CancelBtn = new QPushButton("取消");

    ButtomLayout = new QHBoxLayout();
//    ButtomLayout->setDirection(QHBoxLayout::LeftToRight); //设定QHBoxLayout排列方向
    ButtomLayout->addStretch();    //在按钮前插入占位符,使两个按钮能够靠右对齐,并且在整个对话框的大小发生变化是,保证按钮大小不发生变化
    ButtomLayout->addWidget(OkBtn);
    ButtomLayout->addWidget(CancelBtn);

    /******************主窗口****************/
    QGridLayout *mainLayout = new QGridLayout(this);
    mainLayout->setMargin(15);
    mainLayout->setSpacing(10);
    mainLayout->addLayout(LeftLayout, 0, 0);
    mainLayout->addLayout(RightLayout, 0, 1);
    mainLayout->addLayout(ButtomLayout, 1, 0, 1, 2);
    mainLayout->setSizeConstraint(QLayout::SetFixedSize);

}

Dialog::~Dialog()
{
    
    
}


main.cpp

#include "dialog.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    
    
    QApplication a(argc, argv);
    Dialog w;
    w.show();
    return a.exec();
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/u013420428/article/details/109674254