冻结表格列PyQt

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

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
本文章已经生成可运行项目
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值