上面的代码 错误 type object 'Qt' has no attribute 'IndexNotFound'
时间: 2023-07-31 16:07:48 浏览: 174
抱歉给出的示例代码中有一个错误。`Qt` 模块中没有 `IndexNotFound` 属性。请改为使用 `QModelIndex()` 来代替。以下是修正后的代码:
```python
import sys
from PyQt5.QtWidgets import QApplication, QTableView, QPushButton, QVBoxLayout, QWidget
from PyQt5.QtGui import QStandardItemModel, QStandardItem, QColor, QFont
from PyQt5.QtCore import QAbstractTableModel, Qt
class ExcelTableModel(QAbstractTableModel):
def __init__(self, data, headers, parent=None):
super(ExcelTableModel, self).__init__(parent)
self.data = data
self.headers = headers
def rowCount(self, parent=QModelIndex()):
return len(self.data)
def columnCount(self, parent=QModelIndex()):
if self.data:
return len(self.data[0])
return 0
def data(self, index, role=Qt.DisplayRole):
if not index.isValid():
return None
row = index.row()
col = index.column()
if role == Qt.DisplayRole or role == Qt.EditRole:
return str(self.data[row][col])
if role == Qt.BackgroundRole:
# 获取单元格的背景颜色
return self.data[row][col].background_color
if role == Qt.FontRole:
# 获取单元格的字体样式
font = QFont()
font.setBold(self.data[row][col].bold)
font.setItalic(self.data[row][col].italic)
font.setUnderline(self.data[row][col].underline)
return font
return None
def setData(self, index, value, role=Qt.EditRole):
if index.isValid() and role == Qt.EditRole:
row = index.row()
col = index.column()
self.data[row][col] = value
self.dataChanged.emit(index, index)
return True
return False
def insertRows(self, position, rows, parent=QModelIndex()):
self.beginInsertRows(parent, position, position + rows - 1)
for _ in range(rows):
row = []
for _ in range(self.columnCount()):
cell = QStandardItem('')
cell.background_color = QColor(255, 255, 0) # 设置为黄色背景
cell.bold = False
cell.italic = False
cell.underline = False
row.append(cell)
self.data.insert(position, row)
self.endInsertRows()
return True
def removeRows(self, position, rows, parent=QModelIndex()):
self.beginRemoveRows(parent, position, position + rows - 1)
del self.data[position:position + rows]
self.endRemoveRows()
return True
def headerData(self, section, orientation, role=Qt.DisplayRole):
if role == Qt.DisplayRole and orientation == Qt.Horizontal:
return self.headers[section]
if role == Qt.DisplayRole and orientation == Qt.Vertical:
return str(section + 1)
return None
if __name__ == '__main__':
app = QApplication(sys.argv)
# 初始数据和列标题
data = [
[QStandardItem('Cell 1'), QStandardItem('Cell 2'), QStandardItem('Cell 3')],
[QStandardItem('Cell 4'), QStandardItem('Cell 5'), QStandardItem('Cell 6')],
[QStandardItem('Cell 7'), QStandardItem('Cell 8'), QStandardItem('Cell 9')]
]
headers = ['Column 1', 'Column 2', 'Column 3']
model = ExcelTableModel(data, headers)
tableView = QTableView()
tableView.setModel(model)
# 插入行按钮
insertButton = QPushButton('Insert Row')
insertButton.clicked.connect(lambda: model.insertRows(0, 1))
# 删除行按钮
deleteButton = QPushButton('Delete Row')
deleteButton.clicked.connect(lambda: model.removeRows(0, 1))
# 创建布局并添加表格视图和按钮
layout = QVBoxLayout()
layout.addWidget(tableView)
layout.addWidget(insertButton)
layout.addWidget(deleteButton)
# 创建窗口并设置布局
window = QWidget()
window.setLayout(layout)
window.resize(400, 300)
window.show()
sys.exit(app.exec_())
```
这个修正后的代码应该可以正常运行,并且包含了插入行和删除行的功能。非常抱歉给你带来的困扰。
阅读全文