QT - QGridLayout 需要不同的标题行间距

标签 qt pyqt

我有一个 QGridLayout,我需要所有行中的间距,但不需要第一行中的间距。我怎样才能做到这一点?在下图中,您可以看到单元格之间的空间。我可以设置间距:

self.grid.setSpacing(0)

但这会影响整个网格。

enter image description here

最佳答案

通过删除水平间距并在标题标签上使用columnSpan,可以很容易地完成此操作。

这是一个简单的演示脚本:

from PyQt4 import QtCore, QtGui

class Window(QtGui.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        layout = QtGui.QGridLayout(self)
        layout.setHorizontalSpacing(0)
        self.setStyleSheet('background: black; color: white')
        headers = 'Name: Item: Color: Status:'.split()
        for column, header in enumerate(headers):
            label = QtGui.QLabel(header)
            label.setStyleSheet('background: silver; padding: 5px')
            if column < len(headers) - 1:
                layout.addWidget(label, 0, 2 * column, 1, 2)
                layout.setColumnMinimumWidth(2 * column + 1, 10)
            else:
                layout.addWidget(label, 0, 2 * column)
            widget = QtGui.QWidget()
            widget.setStyleSheet('border: 1px solid white')
            layout.addWidget(widget, 1, 2 * column)
        layout.setRowStretch(1, 1)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.setGeometry(500, 300, 450, 90)
    window.show()
    sys.exit(app.exec_())

结果:

enter image description here

关于QT - QGridLayout 需要不同的标题行间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31150330/

相关文章:

c++ - 删除指向 QMainWindow 的链接时的 Qt 浏览器堆栈

c++ - 在 Qt 中更改主窗口标题

python - 如何在QWebEnginePage-pyqt5中使用POST方法打开URL

c++ - 在命令行上启动服务器与在应用程序内启动服务器

python - Qt5 涂鸦与图层 - QPaintDevice : cannot destroy device that is being painted

python - 将 python 对象列表传递给 qml

python - 类型错误 : decorated slot has no signal compatible with doubleClicked(QModelIndex)

python - 如何更改标题数据

c++ - 更改 QT 布局背景

python - 检查按下的键是PyQt中的字母还是空格