在表格视图单元格内添加多列

时间:2019-03-22 05:54:39

标签: python pyside pyside2

Click here to see the picture

我正在尝试创建一个在单元格内具有一列的表。有没有一种简单的方法可以将这种设计实现到正在使用的表格视图中?

这是我现在的代码:

class ColumnsLayout(QWidget):
    def __init__(self, parent=None):
        super(ColumnsLayout, self).__init__(parent)
        table_row = QTableWidget()
        table_row.setColumnCount(3)
        table_row.horizontalHeader().setVisible(False)
        for x in range(0, 3):
            table_row.horizontalHeader().setSectionResizeMode(x, QHeaderView.Stretch)
        layout = QHBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)
        layout.addWidget(table_row)
        self.setLayout(layout)
self.ui.fields_tableWidget.setCellWidget(self.current_default_total_row, 2, ColumnsLayout())

1 个答案:

答案 0 :(得分:0)

您可以使用setSpan()来加入单元格

from PySide2 import QtWidgets

class ColumnsLayout(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(ColumnsLayout, self).__init__(parent)
        table_row = QtWidgets.QTableWidget(4, 3)
        table_row.horizontalHeader().hide()
        table_row.setSpan(0, 0, 1, 3)
        for x in range(0, 3):
            table_row.horizontalHeader().setSectionResizeMode(x, QtWidgets.QHeaderView.Stretch)
        layout = QtWidgets.QHBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)
        layout.addWidget(table_row)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = ColumnsLayout()
    w.show()
    sys.exit(app.exec_())

enter image description here