在表格vi中添加多列

2024-04-26 04:43:28 发布

您现在位置:Python中文网/ 问答频道 /正文

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())

Tags: 方法代码selfnone视图initdeftable
1条回答
网友
1楼 · 发布于 2024-04-26 04:43:28

您可以使用^{}连接单元格

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

相关问题 更多 >