QGridLayout中QPushButton之间的距离

时间:2012-11-27 05:56:57

标签: python user-interface python-2.7 pyqt pyqt4

我编写了以下程序,该游戏为游戏工作者绘制了一个雷区

# -*- coding: utf-8 -*-
import mainw, sys
from PyQt4 import QtCore, QtGui

class WindowSapper(QtGui.QMainWindow):
    buttons=[]

    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self,parent)
        self.ui=mainw.Ui_mainwin()
        self.ui.setupUi(self)
        for i in xrange(10):
            l=[]
            for j in xrange(10):
                b=QtGui.QPushButton()
                l.append(b)
                self.ui.gridLayout.addWidget(b, i, j, 1, 1)
            self.buttons.append(l)

def main():
    app=QtGui.QApplication(sys.argv)
    window=WindowSapper()
    window.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

我也申请表单模块

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'mainw.ui'
#
# Created: Tue Nov 27 08:52:39 2012
#      by: PyQt4 UI code generator 4.9.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_mainwin(object):
    def setupUi(self, mainwin):
        mainwin.setObjectName(_fromUtf8("mainwin"))
        mainwin.resize(546, 530)
        self.centralwidget = QtGui.QWidget(mainwin)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.gridLayoutWidget = QtGui.QWidget(self.centralwidget)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(10, 30, 521, 461))
        self.gridLayoutWidget.setObjectName(_fromUtf8("gridLayoutWidget"))
        self.gridLayout = QtGui.QGridLayout(self.gridLayoutWidget)
        self.gridLayout.setMargin(0)
        self.gridLayout.setHorizontalSpacing(6)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        mainwin.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(mainwin)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 546, 21))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        mainwin.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(mainwin)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        mainwin.setStatusBar(self.statusbar)

        self.retranslateUi(mainwin)
        QtCore.QMetaObject.connectSlotsByName(mainwin)

    def retranslateUi(self, mainwin):
        mainwin.setWindowTitle(QtGui.QApplication.translate("mainwin", "Сапер", None, QtGui.QApplication.UnicodeUTF8))


class mainwin(QtGui.QMainWindow, Ui_mainwin):
    def __init__(self, parent=None, f=QtCore.Qt.WindowFlags()):
        QtGui.QMainWindow.__init__(self, parent, f)

        self.setupUi(self)
但是它没有达到我的预期:按钮没有完全填充GridLayout,其中有自由空间,也就是说,它们并没有完全填充单元格GridLayout。如何摆脱这些差距?

1 个答案:

答案 0 :(得分:4)

你看到大量间距的第一个原因实际上不是因为QGridLayout,而是因为没有任何东西限制你的布局对象使它们聚集在一起。您需要做的是在布局中添加一个拉伸以尽可能多地占用空间,迫使剩下的物品一起推进。

QGridLayout确实允许你向它添加延伸项,但我认为这会使网格导航更加复杂,因为你总是需要考虑该间隔行/列。因此,您可以将网格布局包装在垂直/水平布局中,并为这些布局添加间隔符。

执行此操作后,您会发现行之间留有少量空间。显然,这只是QGridLayout(see this other question)的一个已知事物。但是你可以使用按钮的大小,以及行和列的最小尺寸来实现它:

以下是一个示例(独立 - 不需要您的UI模块)

class WindowSapper(QtGui.QMainWindow):

    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self,parent)
        self.resize(450,350)
        self.centralwidget = QtGui.QWidget()
        self.setCentralWidget(self.centralwidget)

        self.vLayout = QtGui.QVBoxLayout(self.centralwidget)
        self.hLayout = QtGui.QHBoxLayout()

        self.gridLayout = QtGui.QGridLayout()
        self.gridLayout.setSpacing(0)

        # center the grid with stretch on both sides
        self.hLayout.addStretch(1)
        self.hLayout.addLayout(self.gridLayout)
        self.hLayout.addStretch(1)

        self.vLayout.addLayout(self.hLayout)
        # push grid to the top of the window
        self.vLayout.addStretch(1)

        self.buttons = []
        for i in xrange(10):
            l=[]
            for j in xrange(10):
                b=QtGui.QPushButton()
                b.setFixedSize(40,30)
                l.append(b)
                self.gridLayout.addWidget(b, i, j)
                self.gridLayout.setColumnMinimumWidth(j, 40)
            self.buttons.append(l)
            self.gridLayout.setRowMinimumHeight(i, 26)

enter image description here