gpt4 book ai didi

python - PyQt5:使用 QTableView.setSortingEnabled 和 QSortFilterProxyModel 时的幻像列

转载 作者:行者123 更新时间:2023-11-30 23:03:33 25 4
gpt4 key购买 nike

我有一个自定义 Qt 表模型,允许用户在创建后添加列和行。我正在尝试使用 QSortFilterProxyModel/QTableView 设置来显示此表,但是当我尝试在 TableView 上启用排序时,我遇到了奇怪的行为。我的 View 启动并正确显示添加的数据:

View at launch

但是,当我单击其中一个列标题(进行排序)时,虚拟列将添加到 View 中。

View after header click

有人见过这个或者知道发生了什么事吗?诚然,我还是一个 Qt 新手,所以也许我只是以错误的方式处理这个问题。谢谢。

# -*- mode:python; mode:auto-fill; fill-column:79; coding:utf-8 -*-

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

# =====================================================================
# =====================================================================
class SimpleProxyModel(QSortFilterProxyModel):
def filterAcceptsColumn(self, col, index):
return True

# =====================================================================
# =====================================================================
class SimpleModel(QAbstractTableModel):

def __init__(self):
super(SimpleModel, self).__init__()
self._data = {}

def rowCount(self, index=QModelIndex()):
if len(self._data.values()) > 0:
return len(self._data.values()[0])
else:
return 0

def columnCount(self, index=QModelIndex()):
return len(self._data.keys())

def data( self, index, role = Qt.DisplayRole ):
row, col = index.row(), index.column()

if ( not index.isValid() or
not ( 0 <= row < self.rowCount() ) or
not ( 0 <= col < self.columnCount() ) ):
return QVariant()

if role == Qt.DisplayRole:
return QVariant( self._data[col][row] )

return QVariant()

def addData( self, col, val):

new_col = False
# Turn on flag for new column
if col not in self._data.keys():
new_col = True

if new_col:
self.beginInsertColumns(QModelIndex(), self.columnCount(),self.columnCount())
# Catch this column up with the others by adding blank rows
self._data[col] = [""] * self.rowCount()

self.beginInsertRows(QModelIndex(), self.rowCount(), self.rowCount())
# Add data to each column, either the value specified or a blank
for i in range(self.columnCount()):
if i == col:
self._data[i].append(val)
else:
self._data[i].append( "" )
self.endInsertRows()

if new_col:
self.endInsertColumns()


# =====================================================================
# =====================================================================
class SimpleView(QWidget):

def __init__(self, parent=None):

super(SimpleView, self).__init__(parent)

self._mainmodel = None
self._proxymodel = None
self._tableview = QTableView()
self._tableview.setSortingEnabled(True)

layout = QVBoxLayout()
layout.addWidget( self._tableview )

self.setLayout(layout)

def setModel(self, model):

self._mainmodel = model
proxy = SimpleProxyModel()
proxy.setSourceModel(model)
self._tableview.setModel(proxy)

# =====================================================================
# =====================================================================
app = QApplication([])

v = SimpleView()
m = SimpleModel()
v.setModel( m )

m.addData(0,1)
m.addData(0,2)
m.addData(1,3)

v.show()
app.exec_()

最佳答案

Qt 文档对此相当清楚。您必须调用endInsertRows之后beginInsertRows ,和endInsertColumns之后beginInsertColumns .

上面最后一个链接中最相关的一点是:

Note: This function emits the columnsAboutToBeInserted() signal which connected views (or proxies) must handle before the data is inserted. Otherwise, the views may end up in an invalid state.

因此,您必须开始任何其他插入/删除之前调用endInsertColum()

关于python - PyQt5:使用 QTableView.setSortingEnabled 和 QSortFilterProxyModel 时的幻像列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34033921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com