2个tablewdiget实现首行冻结效果

思路借鉴:PYQT之表格控件QTableWidget复杂表头(多行表头) 及冻结行的简单方法_pyqt5 qtablewidget 表头合并-CSDN博客

PYQT之表格控件QTableWidget复杂表头(多行表头) 及冻结行的简单方法
表格需要用到复杂表头,查了好久觉得方法不是过于麻烦就是不符合表格要求,经过多方综合,总结超简单方法如下:
1.上下布局两个tableWidget。上面的命名为tableWidget_title,用于制作复杂表头,将垂直和水平表头隐藏,将垂直和水平滚动条隐藏,设置最小高度以保证表头一直显示完全;下面的命名为tableWidget_content,用于显示表主体内容,将垂直和水平表头隐藏,将垂直滚动条隐藏
2.设置tableWidget_title和tableWidget_content列宽,两者保持列宽一致。
3.对tableWidget_title使用setSpan函数合并表头单元格,使用setItem函数设置表头内容,完成复杂表头制作。
4.对tableWidget_content进行表主体内容填充(QtableWdiget基本用法,不赘述)。
!!!问题来了!!!如何使tableWidget_title与tableWidget_content联动,实现拖动下方tableWidget_content水平滚动条,上方tableWidget_title同步移动。
————————————————

                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
                        
原文链接:https://blog.csdn.net/DanChunErYongGan/article/details/108081651

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'firstfrezee.py'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.
from PyQt5.Qt import *
from Qt5_demo.source.ui.frezee_first import Ui_Form
from PyQt5 import QtCore, QtGui, QtWidgets
class RegisterPane(QWidget,Ui_Form):

    def __init__(self,parent = None,*args,**kwargs):
        super().__init__(parent, *args, **kwargs)
        self.setupUi(self)
        self.titlewdiget()
        self.contentwdiget()
        self.signal_bar()
    def titlewdiget(self):
        self.tableWidget.setRowCount(1)
        self.tableWidget.setColumnCount(6)
        self.tableWidget.verticalHeader().setVisible(False)
        self.tableWidget.horizontalHeader().setVisible(False)
        # self.tableWidget.setStyleSheet("border-bottom:none;")
        for row in range(self.tableWidget.rowCount()):
            for col in range(self.tableWidget.columnCount()):
                item = QTableWidgetItem(f"Cell {row + 1}, {col + 1}")
                self.tableWidget.setItem(row, col, item)
        for column in range(self.tableWidget.columnCount()):
            self.tableWidget.setColumnWidth(column, 100)
            if column == self.tableWidget.columnCount() - 1:
                self.tableWidget.setColumnWidth(column, 100)

        self.tableWidget.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.tableWidget.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)

    def contentwdiget(self):
        self.tableWidget_2.setRowCount(15)
        self.tableWidget_2.setColumnCount(6)
        self.tableWidget_2.verticalHeader().setVisible(False)
        self.tableWidget_2.horizontalHeader().setVisible(False)
        self.tableWidget_2.setGeometry(QtCore.QRect(70, 49, 691, 401))
        for row in range(self.tableWidget_2.rowCount()):
            for col in range(self.tableWidget_2.columnCount()):
                item = QTableWidgetItem(f"Cell {row + 1}, {col + 1}")
                self.tableWidget_2.setItem(row, col, item)
        for column in range(self.tableWidget_2.columnCount()):
            self.tableWidget_2.setColumnWidth(column, 100)
        # self.tableWidget_2.setStyleSheet("border-top:none;")
    def signal_bar(self):
        # 信号连接

        self.tableWidget_2.horizontalScrollBar().valueChanged.connect(self.setValue)

    # 槽函数
    def setValue(self):
        print(1111)
        self.tableWidget.horizontalScrollBar().setValue(
            self.tableWidget_2.horizontalScrollBar().value())


if __name__ =='__main__':
    import sys
    app = QApplication(sys. argv)
    window = RegisterPane()

    window.show()
    sys.exit(app.exec_())
 

2个tablewdiget是用designer画的:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'frezee_first.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(992, 707)
        self.widget = QtWidgets.QWidget(Form)
        self.widget.setGeometry(QtCore.QRect(100, 120, 831, 521))
        self.widget.setObjectName("widget")
        self.tableWidget = QtWidgets.QTableWidget(self.widget)
        self.tableWidget.setGeometry(QtCore.QRect(70, 20, 691, 61))
        self.tableWidget.setStyleSheet("border-bottom-color: rgb(255, 255, 255);")
        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.setColumnCount(0)
        self.tableWidget.setRowCount(0)
        self.tableWidget_2 = QtWidgets.QTableWidget(self.widget)
        self.tableWidget_2.setGeometry(QtCore.QRect(70, 70, 691, 401))
        self.tableWidget_2.setStyleSheet("border-top-color: rgb(255, 255, 255);")
        self.tableWidget_2.setObjectName("tableWidget_2")
        self.tableWidget_2.setColumnCount(0)
        self.tableWidget_2.setRowCount(0)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
  • 59
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值