开发一个图形界面应用程序,界面的布局影响到界面的美观。在设计一个界面之前,应该考虑到开发的界面可能给不用的用户使用,而用户的屏幕大小、纵横比例、分辨率可能不同,界面还可能是可缩放的,程序应该可以适应这些变化。

前面的程序中都是使用setGeometry()方法定位控件的位置,这个方法比较笨拙。试想如果控件很多,布局这些控件需要编写大量的代码。幸运的是,QT提供了更好的方法布局控件。

常用的三种布局方法:

(1)使用水平布局类QHBoxLayout;

(2)使用垂直布局类QVBoxLayout;

(3)使用网格布局类QGridLayout。

这三种方法可以嵌套使用。

控件在布局时可以先不指定父窗口,最后交由Layout统一指定。

示例:

  1. #include <QApplication>
  2. #include <QDialog>
  3. #include <QPushButton>
  4. #include <QLineEdit>
  5. #include <QLayout>
  6. #include <QLabel>
  7. int main(int argc, char *argv[])
  8. {
  9. QApplication a(argc, argv);
  10. QDialog *mainWindow = new QDialog;
  11. QHBoxLayout *topLayout = new QHBoxLayout;
  12. QLabel *lbl = new QLabel(QWidget::tr("&Input:"), mainWindow);
  13. QLineEdit *lineEdt = new QLineEdit(mainWindow);
  14. lbl->setBuddy(lineEdt);
  15. topLayout->addWidget(lbl);
  16. topLayout->addWidget(lineEdt);
  17. QHBoxLayout *bomLayout = new QHBoxLayout;
  18. QPushButton *btn_ok = new QPushButton(QWidget::tr("OK"), mainWindow);\
  19. btn_ok->setDefault(true);
  20. QPushButton *btn_cancel = new QPushButton(QWidget::tr("Cancel"), mainWindow);
  21. bomLayout->addStretch();
  22. bomLayout->addWidget(btn_ok);
  23. bomLayout->addStretch();
  24. bomLayout->addWidget(btn_cancel);
  25. bomLayout->addStretch();
  26. QVBoxLayout *mainLayout = new QVBoxLayout;
  27. mainLayout->addLayout(topLayout);
  28. mainLayout->addLayout(bomLayout);
  29. mainWindow->setLayout(mainLayout);
  30. mainWindow->resize(300, 100);
  31. mainWindow->setWindowTitle(QWidget::tr("Qt Test"));
  32. mainWindow->show();
  33. return a.exec();
  34. }

编译运行,界面如下:

在界面中,最外部是mainLayout,它的类型是垂直布局类QVBoxLayout。它包含了两个水平布局类QHBoxLayout,分别是topLayout和bomLayout。

比起QHBoxLayout和HVBoxLayout, QGridLayout运用更加灵活。

QGridLayout的常用方法

(1)addWidget:

  1. //放置一个控件到一个单元格
  2. void    addWidget ( QWidget * widget, int row, int column, Qt::Alignment alignment = 0 )
  3. //如果放置的控件超出一个单元格,则使用该方法
  4. void    addWidget ( QWidget * widget, int fromRow, int fromColumn, int rowSpan, int columnSpan, Qt::Alignment alignment = 0 )

1)row:指放置控件的网格行号(行号从0开始);

2)colum:指放置控件的网格列号(从0开始);

3)alignment:对齐方式。

4)fromRow:指放置控件的起始网格行号;

5)fromColumn:指放置控件的起始网格列号;

6)rowSpan:指放置控件占多少行;

7)columnSpan:指放置控件占多少列。

(2)addLayout

  1. void    addLayout ( QLayout * layout, int row, int column, Qt::Alignment alignment = 0 )
  2. void    addLayout ( QLayout * layout, int row, int column, int rowSpan, int columnSpan, Qt::Alignment alignment = 0 )

参数与addWidget类似。

