布局管理之 QBoxLayout (盒子布局)


QBoxLayouot 的描述:

其實,它也一般不單獨使用,它有兩個子類QHBoxLayout  和 QVBoxLayout ,

但是,也可以強行的使用它,不過構造函數中要指定 QBoxLayout.Direction  。

其實兩個子類中的功能都是在這個基類中。

 

QBoxLayout的功能作用:

QBoxLayout的功能作用之修改方向:

 

from PyQt5.Qt import * #剛開始學習可以這樣一下導入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QBoxLayout 的學習")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):
        label1= QLabel("標簽1")
        label1.setStyleSheet("background-color:red;")
        label2= QLabel("標簽2")
        label2.setStyleSheet("background-color:green;")
        label3= QLabel("標簽3")
        label3.setStyleSheet("background-color:yellow;")

        #
        boxLayout = QBoxLayout(QBoxLayout.BottomToTop)

        boxLayout.addWidget(label1)
        boxLayout.addWidget(label2)
        boxLayout.addWidget(label3)

        #修改方向
        timer = QTimer(self)
        def timeout_slot():
            boxLayout.setDirection((boxLayout.direction()+1)%4)
            pass
        timer.timeout.connect(timeout_slot)
        timer.start(1000)

        



        self.setLayout(boxLayout)


if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
修改方向

QBoxLayout的功能作用之添加元素:

from PyQt5.Qt import * #剛開始學習可以這樣一下導入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QBoxLayout 的學習")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):
        label1= QLabel("標簽1")
        label1.setStyleSheet("background-color:red;")
        label2= QLabel("標簽2")
        label2.setStyleSheet("background-color:green;")
        label3= QLabel("標簽3")
        label3.setStyleSheet("background-color:yellow;")

        #
        boxLayout = QBoxLayout(QBoxLayout.BottomToTop)

        boxLayout.addWidget(label1)
        boxLayout.addWidget(label2)
        boxLayout.addWidget(label3)

        #添加元素
        label4= QLabel("標簽4")
        label4.setStyleSheet("background-color:cyan;")
        label5= QLabel("標簽5")
        label5.setStyleSheet("background-color:blue;")
        #添加widget
            # boxLayout.insertWidget(1,label4)
        #添加layout
            # mylayout = QBoxLayout(QBoxLayout.LeftToRight)
            # mylayout.addWidget(label4)
            # mylayout.addWidget(label5)
            #
            # boxLayout.insertLayout(2,mylayout)
        #替換

        #移除label1
            # boxLayout.removeWidget(label1)
            # label1.hide()



        self.setLayout(boxLayout)


if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
View Code

 

下面是添加空白:

from PyQt5.Qt import * #剛開始學習可以這樣一下導入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QBoxLayout 的學習")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):
        label1= QLabel("標簽1")
        label1.setStyleSheet("background-color:red;")
        label2= QLabel("標簽2")
        label2.setStyleSheet("background-color:green;")
        label3= QLabel("標簽3")
        label3.setStyleSheet("background-color:yellow;")

        #
        boxLayout = QBoxLayout(QBoxLayout.BottomToTop)

        boxLayout.addWidget(label1)
        #添加空白  (空白大小不會隨着縮放而變化)
            # boxLayout.addSpacing(100)  # setspacing 是給每一個設置
        boxLayout.addWidget(label2)
        boxLayout.addWidget(label3)

        #插入空白
            # boxLayout.insertSpacing(1,100)

        self.setLayout(boxLayout)

        return None



        #添加元素
        label4= QLabel("標簽4")
        label4.setStyleSheet("background-color:cyan;")
        label5= QLabel("標簽5")
        label5.setStyleSheet("background-color:blue;")

if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
添加,插入空白
from PyQt5.Qt import * #剛開始學習可以這樣一下導入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QBoxLayout 的學習")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):
        label1= QLabel("標簽1")
        label1.setStyleSheet("background-color:red;")
        label2= QLabel("標簽2")
        label2.setStyleSheet("background-color:green;")
        label3= QLabel("標簽3")
        label3.setStyleSheet("background-color:yellow;")

        #
        boxLayout = QBoxLayout(QBoxLayout.TopToBottom)

        boxLayout.addWidget(label1,1)
        #添加伸縮 因子  (彈簧)
        boxLayout.addStretch(2)  #這里需要注意的是,當空間被壓縮的很小的時候,壓縮因子就失效了
        boxLayout.addWidget(label2,2)
        boxLayout.addWidget(label3,2)


        self.setLayout(boxLayout)

        return None



        #添加元素
        label4= QLabel("標簽4")
        label4.setStyleSheet("background-color:cyan;")
        label5= QLabel("標簽5")
        label5.setStyleSheet("background-color:blue;")





