PyQt:如何将QHBoxLayout粘贴到对话框的底边

时间:2014-04-14 18:33:06

标签: python pyqt

ekhumoro使用以下方法为 QFormLayout 精彩地解决了这个问题:

myQFormLayout.setFieldGrowthPolicy(QtGui.QFormLayout.AllNonFixedFieldsGrow)

使用 QHBoxLayout 实现相同的正确语法是什么?

下面的截图显示了GroupBoxAs'和GroupBoxB没有坚持对话窗口......

enter image description here

from PyQt4 import QtGui, QtCore
import sys, os

class Dialog_01(QtGui.QMainWindow):
    def __init__(self):
        super(QtGui.QMainWindow,self).__init__()

        myQWidget = QtGui.QWidget()
        myBoxLayout = QtGui.QFormLayout()
        myBoxLayout.setFieldGrowthPolicy(QtGui.QFormLayout.AllNonFixedFieldsGrow)
        myQWidget.setLayout(myBoxLayout)
        self.setCentralWidget(myQWidget)    

        GroupBoxA = QtGui.QGroupBox('GroupBoxA') 
        LayoutA = QtGui.QHBoxLayout() 
        GroupBoxA.setLayout(LayoutA)

        GroupBoxB = QtGui.QGroupBox('GroupBoxB') 
        LayoutB = QtGui.QHBoxLayout() 
        GroupBoxB.setLayout(LayoutB)    

        myBoxLayout.addRow(GroupBoxA, GroupBoxB)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    dialog_1 = Dialog_01()
    dialog_1.show()
    dialog_1.resize(480,320)
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

经过一些搜索后,我最终使用了QVBoxLayout和QHBoxLayout。这些布局正确调整大小(在Windows和Mac上)。可以使用两个组合来实现一些有趣的小部件放置。这是我放在一起的那个: enter image description here

from PyQt4 import QtGui, QtCore
import sys, os

class Dialog_01(QtGui.QMainWindow):
    def __init__(self):
        super(QtGui.QMainWindow,self).__init__()

        myQWidget = QtGui.QWidget()
        myBoxLayout = QtGui.QVBoxLayout()
        myQWidget.setLayout(myBoxLayout)
        self.setCentralWidget(myQWidget)    

        # Top GroupBox
        TopGroupBox = QtGui.QGroupBox('TopGroupBox')
        TopLayout = QtGui.QHBoxLayout()
        TopGroupBox.setLayout(TopLayout)
        myBoxLayout.addWidget(TopGroupBox)

        # Middle GroupBox
        MiddleGroupBox = QtGui.QGroupBox('MiddleGroupBox')
        MiddleLayout = QtGui.QHBoxLayout()
        MiddleGroupBox.setLayout(MiddleLayout)
        myBoxLayout.addWidget(MiddleGroupBox)

        # Bottom GroupBox
        BottomGroupBox = QtGui.QGroupBox('BottomGroupBox')
        BottomLayout = QtGui.QHBoxLayout()
        BottomGroupBox.setLayout(BottomLayout)
        myBoxLayout.addWidget(BottomGroupBox)

        # Left Bottom Horizontal GroupBox
        LeftHorGroupBox = QtGui.QGroupBox('Left Horizontal')
        LeftHorLayout = QtGui.QHBoxLayout()
        LeftHorGroupBox.setLayout(LeftHorLayout)

        # Left Bottom Horizontal GroupBox
        RightVertGroupBox = QtGui.QGroupBox('Right Vertical')
        RightVertLayout = QtGui.QVBoxLayout()
        RightVertGroupBox.setLayout(RightVertLayout)

        BottomLayout.addWidget(LeftHorGroupBox)
        BottomLayout.addWidget(RightVertGroupBox)

        viewA=QtGui.QListWidget()
        viewB=QtGui.QListWidget()
        viewC=QtGui.QListWidget()
        LeftHorLayout.addWidget(viewA)
        LeftHorLayout.addWidget(viewB)
        LeftHorLayout.addWidget(viewC)

        widgetA=QtGui.QSlider(QtCore.Qt.Horizontal, self)
        widgetB=QtGui.QLineEdit('Some cool LineEdit')
        widgetC=QtGui.QLabel('Some cool Label')
        widgetD=QtGui.QComboBox()
        widgetE=QtGui.QCheckBox('Some cool QCheckBox')
        widgetF = QtGui.QDateEdit()
        RightVertLayout.addWidget(widgetA)
        RightVertLayout.addWidget(widgetB)
        RightVertLayout.addWidget(widgetC)
        RightVertLayout.addWidget(widgetD)
        RightVertLayout.addWidget(widgetE)
        RightVertLayout.addWidget(widgetF)


        # Buttons GroupBox
        ButtonsGroupBox = QtGui.QGroupBox('ButtonsGroupBox')
        ButtonsLayout = QtGui.QHBoxLayout()
        ButtonsGroupBox.setLayout(ButtonsLayout)
        myBoxLayout.addWidget(ButtonsGroupBox)
        Button_01 = QtGui.QPushButton("Button_01")
        Button_02 = QtGui.QPushButton("Button_02")
        ButtonsLayout.addWidget(Button_01)
        ButtonsLayout.addWidget(Button_02)



if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    dialog_1 = Dialog_01()
    dialog_1.show()
    dialog_1.resize(480,320)
    sys.exit(app.exec_())