(3)setSpacing

  1. void QGridLayout::setSpacing ( int spacing )

设置控件水平和垂直之间的间隔。

示例:

  1. #include <QApplication>
  2. #include <QDialog>
  3. #include <QPushButton>
  4. #include <QLineEdit>
  5. #include <QLayout>
  6. #include <QLabel>
  7. #include <QTextEdit>
  8. int main(int argc, char *argv[])
  9. {
  10. QApplication a(argc, argv);
  11. QDialog *mainWindow = new QDialog;
  12. QGridLayout *gridLayout = new QGridLayout;
  13. gridLayout->setColumnStretch(0, 1);
  14. gridLayout->setColumnStretch(1, 4);
  15. gridLayout->setColumnStretch(2, 1);
  16. gridLayout->setColumnStretch(3, 1);
  17. gridLayout->setColumnStretch(4, 4);
  18. gridLayout->setMargin(15);
  19. gridLayout->setColumnMinimumWidth(2, 15);
  20. QLabel *lbl1 = new QLabel(QWidget::tr("First Name:"));
  21. QLineEdit *edit1 = new QLineEdit;
  22. QLabel *lbl2 = new QLabel(QWidget::tr("Last Name:"));
  23. QLineEdit *edit2 = new QLineEdit;
  24. QLabel *lbl3 = new QLabel(QWidget::tr("Sex:"));
  25. QLineEdit *edit3 = new QLineEdit;
  26. QLabel *lbl4 = new QLabel(QWidget::tr("Birthday:"));
  27. QLineEdit *edit4 = new QLineEdit;
  28. QLabel *lbl5 = new QLabel(QWidget::tr("Address:"));
  29. QTextEdit *textEdt = new QTextEdit;
  30. gridLayout->addWidget(lbl1, 0, 0);
  31. gridLayout->addWidget(edit1, 0, 1);
  32. gridLayout->addWidget(lbl2, 0, 3);
  33. gridLayout->addWidget(edit2, 0, 4);
  34. gridLayout->addWidget(lbl3, 1, 0);
  35. gridLayout->addWidget(edit3, 1, 1);
  36. gridLayout->addWidget(lbl4, 1, 3);
  37. gridLayout->addWidget(edit4, 1, 4);
  38. gridLayout->addWidget(lbl5, 2, 0);
  39. gridLayout->addWidget(textEdt, 3, 0, 2, 5);
  40. mainWindow->setLayout(gridLayout);
  41. mainWindow->resize(400, 150);
  42. mainWindow->setWindowTitle(QWidget::tr("Qt Test"));
  43. mainWindow->show();
  44. return a.exec();
  45. }

编译运行,界面如图:

转http://blog.csdn.net/xgbing/article/details/7764326

