1 回答
TA贡献1898条经验 获得超8个赞
解释:
发生的事情是,PySide2将整数从python转换为LongLong(QVariant::LongLong=4),C++默认情况下,默认QItemEditorFactory不会处理,因此使用了(在PyQt中转换为QMetaType::Int=2)。QLineEdit
溶液:
一种可能的解决方案是创建一个返回相应小部件的自定义 QItemEditorFactory:
from PySide2 import QtCore, QtGui, QtWidgets
class ItemEditorFactory(QtWidgets.QItemEditorFactory):
def createEditor(self, userType, parent):
if userType == 4:
return QtWidgets.QSpinBox(parent, minimum=-2147483648, maximum=2147483647)
return super().createEditor(userType, parent)
if __name__ == "__main__":
app = QtWidgets.QApplication([])
factory = ItemEditorFactory()
QtWidgets.QItemEditorFactory.setDefaultFactory(factory)
w = QtWidgets.QTableView()
resourcesModel = QtGui.QStandardItemModel(2, 2)
w.setModel(resourcesModel)
item = QtGui.QStandardItem()
item.setData(123123, QtCore.Qt.EditRole)
resourcesModel.setItem(1, 1, item)
w.resize(640, 480)
w.show()
app.exec_()
添加回答
举报