1. 程式人生 > >PyQt基本佈局常用方法之addStretch

PyQt基本佈局常用方法之addStretch

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
 class LayoutDialog(QDialog):
    def __init__(self,parent=None):
        super(LayoutDialog,self).__init__(parent)
        self.resize(200,600)
        btn1    =   QPushButton("btn1")
        btn2    =   QPushButton("btn2")
        btn3    =   QPushButton("btn3")

        vlayout =   QVBoxLayout(self)
        vlayout.addStretch(1)
        vlayout.addWidget(btn1)
        vlayout.addStretch(1)
        vlayout.addWidget(btn2)
        vlayout.addStretch(2)
        vlayout.addWidget(btn3)
app = QApplication(sys.argv)
dialog = LayoutDialog()
dialog.show()
app.exec_()


我們先看一下addStretch的原型:

QBoxLayout.addStretch (self, int stretch =0)

Adds a stretchable space (a QSpacerItem)with zero minimum size and stretch factor stretch to the end of this boxlayout.

See also insertStretch(), addItem(), andQSpacerItem.

然後通過上面的程式碼來學習一下addStretch的用法。

達到的效果圖如下:

所以該函式的作用就是平分佈局,它所帶的引數就是所佔的比例。