如何设置 QTable 单元格始终处于编辑模式?
How to set QTable cell always in edit mode?
我有一个 table 需要所有 table 单元格始终处于编辑模式,我现在忘记了如何设置此行为,有人可以提供一些提示吗?
演示
import os
import platform
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class SpinBoxDelegate(QStyledItemDelegate):
def createEditor(self, parent: QWidget, option: 'QStyleOptionViewItem', index: QModelIndex) -> QWidget:
editor = QSpinBox(parent)
editor.setRange(0, 10)
return editor
def setEditorData(self, editor: QWidget, index: QModelIndex) -> None:
value = index.data(Qt.EditRole)
if value is None: value = 0
editor.setValue(value)
def setModelData(self, editor: QWidget, model: QAbstractItemModel, index: QModelIndex) -> None:
value = editor.value()
model.setData(index, value, Qt.EditRole)
def updateEditorGeometry(self, editor: QWidget, option: 'QStyleOptionViewItem', index: QModelIndex) -> None:
editor.setGeometry(option.rect)
app = QApplication([])
win = QTableWidget()
win.setColumnCount(20)
win.setRowCount(20)
win.setItemDelegate(SpinBoxDelegate())
for row in range(win.rowCount()):
for col in range(win.columnCount()):
win.openPersistentEditor(win.item(row,col))
win.resize(1000, 500)
win.show()
app.exec()
我听说有两种方法,第一种是使用 openPersistentEditor
,第二种是使用 QStyledItemDelegate
,但我现在忘了怎么做,而且我的代码示例不起作用。
在一个QTableWidget中,虽然有行和列,但并不意味着有与单元格关联的QTableWidgetItems。在这种情况下,item 方法返回 None,因此 openPersistentEditor 将不执行任何操作。解决方案是在每个单元格上设置 QTableWidgetItem。
for row in range(win.rowCount()):
for col in range(win.columnCount()):
it = win.item(row, col)
if it is None:
it = QTableWidgetItem()
win.setItem(row, col, it)
win.openPersistentEditor(it)
我有一个 table 需要所有 table 单元格始终处于编辑模式,我现在忘记了如何设置此行为,有人可以提供一些提示吗?
演示
import os
import platform
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class SpinBoxDelegate(QStyledItemDelegate):
def createEditor(self, parent: QWidget, option: 'QStyleOptionViewItem', index: QModelIndex) -> QWidget:
editor = QSpinBox(parent)
editor.setRange(0, 10)
return editor
def setEditorData(self, editor: QWidget, index: QModelIndex) -> None:
value = index.data(Qt.EditRole)
if value is None: value = 0
editor.setValue(value)
def setModelData(self, editor: QWidget, model: QAbstractItemModel, index: QModelIndex) -> None:
value = editor.value()
model.setData(index, value, Qt.EditRole)
def updateEditorGeometry(self, editor: QWidget, option: 'QStyleOptionViewItem', index: QModelIndex) -> None:
editor.setGeometry(option.rect)
app = QApplication([])
win = QTableWidget()
win.setColumnCount(20)
win.setRowCount(20)
win.setItemDelegate(SpinBoxDelegate())
for row in range(win.rowCount()):
for col in range(win.columnCount()):
win.openPersistentEditor(win.item(row,col))
win.resize(1000, 500)
win.show()
app.exec()
我听说有两种方法,第一种是使用 openPersistentEditor
,第二种是使用 QStyledItemDelegate
,但我现在忘了怎么做,而且我的代码示例不起作用。
在一个QTableWidget中,虽然有行和列,但并不意味着有与单元格关联的QTableWidgetItems。在这种情况下,item 方法返回 None,因此 openPersistentEditor 将不执行任何操作。解决方案是在每个单元格上设置 QTableWidgetItem。
for row in range(win.rowCount()):
for col in range(win.columnCount()):
it = win.item(row, col)
if it is None:
it = QTableWidgetItem()
win.setItem(row, col, it)
win.openPersistentEditor(it)