1. 布局管理器提供相关的类对界面组件进行布局管理,能够自动排列窗口中的界面组件,窗口变化后能自动更新界面组件的大小。

2. QLayout是Qt布局管理器的抽象基类,通过继承QLayout实现了功能各异且互补的布局管理器。

①QBoxLayout: QVBoxLayout, QHBoxLayout

②QGridLayout:

③QFormLayout:

④QStackedLayout:

3. 综合实例(一个简单的安装向导模型)

Widget.h

#ifndef WIDGET_H
#define WIDGET_H #include <QtGui/QWidget>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit> class Widget : public QWidget
{
Q_OBJECT
private:
QPushButton PreBtn;
QPushButton NextBtn;
QLabel Lab0;
QLabel Lab1;
QLabel Lab2;
QLabel Lab3; QPushButton PushButton1;
QPushButton PushButton2; QLineEdit LineEdit;
void InitControl(); QWidget* GetFirstPage();
QWidget* GetSecondPage();
QWidget* GetThirdPage(); private slots:
void PreBtnClicked();
void NextBtnClicked(); public:
Widget(QWidget *parent = );
~Widget();
}; #endif // WIDGET_H

Widget.cpp

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QFormLayout>
#include <QStackedLayout>
#include <QDebug>
#include "widget.h" Widget::Widget(QWidget *parent): QWidget(parent), PreBtn(this), NextBtn(this)
{
InitControl();
} void Widget::InitControl()
{
QVBoxLayout* vLayout = new QVBoxLayout();
QHBoxLayout* hLayout = new QHBoxLayout();
QStackedLayout* sLayout = new QStackedLayout(); PreBtn.setText("Pre Button");
PreBtn.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
PreBtn.setMinimumSize(, ); NextBtn.setText("Next Button");
NextBtn.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
NextBtn.setMinimumSize(, ); sLayout->addWidget(GetFirstPage());
sLayout->addWidget(GetSecondPage());
sLayout->addWidget(GetThirdPage()); hLayout->setSpacing();
hLayout->addWidget(&PreBtn);
hLayout->addWidget(&NextBtn); vLayout->addLayout(sLayout);
vLayout->addLayout(hLayout); setLayout(vLayout); connect(&PreBtn, SIGNAL(clicked()), this, SLOT(PreBtnClicked()));
connect(&NextBtn, SIGNAL(clicked()), this, SLOT(NextBtnClicked())); } QWidget* Widget::GetFirstPage()
{
QWidget* widget = new QWidget();
QGridLayout* gLayout = new QGridLayout(); Lab0.setText("This");
Lab1.setText("is");
Lab2.setText("First");
Lab3.setText("Page"); gLayout->addWidget(&Lab0, , );
gLayout->addWidget(&Lab1, , );
gLayout->addWidget(&Lab2, , );
gLayout->addWidget(&Lab3, , ); widget->setLayout(gLayout); return widget;
} QWidget* Widget::GetSecondPage()
{
QWidget* widget = new QWidget(); QFormLayout* fLayout = new QFormLayout(); LineEdit.setText("This is Second Page"); fLayout->addRow("Name", &LineEdit); widget->setLayout(fLayout); return widget;
} QWidget* Widget::GetThirdPage()
{
QWidget* widget = new QWidget();
QVBoxLayout* vLayout = new QVBoxLayout(); PushButton1.setText("Third"); PushButton2.setText("Page"); vLayout->addWidget(&PushButton1);
vLayout->addWidget(&PushButton2); widget->setLayout(vLayout); return widget;
} void Widget::PreBtnClicked()
{ QStackedLayout* sLayout = dynamic_cast<QStackedLayout*>(((dynamic_cast<QVBoxLayout*>(layout()))->children())[]); if(sLayout != NULL)
{
int currentIndex = sLayout->currentIndex(); currentIndex = (currentIndex > ) ? (currentIndex - ) : currentIndex; sLayout->setCurrentIndex(currentIndex);
} qDebug() << "PreBtnClicked()";
} void Widget::NextBtnClicked()
{
QStackedLayout* sLayout = dynamic_cast<QStackedLayout*>(((dynamic_cast<QVBoxLayout*>(layout()))->children())[]); if(sLayout != NULL)
{
int currentIndex = sLayout->currentIndex(); currentIndex = (currentIndex < ) ? (currentIndex + ) : currentIndex; sLayout->setCurrentIndex(currentIndex);
} qDebug() << "NextBtnClicked()";
} Widget::~Widget()
{ }

