嵌套布局。。。绘制堆叠未分层

2024-05-19 13:24:42 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧。。。我错过了。我正在尝试创建一个对话框,它有一个GridLayout和一些复选框,下面是一个HBoxLayout中的Accept,Close按钮。但是在下面的代码中,我看到的是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)

Tags: selfcloseinitparentheaders对话框cbqtgui
1条回答
网友
1楼 · 发布于 2024-05-19 13:24:42
  1. the HBoxLayout is ON TOP of the GridLayout - not beneath it.

是的,应该在你的代码里。序列在代码中有作用。在

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

如果您想通过指定索引设置widget,可以使用QBoxLayout.insertLayout (self, int index, QLayout layout, int stretch = 0)

^{pr2}$
  1. And the size of the GridLayout does NOT control the size of the overall Layout,

是的,它应该在你的代码中(再次)。因为您的not put复选框在QGridLayout中。根布局上的刚设置几何图形不是QGridLayout。请使用QGridLayout.addWidget (self, QWidget, int row, int column, Qt.Alignment alignment = 0)添加widget指定的索引(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

相关问题 更多 >