2

我编写了以下程序,它为工兵游戏画了一个雷区

# -*- 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。如何摆脱这些差距?

4

1 回答 1

4

您看到大量间距的第一个原因实际上不是因为QGridLayout,而是因为没有什么限制您的布局对象使它们聚集在一起。您需要做的是在布局中添加一个拉伸,以尽可能多地占用空间,迫使其余项目挤在一起。

QGridLayout 确实允许您向其中添加拉伸项目,但我认为这会使网格稍后导航更加复杂,因为您始终必须考虑该间隔行/列。因此,您可以将网格布局包装在垂直/水平布局中,并在其中添加间隔。

执行此操作后,您会注意到行之间留有少量空间。显然,这只是 QGridLayout 的一个已知问题(请参阅另一个问题)。但是您可以使用按钮的大小以及行和列的最小大小来使其正确:

这是一个示例(独立 - 不需要您的 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)

在此处输入图像描述

于 2012-11-27T06:46:33.237 回答