python - 在 QTableView 中显示某些列

标签 python pyqt pyqt5 qtableview qabstracttablemodel

假设我有 QAbstractTableModel,它提供 2 个 QTableView。一个表格 View 显示模型中的所有数据。请告知我如何指定其他表格 View 以仅显示同一模型中的 2 列。

最佳答案

一个简单的解决方案是使用 QTableViewhideColumn() 方法隐藏列。

from PyQt5 import QtCore, QtGui, QtWidgets

class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)

        model = QtGui.QStandardItemModel(5, 5, self)
        for i in range(model.rowCount()):
            for j in range(model.columnCount()):
                it = QtGui.QStandardItem("{}-{}".format(i, j))
                model.setItem(i, j, it)

        table_all = QtWidgets.QTableView()
        table_all.setModel(model)

        table = QtWidgets.QTableView()
        table.setModel(model)

        for column_hidden in (0, 3, 4):
            table.hideColumn(column_hidden)

        lay = QtWidgets.QHBoxLayout(self)
        lay.addWidget(table_all)
        lay.addWidget(table)


if __name__ == '__main__':
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

关于python - 在 QTableView 中显示某些列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52670875/

相关文章:

python - threading.current_thread() 中的 Dummy 是什么?

python - Qtablewidget去除黑色空间PyQt5

python - 如何使用 PyQt5 在 qml 中设置值?

python - pyqt:如何获得对 QFileDialog 中按钮的一致访问

python - 退出python程序后调用bash脚本

python - 如何在 Jinja2 中包含具有相对路径的模板

python - pyqt - 组合框项根据状态启用/禁用 line_edit 项

python - "surrogateescape"无法转义某些字符

python - PyQt Python - 为 QPushButton 创建鼠标右键单击

python - PyQT5 和使用多列过滤表