0

升级到Windows10 Maya2020and后Pyside2,我发现它QAbstractItemView.edit现在正在转换intsunicodes.

在 Maya 2019 中,编辑包含 an 的字段int将加载 aQSpinBox并在编辑后保持正确的类型。

现在在 Maya2020 中,它似乎加载了 QLineEdit,允许字符串字符,因此,unicode 被传递给 QAbstractItemModel.setData

浮点数似乎仍能正确加载QDoubleSpinBox,但整数没有QSpinBox按应有的方式加载

我当然可以转换结果,但我仍然想找出这里出了什么问题。

*

请注意在编辑之前它是一个 int,而在它之后是一个 unicode

这是代码的简化版本:

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()

4

0 回答 0