Qt入门-layout布局的更多相关文章

  1. qt坐标系统与布局的简单入门

     qt坐标系统 qt坐标系统比較简单 ); 上面的代码把button显示为父窗体的20,20处宽度为100,高度为100 接下去是布局 qt里面布局须要增加<QLayout.h>这个头 ...

  2. elementUI 学习入门之 layout 布局

    layout 布局 通过基础的 24 分栏,可进行快速布局 基础布局 使用单一分栏创建基础的栅格布局, 通过 span 属性指定每栏的大小 <el-col :span="8" ...

  3. 新建android工程的时候eclipse没有生成MainActivity和layout布局

    一.新建android工程的时候eclipse没有生成MainActivity和layout布局 最近由于工作上的原因,开始学习Android开发,在入门的时候就遇到了不少的坑,遇到的第一个坑就是&q ...

  4. Android实现入门界面布局

    Android实现入门界面布局 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 代码实现 首先是常量的定义,安卓中固定字符串应该定义在常量中. stri ...

  5. Thinkphp入门 四 —布局、缓存、系统变量 (48)

    原文:Thinkphp入门 四 -布局.缓存.系统变量 (48) [控制器操作方法参数设置] http://网址/index.php/控制器/操作方法 [页面跳转] [变量调节器] Smarty变量调 ...

  6. Android开发中遇到的问题(二)——新建android工程的时候eclipse没有生成MainActivity和layout布局

    一.新建android工程的时候eclipse没有生成MainActivity和layout布局 最近由于工作上的原因,开始学习Android开发,在入门的时候就遇到了不少的坑,遇到的第一个坑就是&q ...

  7. qt——常用的布局方法

    布局相关对象及简介 窗体上的所有的控件必须有一个合适的尺寸和位置.Qt提供了一些类负责排列窗体上的控件,主要有:QHBoxLayout,QVBoxLayout,QGridLayout,QStackLa ...

  8. easyui实现树形菜单Tab功能、layout布局

    一:常见三种前端ui框架 在初学者入门的状态下,我们常见的前端框架有三种且都有自己的官方网站: 1.easyui:官方网站(http://www.jeasyui.net/) 基于jquery的用户页面 ...

  9. layout布局实例化

    实例化xml中的Layout布局在开发中经常会用到,有几种方法可以使用 1.在Activity中使用getLayoutInflater()方法 View layout = getLayoutInfla ...

  10. ASP.NET MVC3 系列教程 – 新的Layout布局系统

    原文地址:http://www.cnblogs.com/highend/archive/2011/04/18/asp_net_mvc3_layout.html I:回忆MVC2当中MasterPage ...

随机推荐

  1. python中的sort、sorted、reverse、reversed详解

    python语言中的列表排序方法有三个:reverse反转/倒序排序.sort正序排序.sorted可以获取排序后的列表.在更高级列表排序中,后两中方法还可以加入条件参数进行排序. reverse() ...

  2. sqlserver 创建对某个存储过程执行情况的跟踪

    有时候需要抓取执行存储过程时某个参数的值,有时候程序调用存储过程执行后结果不太对,不确定是程序的问题还是存储过程的问题,需要单独执行存储过程看结果 即可用下面的方法 --=============== ...

  3. spring boot更换日志为log4j2

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...

  4. Linux服务器修改时区时间

    时间的一致性很关键,对于日志的分析和程序的对接都至关重要! 01.tzselect 修改时区 可以使用命令 tzselect,修改时区.操作示例: $ tzselect Please identify ...

  5. Java IO 要点总结

    Java 流在处理上分为字符流和字节流.字符流处理的单元为 2 个字节的 Unicode 字符,分别操作字符.字符数组或字符串,而字节流处理单元为 1 个字节,操作字节和字节数组. Java 内用 U ...

  6. this.class.getClassLoader().getResourceAsStream与this.class.getResourceAsStream

    本文部分转自:http://xixinfei.iteye.com/blog/1256291 this.getClass().getClassLoader().getResource("tem ...

  7. ACM遇到的问题与解决方案

    C++防止栈溢出措施: 只要在你的代码里加上下面这句话, OK,栈溢出直接搞定!!! #pragma comment(linker, "/STACK:102400000,102400000& ...

  8. leetcode185 Department Top Three Salaries

    Employee表存储员工姓名.员工所在公寓.员工工资 Department表存储公寓id 评选出各个公寓的工资前三名的员工. 遇到的问题如下: limit,in等语句不能用在嵌套select语句中, ...

  9. XP和Win 7双系统安装说明和注意事项

    安装前说明: 1.先装XP,再装Windows 7,最好不要反过来,不然XP不会把Windows 7的启动管理器给覆盖掉,会麻烦些.总之遵循“旧版本到新版本”安装原则. 2.如果分区不够大,请用以下软 ...

  10. RabbitMQ消息队列(二):"Hello, World"[转]

    2. Sending 第一个program send.cs:发送Hello world 到queue.正如我们在上篇文章提到的,你程序的第9行就是建立连接,第12行就是创建channel,第14行创建 ...