使用过mfc编程,就知道控件需要自己拖放。当一个界面有很多小控件时,摆放这些控件特别麻烦。

  但是qt编程中有布局(Layout),让系统来摆放控件。使整个控件有一致的外观和感觉,特别方便。

1.水平方向的布局 QHBoxLayout

      

思考:

简单的把控件添加到布局里面就ok了。

    horizontalGroupbox = new QGroupBox("Horizontal Layout");
hlayout = new QHBoxLayout; QPushButton *button1 = new QPushButton("Button1");
QPushButton *button2 = new QPushButton("Button2");
QPushButton *button3 = new QPushButton("Button3");
QPushButton *button4 = new QPushButton("Button4"); hlayout->addWidget(button1);
hlayout->addWidget(button2);
hlayout->addWidget(button3);
hlayout->addWidget(button4); horizontalGroupbox->setLayout(hlayout);

2.栅格布局。QGridLayout

      

思考:

1.grid layout是按行列来进行布局的。

2.这个布局有4行,3列。

3.第一行只有一个QTextEdit,但是它横跨4行。

4.第二列比第一列长,第三列比第二列长。原因是调用了 setRowStretch()函数。

    gridGroupbox = new QGroupBox("Grid Layout");
gridLayout = new QGridLayout; label1 = new QLabel("Line1");
label2 = new QLabel("Line2");
label3 = new QLabel("Line3"); lineEdit1 = new QLineEdit;
lineEdit2 = new QLineEdit;
lineEdit3 = new QLineEdit;
textEdit = new QTextEdit; textEdit->setPlainText(tr("This widget takes up two thirds of grid layout")); gridLayout->addWidget(textEdit, , , , );
gridLayout->addWidget(label1, , );
gridLayout->addWidget(label2, , );
gridLayout->addWidget(label3, , );
gridLayout->addWidget(lineEdit1, , );
gridLayout->addWidget(lineEdit2, , );
gridLayout->addWidget(lineEdit3, , ); gridLayout->setRowStretch(, );
gridLayout->setRowStretch(, ); gridGroupbox->setLayout(gridLayout);

3.表单布局 QFormLayout

    

思考:

1.表单布局是一种只有2列的布局。

2.表单布局可以用gridlayout实现,但是表单布局简单一些。

3.addRow方法,直接添加label 和 widget。

    formGroupBox = new QGroupBox("Form Layout");
formLayout = new QFormLayout; formLineEdit = new QLineEdit;
comboBox = new QComboBox;
spinBOx = new QSpinBox; formLayout->addRow(tr("Line1"), formLineEdit);
formLayout->addRow(tr("Line2,Long Text:"), comboBox);
formLayout->addRow(tr("Line3"), spinBOx); formGroupBox->setLayout(formLayout);

4.竖直方向布局。

把刚才布局都添加到竖直布局,就是我们整个程序

    mainLayout = new QVBoxLayout;
mainLayout->addWidget(horizontalGroupbox);
mainLayout->addWidget(gridGroupbox);
mainLayout->addWidget(formGroupBox);
mainLayout->addWidget(bigTextEdit);
mainLayout->addWidget(buttonBox, , Qt::AlignRight);

  