Qt中的布局管理器的更多相关文章

  1. JAVA中GridBagLayout布局管理器应用详解

    很多情况下,我们已经不需要通过编写代码来实现一个应用程序的图形界面,而是通过强大的IDE工具通过拖拽辅以简单的事件处理代码即可很轻松的完成.但是我们不得不面对这样操作存在的一些问题,有时候我们希望能够 ...

  2. 【java】浅析java组件中的布局管理器

    这篇博文笔者介绍一下java组件中,常用的布局管理器.java组件中的布局方式有好几十种,所有的这些布局管理器都实现了java.awt.LayoutManager接口.接下来笔者介绍一下常用的5种布局 ...

  3. Qt之自定义布局管理器(QCardLayout)

    简述 手动布局另一种方法是通过继承QLayout类编写自己的布局管理器. 下面我们详细来举一个例子-QCardLayout.它由同名的Java布局管理器启发而来.也被称之为卡片布局,每个项目偏移QLa ...

  4. Draw2d中的布局管理器Layout比较

    最近在研究Eclipse中的GEF开发,在跟着GEF-whole-upload教程做一个GEF应用程序的例子时,发现Figure上的控件无法显示,谷歌了很久也没找到解决方案,最后终于发现是Layout ...

  5. Qt之自定义布局管理器(QBorderLayout)

    简述 QBorderLayout,顾名思义-边框布局,实现了排列子控件包围中央区域的布局. 具体实现要求不再赘述,请参考前几节内容. 简述 实现 效果 源码 使用 实现 QBorderLayout主要 ...

  6. Qt之自定义布局管理器(QFlowLayout)

    简述 QFlowLayout,顾名思义-流布局,实现了处理不同窗口大小的布局.根据应用窗口的宽度来进行控件放置的变化. 具体实现要求不再赘述,请参考前两节内容. 简述 实现 效果 源码 实现 QFlo ...

  7. QT5每日一学(五)QT布局管理器

    Qt中的布局管理器主要包括 QBoxLayout基本布局管理器 QGridLayout栅格布局管理器 QFormLayout窗体布局管理器 而基本布局管理器又分为QHBoxLayout水平布局管理器和 ...

  8. Qt 布局管理器

    在一个颜值当道的今天,无论买衣服,买车还是追星,颜值的高低已经变成了大家最看重的(不管男性女性都一样,千万别和我说你不是):而对于程序猿来说,开发一款软件,不再只注重逻辑和稳定性,美观和用户友好性也是 ...

  9. Qt之布局管理器

    简述 Qt的布局系统提供了一个简单的和强有力的方式,来自动排列窗口子控件布局. 所有QWidget子类可以使用布局来管理他们的子控件.QWidget::setLayout()函数可以为一个控件布局.当 ...

随机推荐

  1. MSSQL 当前数据库中已存在用户或角色,SQLServer2008,错误15023,

    原因: sql server中“登录”与“用户”的区别,“登录”用于用户身份验证,而数据库“用户”帐户用于数据库访问和权限验证.登录通过安全识别符 (SID) 与用户关联.将数据库恢复到其他服务器时, ...

  2. TextView中ellipsize属性

    TextView中可以设置一个ellipsize属性,作用是当文字长度超过textview宽度时的显示方式: 例如,"encyclopedia"显示, 只是举例,以实际显示为准:) ...

  3. floyd路径记录

    #include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #in ...

  4. ARC059F

    传送门 分析 见ptx大爷博客 代码 #include<iostream> #include<cstdio> #include<cstring> #include& ...

  5. ZROI2018普转提day7t2

    传送门 分析 首先我们不难想到我们一定可以将每一个点分开算,然后看这个点被几个矩形包含 于是对于位置为$(i,j)$的点它被包含的次数为$i * (n-i+1) * j * (m-j+1)$ 这个式子 ...

  6. HTML5之FileReader的使用.RP

    HTML5定义了FileReader作为文件API的重要成员用于读取文件,根据W3C的定义,FileReader接口提供了读取文件的方法和包含读取结果的事件模型. FileReader的使用方式非常简 ...

  7. Spring 特点

    IoC:豆浆和打针的例子.一个是主动的,一个是被动的.比如在spring里面我们需要对象了,提出需求,spring容器会把对象给你.(这就是IoC) AOP:

  8. Java50道经典习题-程序47 输入数字打印星号

    题目:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*. import java.util.Scanner; public class Prog47{ public static ...

  9. 基于Ace Admin 的菜单栏实现

    1.首先是数据库表必然包含以下几个字段Id ,ParnetId,Url,Name等 create table dbo.Module ( Id uniqueidentifier not null con ...

  10. 从零开始搭建.NET Core 2.0 API(学习笔记一)

    从零开始搭建.NET Core 2.0 API(学习笔记一) 一. VS 2017 新建一个项目 选择ASP.NET Core Web应用程序,再选择Web API,选择ASP.NET Core 2. ...