当前位置:网站首页>Pyqt5 rapid development and practice 6.4 qboxlayout (box layout)
Pyqt5 rapid development and practice 6.4 qboxlayout (box layout)
2022-07-29 09:26:00 【Ding Jiaxiong】
PyQt5 Rapid development and actual combat
List of articles
6. The first 6 Chapter PyQt5 Layout management
6.4 QBoxLayout( Box layout )
use QBoxLayout Class can arrange controls horizontally and vertically ,QHBoxLayout and QVBoxLayout Class inherits from QBoxLayout class .
6.4.1 QHBoxLayout( Horizontal layout )
use QHBoxLayout class , Add controls in left to right order .
QHBoxLayout Common methods in class :
Method | describe |
---|---|
addLayout(self , QLayout , stretch = 0 ) | Add a layout on the right side of the window , Use stretch( Expansion and contraction ) To stretch , The expansion amount defaults to 0 |
addWidget(self , QWidget , stretch , Qt.Alignment alignment) | Add controls to layout :stretch( Expansion and contraction ), Only applicable to QBoxLayout, Controls and windows will grow as the amount of scaling increases ; |
alignment, Specify the alignment | |
addSpacing(self , int) | Set the upper and lower spacing of each control , Additional space can be added through this method |
QHBoxLayout Class inheritance structure :
QObject → QLayout → QBoxLayout → QHBoxLayout
QHBoxLayout Alignment parameters used in layout :
Parameters | describe |
---|---|
Qt.AlignLeft | Align left horizontally |
Qt.AlignRight | Align horizontally to the right |
Qt.AlignCenter | Align horizontally and Center |
Qt.AlignJustify | Align both ends horizontally |
Qt.AlignTop | Vertical alignment up |
Qt.AlignBottom | Align vertically down |
Qt.AlignVCenter | Align vertically in the middle |
Horizontal layout case
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton
class Winform(QWidget):
def __init__(self, parent=None):
super(Winform, self).__init__(parent)
self.setWindowTitle(" Horizontal layout management example ")
# The horizontal layout is in order from left to right. Add button parts .
hlayout = QHBoxLayout()
hlayout.addWidget(QPushButton(str(1)))
hlayout.addWidget(QPushButton(str(2)))
hlayout.addWidget(QPushButton(str(3)))
hlayout.addWidget(QPushButton(str(4)))
hlayout.addWidget(QPushButton(str(5)))
self.setLayout(hlayout)
if __name__ == "__main__":
app = QApplication(sys.argv)
form = Winform()
form.show()
sys.exit(app.exec_())
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton
from PyQt5.QtCore import Qt
class Winform(QWidget):
def __init__(self, parent=None):
super(Winform, self).__init__(parent)
self.setWindowTitle(" Horizontal layout management example ")
self.resize(800, 200)
# The horizontal layout is in order from left to right. Add button parts .
hlayout = QHBoxLayout()
# Level left Vertical
hlayout.addWidget(QPushButton(str(1)), 0, Qt.AlignLeft | Qt.AlignTop)
hlayout.addWidget(QPushButton(str(2)), 0, Qt.AlignLeft | Qt.AlignTop)
hlayout.addWidget(QPushButton(str(3)))
# Level left Vertical
hlayout.addWidget(QPushButton(str(4)), 0, Qt.AlignLeft | Qt.AlignBottom)
hlayout.addWidget(QPushButton(str(5)), 0, Qt.AlignLeft | Qt.AlignBottom)
self.setLayout(hlayout)
if __name__ == "__main__":
app = QApplication(sys.argv)
form = Winform()
form.show()
sys.exit(app.exec_())
You can also use setSpacing(int) Set the spacing between the controls .
6.4.2 QVBoxLayout( Vertical layout )
use QVBoxLayout class , Add controls in top-down order .
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton
class Winform(QWidget):
def __init__(self, parent=None):
super(Winform, self).__init__(parent)
self.setWindowTitle(" Vertical layout management example ")
self.resize(330, 150)
# The vertical layout is carried out from top to bottom. Add button parts .
vlayout = QVBoxLayout()
vlayout.addWidget(QPushButton(str(1)))
vlayout.addWidget(QPushButton(str(2)))
vlayout.addWidget(QPushButton(str(3)))
vlayout.addWidget(QPushButton(str(4)))
vlayout.addWidget(QPushButton(str(5)))
self.setLayout(vlayout)
if __name__ == "__main__":
app = QApplication(sys.argv)
form = Winform()
form.show()
sys.exit(app.exec_())
6.4.3 addStrech() Use of functions
In the layout, we need to use addStretch() function . Set up stretch After expansion , Allocate the remaining space proportionally .
function | describe |
---|---|
QBoxLayout.addStretch(int stretch = 0) | addStretch() Function to add a scalable control to the layout manager (QSpaceltem),0 Is the minimum , And will stretch Add to the end of the layout as a scaling amount ;stretch The parameter represents the proportion of equal distribution , The default value is 0 |
The above figure shows what it looks like when scaling is not set
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton
import sys
class WindowDemo(QWidget):
def __init__(self):
super().__init__()
btn1 = QPushButton(self)
btn2 = QPushButton(self)
btn3 = QPushButton(self)
btn1.setText('button 1')
btn2.setText('button 2')
btn3.setText('button 3')
hbox = QHBoxLayout()
# Set the expansion amount to 1
# hbox.addStretch(1)
hbox.addWidget(btn1)
# Set the expansion amount to 1
# hbox.addStretch(1)
hbox.addWidget(btn2)
# Set the expansion amount to 1
# hbox.addStretch(1)
hbox.addWidget(btn3)
# Set the expansion amount to 1
hbox.addStretch(1)
self.setLayout(hbox)
self.setWindowTitle("addStretch Example ")
self.resize(600,50)
if __name__ == "__main__":
from pyqt5_plugins.examples.exampleqmlitem import QtCore
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
app = QApplication(sys.argv)
win = WindowDemo()
win.show()
sys.exit(app.exec_())
After setting expansion and contraction
Add a telescopic control before the first control , All controls will be displayed on the right
Empathy , Add a telescopic control after the last control , All controls will be displayed on the left
边栏推荐
- 使用cpolar发布树莓派网页(cpolar功能的完善)
- Tesseract text recognition -- simple
- On contract testing
- OpenCV入门基础学习
- No swagger, what do I use?
- 【集中培训】HCIP-Cloud Computing 资源交流帖
- Cloud native management practice: business led Devops continuous delivery system
- 网络安全(5)
- Unity xchart3.0 basic usage quick start
- The biggest upgrade of Bluetooth over the years: Bluetooth Le audio is about to appear in all kinds of digital products
猜你喜欢
How to introduce your project experience?
MySQL error summary
Floweable advanced
MySQL 错误总结
分布式Session共享的4类技术方案,与优劣势比较
Axurerp prototype design starts quickly
Will the modified data be updated when it is the same as the original data?
36. JS动画
View port occupancy
Cloud native management practice: business led Devops continuous delivery system
随机推荐
36. JS animation
当 update 修改数据与原数据相同时会被更新吗?
Rocky基础之编译安装apache
NFA determination and DFA minimization based on C language
Database system design: partition
QMainWindow 详解
A structured random inactivation UNET for retinal vascular segmentation
【机器学习】朴素贝叶斯代码练习
(视频+图文)机器学习入门系列-第1章 引言
Summary of research on endogenous information security technology of industrial measurement and control equipment
Acwing game 59 [End]
如何介绍自己的项目经验?
How to change MySQL into Chinese
One click automated data analysis! Come and have a look at these treasure tool libraries
网络安全(6)
Custom configuration
乱打日志的男孩运气怎么样我不知道,加班肯定很多
[苹果开发者账号]06 转让开发者账号后,开发者年费自动续费问题
The biggest upgrade of Bluetooth over the years: Bluetooth Le audio is about to appear in all kinds of digital products
Discussion on the integration of storage and calculation and the calculation in storage