1.Basic Layouts的更多相关文章

  1. 读Qt Demo——Basic Layouts Example

    此例程主要展示用代码方式创建控件并用Layout管理类对其进行布局: 例程来自Qt5.2,如过是默认安装,代码位于:C:\Qt\Qt5.2.0\5.2.0\mingw48_32\examples\wi ...

  2. Useful Qt Examples

    Canvas API Basic Layouts Camera Example Video Widget Example Image Viewer Example Part 6 - Loading a ...

  3. Qt Examples Qt实例汇总

    ActiveQt Examples Using ActiveX from Qt applications. Animation Framework Examples Doing animations ...

  4. SiteMesh详解

    Sitemesh是一种页面装饰技术:它通过过滤器(filter)来拦截页面访问,据被访问页面的URL找到合适的装饰模板等等,感兴趣的朋友可以了解下哦 一,基本概念 1,Sitemesh是一种页面装饰技 ...

  5. [转]How To Send Transactional Email In A NodeJS App Using The Mailgun API

    https://www.npmjs.com/package/mailgun-js 本文转自:https://www.mailgun.com/blog/how-to-send-transactional ...

  6. USACO 6.2 Packing Rectangles

    Packing RectanglesIOI 95 The six basic layouts of four rectangles Four rectangles are given. Find th ...

  7. sitemesh 学习之 meta 引入

    在上篇笔记学习了sitemesh的基本用法,这里还有另一种用法 在sitemesh.jar有一个默认的sitemesh-default文件 ,这个文件是可以指定的 可以指定的文件名的sitemesh. ...

  8. Tutorial: Publishing additional services for printing

    Complexity:IntermediateData Requirement:Use your own data There may be occasions when you need to pu ...

  9. BookStore示例项目---菜单栏UI分析

    部署 参照 ABP示例项目BookStore搭建部署 项目解构 1).动态脚本代理 启动项目时,默认会调用两个接口 /Abp/ApplicationConfigurationScript /Abp/S ...

随机推荐

  1. 4.MySQL优化---多表查询优化

     整理自互联网 一.多表查询连接的选择: 相信这内连接,左连接什么的大家都比较熟悉了,当然还有左外连接什么的,基本用不上我就不贴出来了.这图只是让大家回忆一下,各种连接查询. 然后要告诉大家的是,需要 ...

  2. [转载] 最简单的基于FFmpeg的AVDevice例子(读取摄像头)

    =====================================================最简单的基于FFmpeg的AVDevice例子文章列表: 最简单的基于FFmpeg的AVDev ...

  3. .net remoting和wcf自托管——一个bug引发的警示

    一.解决问题,需要深入,并从细节入手,多从代码找原因,不能认为代码是死的,不会出错: 之前代码都运行良好,突然某一天,在我电脑上出问题了.出了问题,那就应该找出原因.其实这个问题,本身并不难,好歹给你 ...

  4. Redis底层探秘(三):字典

    字典,又称为符号表(symbol table).关联数组(associative array)或映射(map),是一种用于保存键值对的抽象数据结构. 字典经常作为一种数据结构内置在很多高级编程语言里面 ...

  5. k2 4.6.9安装记录-够复杂了

    首先需要准备一台Windows server 2008R2 系统.可以从微软官方下载. 下载地址: http://www.microsoft.com/zh-cn/download/confirmati ...

  6. Git在不同环境换行符设置

    首先我们在eclipse查看两个环境文件的换行符区别: 产生背景 关于“回车”(carriage return)和“换行”(line feed)这两个概念的来历和区别.在计算机还没有出现之前,有一种叫 ...

  7. 自己写的工具:把Evernote(印象笔记)的笔记导入到博客(Blog)中

    Evernote是个强大的工具, 这个伴随了我快4年的工具让我积累好多笔记.但是,如何把evernote(印象笔记)中的笔记发布到博客中呢? 自己空闲时候用python 3写了个工具Evernote2 ...

  8. navicat链接远程数据库

    1.之前使用的是常规的连接方式 学习源头: https://jingyan.baidu.com/article/0aa2237573c1e688cc0d6427.html 这里的ip地址是服务器的ip ...

  9. Windows命令查看文件的MD5/SHA1/SHA256

    certutil -hashfile "D:\Tools\Microsoft\SqlServer\2016\ct_sql_server_2016_enterprise_x64_dvd_869 ...

  10. [C++] 动态规划之矩阵连乘、最长公共子序列、最大子段和、最长单调递增子序列、0-1背包

    一.动态规划的基本思想 动态规划算法通常用于求解具有某种最优性质的问题.在这类问题中,可能会有许多可行解.每一个解都对应于一个值,我们希望找到具有最优值的解. 将待求解问题分解成若干个子问题,先求解子 ...