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()函数可以为一个控件布局.当 ...
随机推荐
- 原来windows里记事本的ansi编码就是GB2312啊,跟utf-8,unicode是不一样的。
原来windows里记事本的ansi编码就是GB2312啊,跟utf-8,unicode是不一样的. 程序里的比如java的,Qt的string都是unicode的字符串,因此如果是你从文件中读取文字 ...
- __clone()方法
<?php class NbaPlayer{ public $name; } $james = new NbaPlayer(); $james->name = 'James'; echo ...
- MyBatis02 MyBatis基础知识之Mapper映射器
1 Mapper映射器是什么 是符合映射文件要求的接口 接口要求 a. 方法名要与sql的id一致. b. 方法的参数类型要与parameterType一致. c. 方法的返回类型要与resultTy ...
- c#与Java事件定义的不同
C#: using System; using System.Collections.Generic; using System.Text; namespace Test1 { class Progr ...
- [转载]/etc/security/limits.conf解释及应用
limits.conf的格式如下: username|@groupname type resource limit username|@groupname:设置需要被限制的用户名,组名前面加@和用户名 ...
- cakephp目录结构
- Socket编程--TCP粘包问题
TCP是个流协议,它存在粘包问题 产生粘包的原因是: TCP所传输的报文段有MSS的限制,如果套接字缓冲区的大小大于MSS,也会导致消息的分割发送. 由于链路层最大发送单元MTU,在IP层会进行数据的 ...
- MySQL数据导入导出方法与工具mysqlimport
MySQL数据导入导出方法与工具mysqlimport<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office ...
- TinkerPop中的遍历:图的遍历步骤(2/3)
24 Group Step 有时,所运行的实际路径或当前运行位置不是计算的最终输出,而是遍历的一些其他表示.group()步骤(map / sideEffect)是根据对象的某些功能组织对象的一个方法 ...
- 《Effective Java》第5章 泛型
第23条:请不要在新代码中使用原生态类型 声明中具有一个或者多个类型参数( type parameter)的类或者接口,就是泛型(generic)类或者接口. 每种泛型定义一组参数化的类型(param ...