Qt学习之路_7(线性布局和网格布局初步探索)
来源:互联网 发布:未来软件园mac 编辑:程序博客网 时间:2024/05/22 15:30
QtDesigner这个工具用来设计界面比较方便,如果使用之,则比用纯代码写要容易入手,但是有的时候用代码来写界面可以达到灵活的设计,所以也需要掌握之。
Qt中的布局有垂直布局,水平布局,这2种布局用法一模一样,在android中这种布局叫做叫做线性布局,我这里也暂这么叫着;另外1种为网格布局,即纵横交错;最后1种叫做form布局.下面简单介绍下用纯Qt C++语言进行垂直水平布局和网格布局。
首先来看布局:
效果如下:
cpp代码和注释如下:
#include <QApplication>#include <QPushButton>#include <QHBoxLayout>//水平布局的库#include <QVBoxLayout>//垂直布局的库 int main( int argc, char *argv[]){ QApplication app(argc, argv); QWidget *window = new QWidget;//创建主窗口 //创建3个pushbutton按钮 QPushButton *btn1 = new QPushButton("one"); QPushButton *btn2 = new QPushButton("two"); QPushButton *btn3 = new QPushButton("three"); //创建一个垂直布局,并将三个pushbutton放入其中 QHBoxLayout *hlayout = new QHBoxLayout; hlayout->addWidget(btn1); hlayout->addWidget(btn2); hlayout->addWidget(btn3); //将垂直布局放入主窗口中并显示 window->setLayout(hlayout); window->show(); return app.exec();//程序一直在这个地方等待,循环。}
然后看看网格布局,效果如下:
对应的cpp代码和注释如下:
//网格布局#include <QApplication>#include <QtGui>#include <QtCore> int main( int argc, char *argv[]){ QApplication app(argc, argv); QWidget *window = new QWidget;//创建主窗口 window->setWindowTitle("Grid layout");//设置窗口名字 //创建网格布局 QGridLayout *grid_layout = new QGridLayout; //创建各种需要显示的控件 QLabel *label_name = new QLabel("Name:"); QLabel *label_id = new QLabel("Student ID:"); QLineEdit *edit_name = new QLineEdit; QLineEdit *edit_id = new QLineEdit; QPushButton *button_ok = new QPushButton("ok"); //将需要显示的控件都加入到网格布局中,注意其参数代表的含义 grid_layout->addWidget(label_name, 0, 0);//表示放在第0行第0列的位置上 grid_layout->addWidget(label_id, 1, 0); grid_layout->addWidget(edit_name, 0, 1); grid_layout->addWidget(edit_id, 1, 1); grid_layout->addWidget(button_ok, 2, 0, 1, 2);//最后2个参数表示其体积所占的行数和列数 //将布局文件加载到窗口中并显示出来 window->setLayout(grid_layout); window->show(); return app.exec();//程序一直在这个地方等待,循环。}
总结:Qt中的布局和android的布局类似,但是感觉用代码写起来Qt没有android方便,而用图像化操作的话Qt又要方便些,各有所长吧。
0 0
- Qt学习之路_7(线性布局和网格布局初步探索)
- Qt学习之路_7(线性布局和网格布局初步探索)
- Qt学习之路_7(线性布局和网格布局初步探索)
- android之网格布局和线性布局实现注册页面
- 初步学习Qt布局
- 初步学习Qt布局
- recycleview线性布局和网格布局的切换
- RecyclerView线性布局和网格布局的切换
- QT网格布局
- QT网格布局
- Qt网格布局
- QT网格布局
- Android学习-五布局之线性布局
- Android布局(线性布局 网格布局 帧布局)
- Layout布局之网格布局
- 相对布局和网格布局
- 边界布局和网格布局
- Android学习之线性布局
- 举例阐述游戏战斗系统设计的基本规则
- Dubbo&ZK分布式服务化改造(四)——Dubbo多注册中心 & 服务迁移
- Web 安全问题
- xml在java中的运用
- 8天学通MongoDB——第七天 运维技术
- Qt学习之路_7(线性布局和网格布局初步探索)
- 排序
- 类加载器
- 史上最详细的javascript闭包(Closure)说明
- Android反编译
- 实例变量与局部变量
- Andorid之注解插件Android ButterKnife Zelezny的安装与使用,插件ADBWIFI
- iOS UILabel 自适应高度宽度
- RISCV: Platform-Level Interrupt Controller(PLIC)