Box layout

2023-11-16

Layout management with layout classes is much more flexible and practical. It is the preferred way to place widgets on a window. The QtGui.QHBoxLayout and QtGui.QVBoxLayout are basic layout classes that line up widgets horizontally and vertically.

Imagine that we wanted to place two buttons in the right bottom corner. To create such a layout, we will use one horizontal, and one vertical box. To create the necessary space, we will add a stretch factor.

#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
ZetCode PyQt4 tutorial 

In this example, we position two push
buttons in the bottom-right corner 
of the window. 

author: Jan Bodnar
website: zetcode.com 
last edited: October 2011
"""

import sys
from PyQt4 import QtGui

class Example(QtGui.QWidget):
    
    def __init__(self):
        super(Example, self).__init__()
        
        self.initUI()
        
    def initUI(self):
        
        okButton = QtGui.QPushButton("OK")
        cancelButton = QtGui.QPushButton("Cancel")

        hbox = QtGui.QHBoxLayout()
        hbox.addStretch(1)
        hbox.addWidget(okButton)
        hbox.addWidget(cancelButton)

        vbox = QtGui.QVBoxLayout()
        vbox.addStretch(1)
        vbox.addLayout(hbox)
        
        self.setLayout(vbox)    
        
        self.setGeometry(300, 300, 300, 150)
        self.setWindowTitle('Buttons')    
        self.show()
        
def main():
    
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

The example places two buttons in the bottom-right corner of the window. They stay there when we resize the application window. We use both a QtGui.HBoxLayout and a QtGui.QVBoxLayout.

okButton = QtGui.QPushButton("OK")
cancelButton = QtGui.QPushButton("Cancel")

Here we create two push buttons.

hbox = QtGui.QHBoxLayout()
hbox.addStretch(1)
hbox.addWidget(okButton)
hbox.addWidget(cancelButton)

We create a horizontal box layout and add a stretch factor and both buttons. The stretch adds a stretchable space before the two buttons. This will push them to the right of the window.

vbox = QtGui.QVBoxLayout()
vbox.addStretch(1)
vbox.addLayout(hbox)

To create the necessary layout, we put a horizontal layout into a vertical one. The stretch factor in the vertical box will push the horizontal box with the buttons to the bottom of the window.

self.setLayout(vbox)

Finally, we set the main layout of the window.

ButtonsFigure: Buttons

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Box layout 的相关文章

随机推荐

  • HttpMediaTypeNotAcceptableException的解决过程

    今儿的Web项目中突然报错 HttpMediaTypeNotAcceptableException Could not find acceptable representation 涉及接口是 RequestMapping value X
  • JAVA语言特点

    Java语言特点 Java语言是面向对象的 oop Java语言是健壮的 Java的强类型机制 异常处理 垃圾的自动收集等是Java程序健壮性的重要保障 Java语言是跨平台性的 即一个编译好的 class文件可以在多个系统下运行 借助各系
  • 创建Vue3.0工程和常用 Composition API

    一 创建Vue3 0工程 1 使用 vue cli 创建 官方文档 https cli vuejs org zh guide creating a project html vue create 查看 vue cli版本 确保 vue cl
  • QWidget关闭子窗口后显示主窗口(父窗口)

    已经弃坑 不再使用Qt工具 有疑问可以查阅博主的其它博客 看看有没有解决方案 如果还没有可以百度或Google搜一下其他博主的教程 谢谢 Holle 好久没见了 据我上次发博客已经过去很久 今天呢 带给大家一个Qt上的小问题 好多初学者在Q
  • 回顾篇-SpringBoot-Tomcat

    为什么写 今天看了Spring实战第五版 里边有句话如下 传统的基于Servlet的Web框架 如Spring MVC 在本质上都是阻塞和多线程的 每个连接都会使用一个线程 在请求处理的时候 会在线程池中拉取一个工作者 worker 线程来
  • Linux下修改密码命令及查看密码修改时间

    Linux下修改密码命令及查看密码修改时间 1 如修改用户liu的密码为123456 在root用户下执行 echo 123456 passwd stdin liu 2 查看密码修改时间 date d 1970 01 01 more etc
  • CloudCompare——计算点云的KD树并可视化

    目录 1 功能概述 2 完整操作 3 算法源码 4 相关代码 1 功能概述 使用Tools gt Sand box research gt compute kd tree访问此工具 该功能可以用于论文写作中的KD树绘图 2 完整操作 3 算
  • 你的小程序

    你的小程序还未设置管理员信息 无法被绑定 你可先访问mp weixin qq com 在 用户身份 页面设置管理员信息后即可进行绑定 解决方法 换一个浏览器 别用火狐 真XXX 但解决放哪就这么简单
  • 如何在Visual Studio给多行代码进行注释和取消注释

    如何在Visual Studio给多行代码进行注释和取消注释 用鼠标选中要注释的代码 先按Ctr K 键 再按 Ctrl C 键进行注释多行代码 如果要取消注释 用鼠标选中被注释的代码 先按Ctrl K 再按 Ctrl U 即可取消注释
  • 中文OCR识别

    在闲暇时刻做了一个中文识别能力的工程 工程主要对中文文字进行识别 当前工程没有检测文本能力 后续会加入 文本字段在32000验证集合上准确率为83 2 可识别中文字符5990 由于没有时间做太多优化 可能对于相近字体的准确率并不太高 但是可
  • 拿来即用的 Python SSH+SFTP 实现类

    一个拿来即用的 Python SSH 和 SFTP 实现类 可用于 与 Linux 服务器建立持续交互的 SSH 会话 从 Linux 服务器下载远程文件 上传本地文件到 Linux 服务器 新创建一个 linux client py 文件
  • Vue.js中的v-model指令(双向绑定)

    Vue js中v model的作用 v model的作用和使用场景 1 v model的作用 双向绑定 2 v model双向绑定的使用场景 表单 3 总结 v model的作用和使用场景 你好 Vue js作为现在最为常用的前端框架之一
  • UI自动化测试通过飞书发送告警信息

    1 发送纯文本消息 1 1代码如下 usr bin env python coding utf 8 import json import requests url https open feishu cn open apis bot v2
  • Matting(抠图)--用深度学习自动去除照片背景

    转自 https zhuanlan zhihu com p 38031181 https zhuanlan zhihu com p 151212267 现在又有一个 AI 能干 Photoshop 的活了 自动抠图 一键去除照片背景 这款
  • AD如何画多图纸原理图

    很多东西都讲究模块化 比如 程序模块化 原理图也不例外 模块化后的原理图更能直观的显示其原理 文章中操作的图标是什么功能可以先看下参考文献中的博客在来看看我的这篇博客 效果会更好 原本以为原理图多图纸很难 其实也差不多 只是多了几步罢了 多
  • getClassLoader()返回null,getClassLoader()获取为空

    一 问题描述 通过getClassLoader 的方式获取jar包中的资源 为空 this getClassLoader getResources com xxxx test Start class hasMoreElements this
  • gin context和官方context_go-gin框架入门

    gin入门 介绍 gin是一个golang的微框架 封装比较优雅 api友好 源码注释比较明确 具有快速灵活 容错方便等特点 对于golang而言 web框架的依赖要比python java之类的要小的多 完全使用自身net http包封装
  • 课时 10 自测题

    使用存储快照功能需要用到哪些 Kubernetes API 资源对象 多选题 A VolumeSnapshot B VolumeSnapshotClass C VolumeSnapshotContent D PersistentVolume
  • C++学习 十、函数重载,函数模板

    C 学习 十 函数重载 函数模板 前言 函数重载 二义性 强制类型转换 类型与类型引用 默认参数 const指针参数和const引用参数 函数模板 模板函数声明与定义 函数模板重载 显式实例化 显式具体化 重载解析 引导编译器使用函数模板
  • Box layout

    Layout management with layout classes is much more flexible and practical It is the preferred way to place widgets on a