欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 金融 > 多个标签页中复用同一 QTableView

多个标签页中复用同一 QTableView

2024/10/24 15:17:28 来源:https://blog.csdn.net/weixin_44617651/article/details/140313584  浏览:    关键词:多个标签页中复用同一 QTableView

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

在这里插入图片描述

1、问题背景

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

2、解决方案

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

代码例子1创建独立的 QTableView

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QTabWidget, QTableView, QVBoxLayout, QPushButtonclass MainWindow(QMainWindow):def __init__(self):super().__init__()self.setWindowTitle("Multiple TableViews in Tabs")self.resize(640, 480)# Create a QTabWidget to hold the tabsself.tabs = QTabWidget()# Create a QTableView for each tabself.tableView1 = QTableView()self.tableView2 = QTableView()# Add the table views to the tabsself.tabs.addTab(self.tableView1, "Tab 1")self.tabs.addTab(self.tableView2, "Tab 2")# Set the central widget to the tab widgetself.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 设置不同的数据过滤器,以便只显示所需的数据。
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QTabWidget, QTableView, QVBoxLayout, QPushButton, QSortFilterProxyModelclass MainWindow(QMainWindow):def __init__(self):super().__init__()self.setWindowTitle("Multiple TableViews with Filtered Data")self.resize(640, 480)# Create a QTabWidget to hold the tabsself.tabs = QTabWidget()# Create a QAbstractItemModel to hold the dataself.model = QAbstractItemModel()# Create QTableViews for each tabself.tableView1 = QTableView()self.tableView2 = QTableView()# Create QSortFilterProxyModels for each table viewself.proxyModel1 = QSortFilterProxyModel()self.proxyModel2 = QSortFilterProxyModel()# Set the source model for the proxy modelsself.proxyModel1.setSourceModel(self.model)self.proxyModel2.setSourceModel(self.model)# Set the proxy models for the table viewsself.tableView1.setModel(self.proxyModel1)self.tableView2.setModel(self.proxyModel2)# Set the filters for the proxy modelsself.proxyModel1.setFilterKeyColumn(0)self.proxyModel1.setFilterFixedString("Tab 1")self.proxyModel2.setFilterKeyColumn(0)self.proxyModel2.setFilterFixedString("Tab 2")# Add the table views to the tabsself.tabs.addTab(self.tableView1, "Tab 1")self.tabs.addTab(self.tableView2, "Tab 2")# Set the central widget to the tab widgetself.setCentralWidget(self.tabs)if __name__ == "__main__":app = QApplication(sys.argv)window = MainWindow()window.show()sys.exit(app.exec_())

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com