Python 如何创建组合框QItemDelegate
我实现了以下委托,以在QTableView中提供一个组合框。用例是用文本等价物替换通常对用户没有意义的列(键)(例如数字id) 下面的代码段可以工作(也可以保存正确的值),但它有三个问题:Python 如何创建组合框QItemDelegate,python,qt,pyside,qcombobox,qitemdelegate,Python,Qt,Pyside,Qcombobox,Qitemdelegate,我实现了以下委托,以在QTableView中提供一个组合框。用例是用文本等价物替换通常对用户没有意义的列(键)(例如数字id) 下面的代码段可以工作(也可以保存正确的值),但它有三个问题: 它显示原始值,而不是等效文本 QTableView中的行选择提供了所有列,但不提供具有此委托的列 理想情况下,我希望组合框显示为这样,而用户不必单击它来发现它是一个 注意:键可以是任何字符串(不一定是整数)。一个典型的例子是国家(值“France”对应于键“FR”) 类组合委托(QtGui.QItemDele
类组合委托(QtGui.QItemDelegate):
"""
将QComboBox放置在每个
要应用它的列的单元格
"""
定义初始化(自、父、值数据):
值\数据是元组列表:(项,标签)
QtGui.QItemDelegate.\uuuuu init\uuuuuu(self,parent)
self.value\u数据=value\u数据
@财产
def项目(自身):
“要显示的项目列表”
返回[self.value\u数据中项目的项目[0]
@财产
def标签(自):
“要显示的标签列表”
返回[自我价值_数据中项目的项目[1]
def项目(自身、标签):
“从标签获取项目”
尝试:
index=self.labels.index(标签)
除值错误外:
通过
打印(“值编号和%s”%index)
返回self.items[索引]
def createEditor(自身、父项、选项、索引):
“创建编辑器(每次调用)”
combo=QtGui.QComboBox(父项)
对于在self.value\u数据中的duplet:
#小品是标签、物品
项目,标签=duplet
组合添加项(标签)
combo.currentIndexChanged.connect(self.currentIndexChanged)
返回组合
def setEditorData(自身、编辑器、索引):
编者:blockSignals(真)
editor.setCurrentIndex(index.row())
编者:blockSignals(假)
def setModelData(自身、编辑器、模型、索引):
“这是存储在字段中的数据”
打印(“当前文本:%s”%editor.currentText()
model.setData(索引,self.item(editor.currentText()))
def currentIndexChanged(自):
self.commitData.emit(self.sender())
第一个问题最容易由模型解决,即当要求显示角色
时,它可以提供特定值的文本,并且仍然通过编辑角色
提供数值
显示组合框有两个选项
paint()
方法,例如,通过像QComboBox
本身一样委托当前小部件样式qabstractemview::setIndexWidget()
添加
{your table view}.openpersisteneditor({your QModelIndex})
这是我的解决方案:
import sys
from PySide import QtGui, QtCore
class ComboBoxDelegate(QtGui.QItemDelegate):
def __init__(self, parent=None):
super(ComboBoxDelegate, self).__init__(parent)
self.items = []
def setItems(self, items):
self.items = items
def createEditor(self, widget, option, index):
editor = QtGui.QComboBox(widget)
editor.addItems(self.items)
return editor
def setEditorData(self, editor, index):
value = index.model().data(index, QtCore.Qt.EditRole)
if value:
editor.setCurrentIndex(int(value))
def setModelData(self, editor, model, index):
model.setData(index, editor.currentIndex(), QtCore.Qt.EditRole)
def updateEditorGeometry(self, editor, option, index):
editor.setGeometry(option.rect)
def paint(self, painter, option, index):
text = self.items[index.row()]
option.text = text
QtGui.QApplication.style().drawControl(QtGui.QStyle.CE_ItemViewItem, option, painter)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
column = 0
model = QtGui.QStandardItemModel(4, 2)
tableview = QtGui.QTableView()
tableview.setModel(model)
delegate = ComboBoxDelegate()
delegate.setItems([str(x) for x in range(10)])
tableview.setItemDelegateForColumn(column, delegate)
for row in range(4):
for col in range(2):
index = model.index(row, col, QtCore.QModelIndex())
value = (row + 1)*(col + 1)
model.setData(index, value)
for i in range(model.rowCount()):
tableview.openPersistentEditor(model.index(i, column))
tableview.show()
sys.exit(app.exec_())
字符串被放置,因为我在前面的示例中使用了str()
,所以我将向您展示另一个显示国家的示例
import sys
from PySide import QtGui, QtCore
class ComboBoxDelegate(QtGui.QItemDelegate):
def __init__(self, parent=None):
super(ComboBoxDelegate, self).__init__(parent)
self.items = []
def setItems(self, items):
self.items = items
def createEditor(self, widget, option, index):
editor = QtGui.QComboBox(widget)
editor.addItems(self.items)
return editor
def setEditorData(self, editor, index):
value = index.model().data(index, QtCore.Qt.EditRole)
if value:
editor.setCurrentIndex(int(value))
def setModelData(self, editor, model, index):
model.setData(index, editor.currentIndex(), QtCore.Qt.EditRole)
def updateEditorGeometry(self, editor, option, index):
editor.setGeometry(option.rect)
def paint(self, painter, option, index):
text = self.items[index.row()]
option.text = text
QtGui.QApplication.style().drawControl(QtGui.QStyle.CE_ItemViewItem, option, painter)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
column = 0
model = QtGui.QStandardItemModel(4, 2)
tableview = QtGui.QTableView()
tableview.setModel(model)
delegate = ComboBoxDelegate()
delegate.setItems([QtCore.QLocale.countryToString(QtCore.QLocale.Country(locale)) for locale in range(QtCore.QLocale.Afghanistan, QtCore.QLocale.Zulu+ 1 )])
tableview.setItemDelegateForColumn(column, delegate)
for row in range(4):
for col in range(2):
index = model.index(row, col, QtCore.QModelIndex())
value = (row + 1)*(col + 1)
model.setData(index, value)
for i in range(model.rowCount()):
tableview.openPersistentEditor(model.index(i, column))
tableview.show()
sys.exit(app.exec_())
非常感谢:这是一款清晰优雅的产品!我忘了提到键可以是任何字符串,而不仅仅是一个整数(通常是“FR”=>“France”,“IT”=>“意大利语”,等等)。再次感谢!在这种情况下,返回的值是一个整数索引,对吗?如果我希望返回的值是一个字符串(它代表意大利语,等等),对你来说,什么是最好的方法?我还看到你重新实现了UpdatedEditorGeometry和paint的方法。我觉得有点神秘,为什么需要这样做?我很好奇问题到底是什么,这些行动是如何解决的?