QT有个官方的例子:Frozen Column Example,在Qt Creator例子查找即可
官方例子python版本:
Frozen Column Example - Qt for Python
不过官方python版应该是机器直接翻译的C++版的,代码都不正常
根据C++和python版,自己实现了python版的,完善部分功能
import sys
from PyQt5.QtWidgets import QApplication, QHeaderView, QTableView, QAbstractItemView, QScrollBar, QFrame
from PyQt5.QtCore import Qt
# from PyQt5.QtCore import Signal, Slot
# from PyQt5.QtCore import SIGNAL, SLOT
from PyQt5.QtGui import QStandardItemModel, QStandardItem
class FreezeTableWidget(QTableView):
"""
冻结部分列类
参照例程 frozencolumn 实现,
https://doc.qt.io/qtforpython/overviews/qtwidgets-itemviews-frozencolumn-example.html?highlight=frozen
中也包含部分修改后的python代码,似乎不完整
"""
def __init__(self, parent=None):
super(FreezeTableWidget, self).__init__()
# 创建一个QTableView子控件
self.frozenTableView = QTableView(self)
self.frozenColumnCount = 1 # 冻结列数,冻结前几列
# connect the headers and scrollbars of both tableviews together
# self.connect(self.horizontalHeader(), SIGNAL('sectionResized(int,int,int)'), self, SLOT('updateSectionWidth(int,int,int)'))
self.horizontalHeader().sectionResized.connect(self.updateSectionWidth)
# self.connect(self.verticalHeader(), SIGNAL('sectionResized(int,int,int)'), self, SLOT('updateSectionHeight(int,int,int)'))
self.verticalHeader().sectionResized.connect(self.updateSectionHeight)
# self.connect(self.frozenTableView.horizontalHeader(), SIGNAL('sectionResized(int,int,int)'), self, SLOT('frozenTableHeaderSectionResized(int,int,int)'))
self.frozenTableView.horizontalHeader().sectionResized.connect(self.frozenTableHeaderSectionResized)
# self.connect(self.frozenTableView.verticalScrollBar(), SIGNAL('valueChanged(int)'), self.verticalScrollBar(), SLOT('setValue(int)'))
self.frozenTableView.verticalScrollBar().valueChanged.connect(self.verticalScrol

该代码示例展示了如何在Python的QT环境中创建一个QTableView,并实现冻结前几列的功能。代码包括初始化QTableView子类,处理列宽和行高的调整,以及同步滚动条的行为。此外,还处理了模型变更时的列显示和隐藏,确保冻结列的宽度与主表格一致。
最低0.47元/天 解锁文章
1316

被折叠的 条评论
为什么被折叠?



