升级到Windows10
Maya2020
and后Pyside2
,我发现它QAbstractItemView.edit
现在正在转换ints
为unicodes
.
在 Maya 2019 中,编辑包含 an 的字段int
将加载 aQSpinBox
并在编辑后保持正确的类型。
现在在 Maya2020 中,它似乎加载了 QLineEdit,允许字符串字符,因此,unicode 被传递给 QAbstractItemModel.setData
浮点数似乎仍能正确加载QDoubleSpinBox
,但整数没有QSpinBox
按应有的方式加载
我当然可以转换结果,但我仍然想找出这里出了什么问题。
*
这是代码的简化版本:
import sys
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
class BlueprintDataView(QTableView):
def mouseDoubleClickEvent(self, event):
super(BlueprintDataView, self).mouseDoubleClickEvent(event)
self.edit(self.indexAt(event.pos()))
class BlueprintDataModel(QAbstractTableModel):
def __init__(self, items, *args, **kwargs):
super(BlueprintDataModel, self).__init__(*args, **kwargs)
self.items = items
def columnCount(self, index):
return 2
def rowCount(self, index):
return len(self.items)
def setData(self, index, value, role):
if role == Qt.EditRole or role == Qt.DisplayRole:
row = index.row()
key, old_value = self.items[row]
print('Old Type = %s New Type = %s' % (type(old_value), type(value)))
self.items[row][1] = value
return True
return False
def data(self, index, role):
column = index.column()
key, value = self.get_item(index)
if column == 0:
if role == Qt.DisplayRole:
return key
if column == 1:
if role == Qt.EditRole:
return value
if role == Qt.DisplayRole:
return value
def get_item(self, index):
return self.items[index.row()]
def flags(self, index):
if not index.isValid():
return Qt.ItemIsEnabled
column = index.column()
if column == 0:
return Qt.ItemIsEnabled | Qt.ItemIsSelectable
elif column == 1:
return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable
view = BlueprintDataView()
view.setModel(BlueprintDataModel([['One', 1], ['Two', 2], ['Three', 3]]))
view.show()