多个标签页中复用同一 QTableView

在 PyQt 中实现在多个标签页中复用同一个 QTableView 实例,复用同一个 QTableView 实例可以减少内存和资源的使用。每个 QTableView 实例都会消耗一定的内存和处理资源,如果每个标签页都创建一个新的实例,会增加系统的负担。通过复用实例,可以显著降低资源消耗,提升应用程序的性能。

1、问题背景

在使用 PyQt5 开发 GUI 程序时,有时需要在多个标签页中显示相同的数据。为了提高性能,希望使用同一个 QTableView 来显示不同标签页中的数据,只需过滤数据即可。

2、解决方案

经过调研,发现 QTableView 不支持在多个标签页中复用。最优雅的解决方案是为每个标签页创建一个独立的 QTableView。

代码例子1创建独立的 QTableView

python 复制代码
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QTabWidget, QTableView, QVBoxLayout, QPushButton

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Multiple TableViews in Tabs")
        self.resize(640, 480)

        # Create a QTabWidget to hold the tabs
        self.tabs = QTabWidget()

        # Create a QTableView for each tab
        self.tableView1 = QTableView()
        self.tableView2 = QTableView()

        # Add the table views to the tabs
        self.tabs.addTab(self.tableView1, "Tab 1")
        self.tabs.addTab(self.tableView2, "Tab 2")

        # Set the central widget to the tab widget
        self.setCentralWidget(self.tabs)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

代码例子2使用同一个 QTableView 过滤数据

由于 QTableView 不支持在多个标签页中复用,因此如果需要在多个标签页中显示相同的数据,但需要过滤数据,可以使用以下方法:

  1. 创建一个 QAbstractItemModel,该模型包含所有数据。
  2. 为每个标签页创建 QTableView,并使用相同的 QAbstractItemModel。
  3. 为每个 QTableView 设置不同的数据过滤器,以便只显示所需的数据。
python 复制代码
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QTabWidget, QTableView, QVBoxLayout, QPushButton, QSortFilterProxyModel

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Multiple TableViews with Filtered Data")
        self.resize(640, 480)

        # Create a QTabWidget to hold the tabs
        self.tabs = QTabWidget()

        # Create a QAbstractItemModel to hold the data
        self.model = QAbstractItemModel()

        # Create QTableViews for each tab
        self.tableView1 = QTableView()
        self.tableView2 = QTableView()

        # Create QSortFilterProxyModels for each table view
        self.proxyModel1 = QSortFilterProxyModel()
        self.proxyModel2 = QSortFilterProxyModel()

        # Set the source model for the proxy models
        self.proxyModel1.setSourceModel(self.model)
        self.proxyModel2.setSourceModel(self.model)

        # Set the proxy models for the table views
        self.tableView1.setModel(self.proxyModel1)
        self.tableView2.setModel(self.proxyModel2)

        # Set the filters for the proxy models
        self.proxyModel1.setFilterKeyColumn(0)
        self.proxyModel1.setFilterFixedString("Tab 1")
        self.proxyModel2.setFilterKeyColumn(0)
        self.proxyModel2.setFilterFixedString("Tab 2")

        # Add the table views to the tabs
        self.tabs.addTab(self.tableView1, "Tab 1")
        self.tabs.addTab(self.tableView2, "Tab 2")

        # Set the central widget to the tab widget
        self.setCentralWidget(self.tabs)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

通过这种方法,你可以在 PyQt 应用程序中轻松地在多个标签页中复用同一个 QTableView 实例,并根据需要对每个标签页的视图进行自定义配置和操作。

相关推荐
放逐者-保持本心,方可放逐18 分钟前
webgl(three.js 与 cesium 等实例应用)之浏览器渲染应用及内存释放的关联与应用
开发语言·javascript·webgl·顶点着色器·three.js 释放·cesium 释放·片元着色器
Fatbobman(东坡肘子)32 分钟前
WWDC 2025 开发者特辑 | 肘子的 Swift 周报 #088
开发语言·macos·ios·swiftui·ai编程·swift·wwdc
南玖yy37 分钟前
深入理解 x86 汇编中的符号扩展指令:从 CBW 到 CDQ 的全解析
开发语言·汇编·arm开发·后端·架构·策略模式
想躺在地上晒成地瓜干41 分钟前
树莓派超全系列教程文档--(57)如何设置 Apache web 服务器
服务器·apache·树莓派·raspberrypi·树莓派教程
尽兴-1 小时前
达梦数据库CASE WHEN条件
数据库·oracle·达梦
云云3211 小时前
亚矩阵云手机针对AdMob广告平台怎么进行多账号的广告风控
大数据·网络·线性代数·游戏·智能手机·矩阵
遗忘妳1 小时前
PostgreSQL初体验
数据库·postgresql
「、皓子~2 小时前
AI创作系列(2):UniApp跨端开发实战 - 海狸IM移动端完全由AI编写
开发语言·人工智能·uni-app·开源·vue·开源软件·ai编程
Sui_Network2 小时前
WAYE.ai 为Sui 上 AI 的下一个时代赋能
大数据·前端·人工智能·物联网·游戏
南無忘码至尊2 小时前
Unity C# 入门基础知识点整理与实战技巧
开发语言·c#