Qt中的布局管理器
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中的布局管理器的更多相关文章
- JAVA中GridBagLayout布局管理器应用详解
很多情况下,我们已经不需要通过编写代码来实现一个应用程序的图形界面,而是通过强大的IDE工具通过拖拽辅以简单的事件处理代码即可很轻松的完成.但是我们不得不面对这样操作存在的一些问题,有时候我们希望能够 ...
- 【java】浅析java组件中的布局管理器
这篇博文笔者介绍一下java组件中,常用的布局管理器.java组件中的布局方式有好几十种,所有的这些布局管理器都实现了java.awt.LayoutManager接口.接下来笔者介绍一下常用的5种布局 ...
- Qt之自定义布局管理器(QCardLayout)
简述 手动布局另一种方法是通过继承QLayout类编写自己的布局管理器. 下面我们详细来举一个例子-QCardLayout.它由同名的Java布局管理器启发而来.也被称之为卡片布局,每个项目偏移QLa ...
- Draw2d中的布局管理器Layout比较
最近在研究Eclipse中的GEF开发,在跟着GEF-whole-upload教程做一个GEF应用程序的例子时,发现Figure上的控件无法显示,谷歌了很久也没找到解决方案,最后终于发现是Layout ...
- Qt之自定义布局管理器(QBorderLayout)
简述 QBorderLayout,顾名思义-边框布局,实现了排列子控件包围中央区域的布局. 具体实现要求不再赘述,请参考前几节内容. 简述 实现 效果 源码 使用 实现 QBorderLayout主要 ...
- Qt之自定义布局管理器(QFlowLayout)
简述 QFlowLayout,顾名思义-流布局,实现了处理不同窗口大小的布局.根据应用窗口的宽度来进行控件放置的变化. 具体实现要求不再赘述,请参考前两节内容. 简述 实现 效果 源码 实现 QFlo ...
- QT5每日一学(五)QT布局管理器
Qt中的布局管理器主要包括 QBoxLayout基本布局管理器 QGridLayout栅格布局管理器 QFormLayout窗体布局管理器 而基本布局管理器又分为QHBoxLayout水平布局管理器和 ...
- Qt 布局管理器
在一个颜值当道的今天,无论买衣服,买车还是追星,颜值的高低已经变成了大家最看重的(不管男性女性都一样,千万别和我说你不是):而对于程序猿来说,开发一款软件,不再只注重逻辑和稳定性,美观和用户友好性也是 ...
- Qt之布局管理器
简述 Qt的布局系统提供了一个简单的和强有力的方式,来自动排列窗口子控件布局. 所有QWidget子类可以使用布局来管理他们的子控件.QWidget::setLayout()函数可以为一个控件布局.当 ...
随机推荐
- Flow Layout
--------------siwuxie095 将根面板 contentPane 的布局切换为 Flow Layout Flow La ...
- 列表控件JList的使用
--------------siwuxie095 工程名:TestUI 包名:com.siwuxie095.ui 类名:TestList.jav ...
- nginx负载均衡, 配置地址带端口
nginx.conf 配置如下: upstream wlcf.dev.api { server 127.0.0.1:8833; server 127.0.0.2:8833; } server { l ...
- 算法Sedgewick第四版-第1章基础-007一用两个栈实现简单的编译器
1. package algorithms.util; import algorithms.ADT.Stack; /****************************************** ...
- 715B Complete The Graph
传送门 题目大意 给出一个图,一些边带权,另一些边等待你赋权(最小赋为1).请你找到一种赋权方式,使得 s 到 t 的最短路为 L n ≤ 1e3 ,m ≤ 1e4 ,L ≤ 1e9 分析 二分所有边 ...
- spoj1716 Can you answer these queries III
传送门 (分析见正睿2018.10.1笔记) 代码 #include<iostream> #include<cstdio> #include<cstring> #i ...
- CF 464E The Classic Problem
补一补之前听课时候的题. 考虑使用dij算法求最短路,因为边权存不下,所以考虑用主席树维护二进制位,因为每一次都只会在一个位置进行修改,所以可以暴力进位,这样均摊复杂度是对的. <算法导论> ...
- c# 的默认访问修饰符小结(转)
c# 的访问修饰符是private 还是 internal? 准确的说,不能一概而论. [MSDN] Classes and structs that are not nested within ot ...
- 不设置JAVA_HOME运行eclipse
编辑eclipse目录下的eclipse.ini 在第一行加入下面那句话,实际路径按照系统中的jdk目录设置.这样设置后可以省了环境中的JAVA_HOME像myeclipse一样. -vm C:\ ...
- Data Base oracle简单使用及管理工具使用
oracle简单使用及管理工具使用 一.常用工具: 1.sqldeveloper 2.navicat for oracle 3.PLSQL Developer 4.toad