我可以在pyQt中使窗口小部件跨多列吗

时间:2020-10-22 22:54:41

标签: python pyqt

我想要这样的东西:

self.name_textbox.setColumnSpan(2)

已经尝试将其他所有内容放入一个巨型列中。
效果不好。

1 个答案:

答案 0 :(得分:0)

无法在小部件上设置行和列的跨度,这是布局的责任。

要更改已在布局中的窗口小部件的跨度,只需使用新的跨度值再次调用addWidget()

class Test(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        layout = QtWidgets.QGridLayout(self)
        expandButton = QtWidgets.QPushButton('Expand')
        layout.addWidget(expandButton)
        expandButton.clicked.connect(self.expand)
        collapseButton = QtWidgets.QPushButton('Collapse')
        layout.addWidget(collapseButton, 0, 1)
        collapseButton.clicked.connect(self.collapse)
        self.lineEdit = QtWidgets.QLineEdit()
        layout.addWidget(self.lineEdit)

    def expand(self):
        self.layout().addWidget(self.lineEdit, 1, 0, 1, 2)

    def collapse(self):
        self.layout().addWidget(self.lineEdit, 1, 0, 1, 1)
相关问题