1
我最近從PyQt 5.7.0更新到5.7.1,並且在更新之前正常工作的代碼停止正常工作。所以,無論是我的代碼總是錯誤的,但PyQt5允許它工作,或者在PyQt 5.7.1中引入了一個錯誤。QTableView不再顯示自更新至5.7.1以來的行
我有一個自定義表格視圖,它使用從QAbstractTableModel繼承的自定義模型從QTableView繼承。當新行添加到模型中時,它們在表格視圖中不可見。事實上,沒有行可見。通過一些調試,我已經驗證了行數在我的派生模型類中按預期發生了變化。
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import PyQt5.Qt
class JobTableModel(QAbstractTableModel):
def __init__(self, data, parent):
super(JobTableModel, self).__init__()
assert isinstance(parent, QTableView), "'parent' is not a QTableView object"
self._parent = parent
self._data = data
self._rows = 0
self._updateModel()
# end constructor
def updateRows(self, rows):
self.layoutAboutToBeChanged.emit()
self._rows = rows
self.layoutChanged.emit()
# end updateRows
def _updateModel(self):
# Only update rows that are visible to the user
# Note: self._parent is a QTableView
minRow = self._parent.rowAt(0)
if minRow >= 0:
maxRow = self._parent.rowAt(self._parent.height())
if maxRow < 0: maxRow = self._rows - 1
for row in range(minRow, maxRow + 1):
self.dataChanged.emit(self.index(row, 0), self.index(row, self.columnCount(None) - 1))
QTimer.singleShot(490, self._updateModel)
# end _updateModel
def headerData(self, section, orientation, role):
if role == Qt.DisplayRole and orientation == Qt.Horizontal:
return str(section)
#
# end headerData
def rowCount(self, modelIndex):
return self._rows
# end rowCount
def columnCount(self, modelIndex):
return 8 # always the same number of columns
# end columnCount
def data(self, index, role):
if not index.isValid(): return None
if role == Qt.DisplayRole: return '{0}, {1}'.format(index.row(), index.column())
return None
# end data
class JobTableView(QTableView):
def __init__(self, data, parent):
super(JobTableView, self).__init__(parent)
self.setModel(JobTableModel(data, self))
self.setAlternatingRowColors(True)
self.setWordWrap(False)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
self.verticalHeader().setVisible(False)
self.verticalHeader().setDefaultSectionSize(23)
self.setSelectionBehavior(QAbstractItemView.SelectRows)
self.setEditTriggers(QAbstractItemView.NoEditTriggers)
if __name__ == '__main__':
app = QApplication(sys.argv)
tv = JobTableView(None, None)
tv.show()
tv.model().updateRows(1)
app.exec_()
因爲PyQt5的維護者永遠不會看到它們,所以在這裏報告潛在的錯誤幾乎沒有意義。無論如何,您首先需要使用[SIP](https://www.riverbankcomputing.com/software/sip/download)和[PyQt5](https://www.riverbankcomputing.com/)的最新快照進行測試軟件/ PyQt的/下載5)。如果問題仍然存在,請將報告發送到相應的[郵件列表](https://www.riverbankcomputing.com/support/lists),以及一個複製問題的小型自包含測試用例。 – ekhumoro
正如第二句話所暗示的,我並沒有在這裏報告錯誤,我試圖找出我的編碼是否有錯,或者是否有錯誤,在這種情況下我會報告錯誤。 – steveo225
在PyQt5或您自己的代碼中顯然有一個錯誤。無論哪種方式,您都需要先使用最新的快照進行測試並提供[mcve]。 – ekhumoro