布局管理器
布局管理器
布局管理器,有方向,间距,addwidget
类 | 描述 |
---|---|
QBoxLayout | 水平或垂直排列控件的基类 |
QButtonGroup | 组织按钮的容器 |
QFormLayout | 管理输入控件和其相关的标签 |
QGraphicsAnchor | 表示在QGraphicsAnchorLayout中两个项目之间的锚 |
QGraphicsAnchorLayout | 在图形视图中可以将锚连接到一起 |
QGridLayout | 网格布局(多行多列) |
QGroupBox | 带标题的分组框 |
QHBoxLayout | 水平排列控件 |
QLayout | 几何管理器的基类 |
QLayoutItem | 抽象的操作布局Item |
QSizePolicy | 描述水平和垂直大小调整的策略 |
QSpacerItem | 布局中的空间隔 |
QStackedLayout | 切换控件,同一时间只有一个控件可见 |
QStackedWidget | 切换控件,同一时间只有一个控件可见 |
QVBoxLayout | 垂直排列控件 |
QWidgetItem | 表示一个控件的布局项 |
布局例子
QBoxLayout/QVBoxLayout/QHBoxlayout
拥有QHBoxLayout和QVBoxLayout
- set
函数 | 描述 |
---|---|
QBoxLayout(QBoxLayout::Direction dir, QWidget *parent = nullptr) | 构造函数 |
virtual ~QBoxLayout() | 析构函数 |
void addLayout(QLayout *layout, int stretch = 0) | 添加 |
void addSpacerItem(QSpacerItem *spacerItem) | 添加spaceitem |
void addSpacing(int size) | 间距 |
void addStretch(int stretch = 0) | 可以让其居左或者居右或者居中,看例子 |
void addStrut(int size) | |
void addWidget(QWidget *widget, int stretch = 0, Qt::Alignment alignment = …) | 对齐方式 |
QBoxLayout::Direction direction() const | 方向 |
void insertItem(int index, QLayoutItem *item) | 插入 |
void insertLayout(int index, QLayout *layout, int stretch = 0) | 插入 |
void insertSpacerItem(int index, QSpacerItem *spacerItem) | |
void insertSpacing(int index, int size) | |
void insertStretch(int index, int stretch = 0) | |
void insertWidget(int index, QWidget *widget, int stretch = 0, Qt::Alignment alignment = …) | |
void setDirection(QBoxLayout::Direction direction) | 设置方向 |
void setSpacing(int spacing) | 设置空格 |
void setStretch(int index, int stretch) | 设置长度 |
bool setStretchFactor(QWidget *widget, int stretch) | 可以进行伸缩 |
bool setStretchFactor(QLayout *layout, int stretch) | |
int spacing() const | 返回间距 |
int stretch(int index) const | 返回长度 |
/*
* hbox and vbox 是box确定方向之后的产物,没啥特殊
* box 属性有间距addspace,空白补齐addstrlen,以及方向direction
* 他的函数也全部用来操作上面的属性
*/
mywidget::mywidget()
{
setWindowTitle("my widget test");
QHBoxLayout *layout=new QHBoxLayout(this);
//垂直的布局
QWidget *pvwidget=new QWidget(this);
QBoxLayout *pvlayout=new QBoxLayout(QBoxLayout::BottomToTop,pvwidget); //布局管理器
for(int i=0;i<5;i++)
{
QPushButton *ppushbutton=new QPushButton("vertical"+QString::number(i+1,10));
pvlayout->addWidget(ppushbutton);
}
//水平的布局
QWidget *phwidget=new QWidget(this);
QBoxLayout *phlayout=new QBoxLayout(QBoxLayout::RightToLeft,phwidget); //布局管理器
for(int i=0;i<5;i++)
{
phlayout->addStretch();//添加伸缩,此时会均分长度,但是在第一个控件前添加,会居右,最后一个控件后添加会居左
QPushButton *ppushbutton=new QPushButton("horizontal"+QString::number(i+1,10));
phlayout->addWidget(ppushbutton);
if(i==2)phlayout->setStretchFactor(ppushbutton,2); //第三个可以进行拉伸
ppushbutton->setFixedHeight(70);
ppushbutton->setStyleSheet("font-size:30px;background:red;");
}
//间距space的设置
pvlayout->setSpacing(0);
phlayout->setSpacing(50);
//其他
pvlayout->setContentsMargins(50,50,50,50); //边距
layout->addWidget(pvwidget);
layout->addWidget(phwidget);
this->show();
}
QGridLayout
函数 | 描述 |
---|---|
void setColumnMinimumWidth(int column, int minSize) | 列最小的宽度 |
void setColumnStretch(int column, int stretch) | 可拉伸 |
void setHorizontalSpacing(int spacing) | 水平间距 |
void setRowMinimumHeight(int row, int minSize) | 行最小高度 |
void setRowStretch(int row, int stretch) | 设置靠左靠右,或者平分,看qboxlayout的addstretch |
void setSpacing(int spacing) | 间距 |
void setVerticalSpacing(int spacing) | 垂直间距 |
columnCount() | 获取列数 |
rowCount() | 获取行数 |
/*
* 头像 第0行,第0列开始,占3行1列
* pLayout->addWidget(pImageLabel, 0, 0, 3, 1);
* pLayout->setColumnMinimumWidth(1,100); //特殊的列的宽度为100
* pLayout->setRowMinimumHeight(3,10); //特殊行的行高为10,
* pLayout->setHorizontalSpacing(10); //设置水平间距
* pLayout->setVerticalSpacing(10); //设置垂直间距
*/
mywidget::mywidget()
{
setWindowTitle("my widget test");
// 构建控件 头像、用户名、密码输入框等
QLabel *pImageLabel = new QLabel(this);
QLineEdit *pUserLineEdit = new QLineEdit(this);
QLineEdit *pPasswordLineEdit = new QLineEdit(this);
QCheckBox *pRememberCheckBox = new QCheckBox(this);
QCheckBox *pAutoLoginCheckBox = new QCheckBox(this);
QPushButton *pLoginButton = new QPushButton(this);
QPushButton *pRegisterButton = new QPushButton(this);
QPushButton *pForgotButton = new QPushButton(this);
pLoginButton->setFixedHeight(30);
pUserLineEdit->setFixedWidth(200);
// 设置头像
QPixmap pixmap(":/image/hz1.jpg");
pImageLabel->setFixedSize(90, 90);
pImageLabel->setPixmap(pixmap);
pImageLabel->setScaledContents(true);
// 设置文本
pUserLineEdit->setPlaceholderText(QStringLiteral("QQ号码/手机/邮箱"));
pPasswordLineEdit->setPlaceholderText(QStringLiteral("密码"));
pPasswordLineEdit->setEchoMode(QLineEdit::Password);
pRememberCheckBox->setText(QStringLiteral("记住密码"));
pAutoLoginCheckBox->setText(QStringLiteral("自动登录"));
pLoginButton->setText(QStringLiteral("登录"));
pRegisterButton->setText(QStringLiteral("注册账号"));
pForgotButton->setText(QStringLiteral("找回密码"));
QGridLayout *pLayout = new QGridLayout(this);
pLayout->setColumnMinimumWidth(1,100); //特殊的列的宽度为100
pLayout->setRowMinimumHeight(3,10); //特殊行的行高为10,
pLayout->setHorizontalSpacing(10); //设置水平间距
pLayout->setVerticalSpacing(10); //设置垂直间距
pLayout->addWidget(pImageLabel, 0, 0, 3, 1); // 头像 第0行,第0列开始,占3行1列
pLayout->addWidget(pUserLineEdit, 0, 1,1,2);
pLayout->addWidget(pPasswordLineEdit, 1, 1,1,2);
pLayout->addWidget(pRegisterButton, 0, 3);
pLayout->addWidget(pForgotButton, 1, 3);
pLayout->addWidget(pRememberCheckBox, 2, 1, 1, 1, Qt::AlignLeft | Qt::AlignVCenter);
pLayout->addWidget(pAutoLoginCheckBox, 2, 2, 1, 1, Qt::AlignRight | Qt::AlignVCenter);
pLayout->addWidget(pLoginButton, 3, 1,1,2);
pLayout->setContentsMargins(10, 10, 10, 10);
setLayout(pLayout);
this->show();
}
表单布局QFormLayout
一般不常用,需要是看文档
mywidget::mywidget()
{
QLineEdit *nameLineEdit=new QLineEdit(this);
QLineEdit *emailLineEdit=new QLineEdit(this);
QSpinBox *ageSpinBox=new QSpinBox(this);
QFormLayout *formLayout = new QFormLayout;
formLayout->addRow(tr("&Name:"), nameLineEdit);
formLayout->addRow(tr("&Email:"), emailLineEdit);
formLayout->addRow(tr("&Age:"), ageSpinBox);
setLayout(formLayout);
this->show();
}
可以进行页面切换的布局QStackedLayout
找到现在的位置,可以到达想去的位置
QStackedWidget类似
函数 | 描述 |
---|---|
int addWidget(QWidget *widget) | 添加widget |
int currentIndex() const | 返回位置 |
QWidget * currentWidget() const | 返回位置 |
int insertWidget(int index, QWidget *widget) | 插入wedget |
void setStackingMode(QStackedLayout::StackingMode stackingMode) | |
void setCurrentIndex(int index) | 到达想去的wibget |
void setCurrentWidget(QWidget *widget) | 到达想去的位置 |
signal
void currentChanged(int index)
void widgetRemoved(int index)
/*
* QStackedLayout
* addwidget添加widget
* currentIndex()返回现在的位置
* sercurrentindex()设置现在的位置
*/
mywidget::mywidget()
{
//主要的布局
setWindowTitle("widget test");
QVBoxLayout *mainLayout = new QVBoxLayout(this);
QLabel *now_index=new QLabel(this);
QPushButton *next_widget=new QPushButton("下一页");
QPushButton *prior_widget=new QPushButton("上一页");
QLineEdit *to_widget=new QLineEdit;
to_widget->setPlaceholderText("跳转到页数");
QPushButton *summit=new QPushButton("跳转");
QHBoxLayout *menu_widget_layout=new QHBoxLayout;
menu_widget_layout->addWidget(prior_widget);
menu_widget_layout->addWidget(next_widget);
menu_widget_layout->addWidget(to_widget);
menu_widget_layout->addWidget(summit);
menu_widget_layout->addWidget(now_index);
//可以跳转页面的layout
QStackedLayout *stack_layout = new QStackedLayout;
QWidget *widget_1 = new QWidget;
QWidget *widget_2 = new QWidget;
QWidget *widget_3 = new QWidget;
stack_layout->addWidget(widget_1);
stack_layout->addWidget(widget_2);
stack_layout->addWidget(widget_3);
mainLayout->addLayout(menu_widget_layout);
mainLayout->addLayout(stack_layout);
now_index->setText("第"+QString::number(stack_layout->currentIndex()+1,10)
+"页,共"+QString::number(stack_layout->count(),10) +"页");
//信号和槽,实现上一页,下一页,跳转功能
connect(summit,QOverload<bool>::of(&QPushButton::clicked),[stack_layout,to_widget]{
if(stack_layout->currentIndex() >=0 && stack_layout->currentIndex()<=stack_layout->count()-1)
stack_layout->setCurrentIndex(to_widget->text().toInt()-1);
});
connect(prior_widget,QOverload<bool>::of(&QPushButton::clicked),[stack_layout]{
if(stack_layout->currentIndex() >=0 && stack_layout->currentIndex()<=stack_layout->count()-1)
stack_layout->setCurrentIndex(stack_layout->currentIndex()-1);
});
connect(next_widget,QOverload<bool>::of(&QPushButton::clicked),[stack_layout]{
if(stack_layout->currentIndex() >=0 && stack_layout->currentIndex()<=stack_layout->count()-1)
stack_layout->setCurrentIndex(stack_layout->currentIndex()+1);
});
connect(stack_layout,QOverload<int>::of(&QStackedLayout::currentChanged),[now_index,stack_layout]{
now_index->setText("第"+QString::number(stack_layout->currentIndex()+1,10)
+"页,共"+QString::number(stack_layout->count(),10) +"页");
});
setLayout(mainLayout);
this->show();
}
QSpacerItem提供一个空白区域
pHLayout->addSpacerItem(new QSpacerItem(20, 20));