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. js闭包(二)

    一.何谓“闭包”? 所谓“闭包(Closure)”,指的是一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分. 描述的如此学术的官方解释,相信很少人能够 ...

  2. webfrom 母版页

    ASP.NET中母版页作用 一是提高代码的复用(把相同的代码抽出来) 二是使整个网站保持一致的风格和样式. 母版页存在就一定要有内容页的存在,否则母版页的存在就没有了意义. .master 一.添加母 ...

  3. g2o20160430下的csparse文件夹内的CMakeLists.txt

    1. g2o20160430下的csparse文件夹内的CMakeLists.txt cmake_minimum_required(VERSION 2.6) PROJECT(csparse) SET( ...

  4. php学习笔记-多维数组

    多维数组就是有一个数组,它里面的每个元素又是一个数组. <?php $stuff =array('food'=>array('apple','orange'),'book'=>arr ...

  5. zedboard:使用ISE和modelsim搭建仿真环境 标签: zedboardfpgamodelsimise 2017-03-03 14:00 528人阅读

    详细步骤: 产生ISE仿真库文件 开始->所有程序->xilinx design tools->simulation library compilation wizard.路径可能不 ...

  6. GCD 学习(一)简介

    文章摘抄至:http://zhuyanfeng.com/archives/3015 并有一些改动 GCD(Grand Central Dispatch)是从OS X Snow Leopard和iOS ...

  7. Person.delete请求----强大的bug---下班之前总结整个过程

    默认访问的是: findAll(query) 还有个findAll(ids,query) 只有findAll才调用了findEntity->findById: 那么我重写了findById,查询 ...

  8. Bulma 源码解析之 .container 类

    Bulma 的 .container 类是这样实现的. .container position: relative // 不设置桌面以下设备的 container +desktop margin: 0 ...

  9. [学习笔记]_exit和exit深入理解

    #include<stdio.h> #include<stdlib.h> #include<string.h> #include <unistd.h> ...

  10. [译]Javascript中的递归函数

    本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...