【问题标题】:qt gridlayout spanning multiple columns跨越多列的qt gridlayout
【发布时间】:2016-01-16 15:14:26
【问题描述】:

我正在尝试创建这个几何图形:

 _ ___
| |   |
|1| 3 |
|_|___|
  |_2_|

第一个盒子又高又瘦,第二个盒子又短又宽。我不能完全得到正确的布局。当我将 1 和 2 的 rowSpan 和 columnSpan 从 0 变为 1 时,我看到底部框的相对位置正确,但高度错误,框 1 的宽度错误。

这个所以 qq 帮助但没有完全解决我的问题:How to arrange the items in QGridLayout as shown?

import sys
from PyQt4 import QtGui
class Example(QtGui.QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):
        textEdit1 = QtGui.QTextEdit("LHS rectangle")      
        textEdit2 = QtGui.QTextEdit("Bottom rectangle")      
        textEdit3 = QtGui.QTextEdit("Central square")      
        self.gridLayout = QtGui.QGridLayout()
        self.gridLayout.addWidget(textEdit1, 0, 0, 2, 0) 
        self.gridLayout.addWidget(textEdit2, 2, 1, 0, 2)
        self.gridLayout.addWidget(textEdit3, 0, 1, 2, 2) 
        self.setLayout(self.gridLayout)
        self.show()

def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

【问题讨论】:

  • 尝试在行跨度、列跨度 (textEdit1,0,0,2,1) 中将 0 替换为 1
  • @tmoreau 在我的机器上,它在 textEdit3 上方而不是在 textEdit3 下方添加了少量 textEdit2 小部件。

标签: qt pyqt pyqt4 pyqt5


【解决方案1】:

您需要设置适当的拉伸因子来更改为每行/列分配的空间量:

    self.gridLayout.addWidget(textEdit1, 0, 0)
    self.gridLayout.addWidget(textEdit2, 1, 1)
    self.gridLayout.addWidget(textEdit3, 0, 1)
    self.gridLayout.setColumnStretch(0, 1)
    self.gridLayout.setColumnStretch(1, 3)
    self.gridLayout.setRowStretch(0, 3)
    self.gridLayout.setRowStretch(1, 1)

结果:

【讨论】:

    猜你喜欢
    • 2018-01-28
    • 1970-01-01
    • 2014-03-09
    • 1970-01-01
    • 2017-11-23
    • 2022-01-05
    • 2019-10-27
    • 1970-01-01
    • 2015-02-10
    相关资源
    最近更新 更多