if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
伸縮因子(彈簧)

QBoxLayout的功能作用之設置伸縮(添加彈簧):

要注意下面的情況(構造函數中沒有使用伸縮因子這個參數時):

from PyQt5.Qt import * #剛開始學習可以這樣一下導入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QBoxLayout 的學習")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):
        label1= QLabel("標簽1")
        label1.setStyleSheet("background-color:red;")
        label2= QLabel("標簽2")
        label2.setStyleSheet("background-color:green;")
        label3= QLabel("標簽3")
        label3.setStyleSheet("background-color:yellow;")

        #
        boxLayout = QBoxLayout(QBoxLayout.TopToBottom)

        boxLayout.addWidget(label1)
        boxLayout.addStretch()  #加了個彈簧,其他控件是 建議的寬度  
        boxLayout.addWidget(label2)
        boxLayout.addWidget(label3)


        self.setLayout(boxLayout)

        return None


        #添加元素
        label4= QLabel("標簽4")
        label4.setStyleSheet("background-color:cyan;")
        label5= QLabel("標簽5")
        label5.setStyleSheet("background-color:blue;")


if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
View Code
from PyQt5.Qt import * #剛開始學習可以這樣一下導入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QBoxLayout 的學習")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):
        label1= QLabel("標簽1")
        label1.setStyleSheet("background-color:red;")
        label2= QLabel("標簽2")
        label2.setStyleSheet("background-color:green;")
        label3= QLabel("標簽3")
        label3.setStyleSheet("background-color:yellow;")

        #
        boxLayout = QBoxLayout(QBoxLayout.TopToBottom)

        boxLayout.addWidget(label1)
        boxLayout.addStretch()  #加了個彈簧,其他控件是 建議的寬度
        boxLayout.addWidget(label2)
        boxLayout.addStretch()  #加了個彈簧,其他控件是 建議的寬度
        boxLayout.addWidget(label3)


        self.setLayout(boxLayout)

        return None



        #添加元素
        label4= QLabel("標簽4")
        label4.setStyleSheet("background-color:cyan;")
        label5= QLabel("標簽5")
        label5.setStyleSheet("background-color:blue;")





if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
View Code

 

QBoxLayout的功能作用之設置伸縮因子:

from PyQt5.Qt import * #剛開始學習可以這樣一下導入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QBoxLayout 的學習")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):
        label1= QLabel("標簽1")
        label1.setStyleSheet("background-color:red;")
        label2= QLabel("標簽2")
        label2.setStyleSheet("background-color:green;")
        label3= QLabel("標簽3")
        label3.setStyleSheet("background-color:yellow;")

        #
        boxLayout = QBoxLayout(QBoxLayout.TopToBottom)

        boxLayout.addWidget(label1)
        boxLayout.addStretch()  #加了個彈簧,其他控件是 建議的寬度
        boxLayout.addWidget(label2)
        boxLayout.addStretch()  #加了個彈簧,其他控件是 建議的寬度
        boxLayout.addWidget(label3)

        #設置伸縮因子  子控件
            # boxLayout.setStretchFactor(label2,1)
        #設置伸縮因子  子布局
            # mylayout = QBoxLayout(QBoxLayout.LeftToRight)
            # label4= QLabel("標簽4")
            # label4.setStyleSheet("background-color:cyan;")
            # label5= QLabel("標簽5")
            # label5.setStyleSheet("background-color:blue;")
            #
            # mylayout.addWidget(label4)
            # mylayout.addWidget(label5)
            #
            # boxLayout.addLayout(mylayout)
            # #給子布局添加伸縮因子
            # boxLayout.setStretchFactor(mylayout,1)

        self.setLayout(boxLayout)

        return None


if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
View Code

 

QBoxLayout的功能作用之邊距:

 

總結:

到此,就是QBoxLayout  , 而且他的兩個子類和它幾乎都是差不多(差別僅僅是構造的時候是否傳入方向而已)

下面要說的是:QFormLayout 表單布局:https://www.cnblogs.com/zach0812/p/11400796.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM