嵌套布局...绘制堆叠不分层

时间:2014-08-18 16:56:42

标签: python qt pyqt

好的......我想念它。我正在尝试创建一个Dialog,其中GridLayout具有多个CheckBox,而下面是HBoxLayout中的Accept,Close PushButton。但是,使用以下代码,我看到GridLayout位于HBoxLayout之上。为什么?如何将对话框标注为GridLayout的宽度?

class CheckerDialog(QtGui.QDialog) :

    def __init__(self, parent, headers) :
        super(CheckerDialog, self).__init__(parent)

        checksLayout = QtGui.QGridLayout()
        self.cbList = []
        i  = 0
        for y in range(13):
            for x in range (9):
                if i == len(headers) : break
                cb = QtGui.QCheckBox(headers[i], self)
                cb.move(OFFSET_X + SPACER_X*x, OFFSET_Y + SPACER_Y*y)
                self.cbList.append(cb)
                i += 1

        buttonLayout = QtGui.QHBoxLayout()
        applyButton = QtGui.QPushButton("Apply")
        closeButton = QtGui.QPushButton("Close")
        buttonLayout.addWidget(applyButton)        
        buttonLayout.addWidget(closeButton)
        self.connect(applyButton, QtCore.SIGNAL('clicked()'), self._apply)
        self.connect(closeButton, QtCore.SIGNAL('clicked()'), self.close)

        checkerLayout = QtGui.QVBoxLayout()
        checkerLayout.addLayout(buttonLayout)
        checkerLayout.addLayout(checksLayout)

1 个答案:

答案 0 :(得分:0)

  
      
  1. HBoxLayout位于GridLayout的顶部 - 不在其下方。
  2.   

是的,它应该在您的代码中。序列在您的代码中有效。

    checkerLayout.addLayout(buttonLayout) # <- TOP
    checkerLayout.addLayout(checksLayout) # <- BOTTOM

如果您想通过指定索引来设置窗口小部件,则可以使用QBoxLayout.insertLayout (self, int index, QLayout layout, int stretch = 0);

    checkerLayout.insertLayout(1, buttonLayout) # <- BOTTOM
    checkerLayout.insertLayout(0, checksLayout) # <- TOP

  
      
  1. 并且GridLayout的大小不控制的大小   整体布局,
  2.   

是的,它应该在您的代码中(再次)。因为{not}放置了QGridLayout中的复选框。您在根布局上设置的几何图形不是QGridLayout。请求使用QGridLayout.addWidget (self, QWidget, int row, int column, Qt.Alignment alignment = 0)添加小部件指定索引(x,y);

小例子:

import sys
from PyQt4 import QtGui

class QCheckerDialog (QtGui.QDialog):
    def __init__ (self, parent = None) :
        super(QCheckerDialog, self).__init__(parent)

        checkBoxQGridLayout = QtGui.QGridLayout()
        for y in range(13):
            for x in range(9):
                currentQCheckBox = QtGui.QCheckBox('%d, %d' % (x, y))
                currentQCheckBox.setSizePolicy(QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Minimum))
                checkBoxQGridLayout.addWidget(currentQCheckBox, x, y)

        buttonQHBoxLayout  = QtGui.QHBoxLayout()
        applyQPushButton = QtGui.QPushButton('Apply')
        closeQPushButton = QtGui.QPushButton('Close')
        buttonQHBoxLayout.addWidget(applyQPushButton)        
        buttonQHBoxLayout.addWidget(closeQPushButton)

        allQVBoxLayout = QtGui.QVBoxLayout()
        allQVBoxLayout.insertLayout(1, buttonQHBoxLayout)
        allQVBoxLayout.insertLayout(0, checkBoxQGridLayout)
        self.setLayout(allQVBoxLayout)

myQApplication = QtGui.QApplication(sys.argv)
myQQCheckerDialog = QCheckerDialog()
myQQCheckerDialog.show()
sys.exit(myQApplication.exec_())

另见:

QBoxLayout.insertLayout (self, int index, QLayout layout, int stretch = 0) Reference

QGridLayout.addWidget (self, QWidget, int row, int column, Qt.Alignment alignment = 0) Reference