简体   繁体   English

QGridLayout中的小部件之间的间距如何在调整窗口大小时保持固定?

[英]How can the spacing between widgets in a QGridLayout remain fixed on window resize?

I am building an application in PyQT4 . 我正在PyQT4构建应用程序。 A principle part of this app will be to maintain a grid of widgets (sub classed from QLineEdit widgets). 该应用程序的主要部分是维护小部件的网格(从QLineEdit小部件中分类)。 I am organizing the widgets into a QGridLayout . 我正在将小部件组织到QGridLayout

When I run the window, I get a grid organized just how I want, ie 当我运行窗口时,我得到的网格就是我想要的,即

网格(初始大小)

However, the QGridLayout has the property that it automatically pads the spacing between widgets when the window is resized, ie 但是, QGridLayout具有以下属性:当调整窗口大小时,它会自动QGridLayout小部件之间的间距,即

网格(扩展尺寸)

I would love for the grid to have the same spacing between widgets, no matter how I resize the window. 无论我如何调整窗口大小,我都希望网格在小部件之间具有相同的间距。 I have looked and cannot seem to find how to accomplish this. 我已经看过,但似乎找不到解决方法。 I would have imagined something that fixes the spacing, but none of the likely sounding functions have this effect. 我本来可以想象固定间隔的东西,但是可能的探测功能都没有这种作用。

Here is a code snippet below, specifically just the part with the QGridLayout . 这是下面的代码片段,特别是QGridLayout的部分。

class GridBlockTxtbx(QtGui.QWidget):

    def __init__(self, blocks=5, spaces=5):
        QtGui.QWidget.__init__(self)
        self.dctn_txtbx = {}
        self.blocks = blocks
        self.spaces = spaces

        # Create layout
        layout = QtGui.QGridLayout()
        # Set initial spacing between widgets to 2 pixels...
        # I want this to be fixed on window resize!
        layout.setSpacing(2)
        # Function to load the widgets into the grid
        GridBlockTxtbx._gen_block_txtbx_grid(layout, self.blocks,     
                                             self.spaces,
                                             self.dctn_txtbx)
        # Set the layout
        self.setLayout(layout)

    def _gen_block_txtbx_grid(layout, rows, cols, dctn):
        for i in range(rows):
            for j in range(cols):
                blk = GridBlockTxtbx._gen_block_txtbx(idx=(i, j))
                layout.addWidget(blk, i, j)
                dctn[i, j] = blk

Add an expanding spacer to the last row/column of the grid-layout: 在网格布局的最后一行/列中添加一个扩展的间隔符:

    vspacer = QtGui.QSpacerItem(
        QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
    layout.addItem(vspacer, last_row, 0, 1, -1)

    hspacer = QtGui.QSpacerItem(
        QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
    layout.addItem(hspacer, 0, last_column, -1, 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM