PyQt4 QAbstractItemModel在两个小数位上切割浮点数

时间:2013-12-20 21:01:22

标签: python qt pyqt4

我有一个抽象项目模型。显示为树。 目的是存储一组首选项。 qt对象的代码是:

class QPreferenceModel(QAbstractItemModel):
    'Convention states only items with column index 0 can have children'
    @report_thread_error
    def __init__(self, pref_struct, parent=None):
        super(QPreferenceModel, self).__init__(parent)
        self.rootPref  = pref_struct

    @report_thread_error
    def index2Pref(self, index=QModelIndex()):
        '''Internal helper method'''
        if index.isValid():
            item = index.internalPointer()
            if item:
                return item
        return self.rootPref

    #-----------
    # Overloaded ItemModel Read Functions
    @report_thread_error
    def rowCount(self, parent=QModelIndex()):
        parentPref = self.index2Pref(parent)
        return parentPref.qt_row_count()

    @report_thread_error
    def columnCount(self, parent=QModelIndex()):
        parentPref = self.index2Pref(parent)
        return parentPref.qt_col_count()

    @report_thread_error
    def data(self, index, role=Qt.DisplayRule):
        '''Returns the data stored under the given role
        for the item referred to by the index.'''
        if not index.isValid():
            return QVariant()
        if role != Qt.DisplayRole and role != Qt.EditRole:
            return QVariant()
        nodePref = self.index2Pref(index)
        data = nodePref.qt_get_data(index.column())
        var = QVariant(data)
        print('---')
        print('data = %r' % data)
        print('type(data) = %r' % type(data))
        #if isinstance(data, float):
            #var = var.toDouble()[0]
        print('var= %r' % var)
        return var

    @report_thread_error
    def index(self, row, col, parent=QModelIndex()):
        '''Returns the index of the item in the model specified
        by the given row, column and parent index.'''
        if parent.isValid() and parent.column() != 0:
            return QModelIndex()
        parentPref = self.index2Pref(parent)
        childPref  = parentPref.qt_get_child(row)
        if childPref:
            return self.createIndex(row, col, childPref)
        else:
            return QModelIndex()

    @report_thread_error
    def parent(self, index=None):
        '''Returns the parent of the model item with the given index.
        If the item has no parent, an invalid QModelIndex is returned.'''
        if index is None:  # Overload with QObject.parent()
            return QObject.parent(self)
        if not index.isValid():
            return QModelIndex()
        nodePref = self.index2Pref(index)
        parentPref = nodePref.qt_get_parent()
        if parentPref == self.rootPref:
            return QModelIndex()
        return self.createIndex(parentPref.qt_parents_index_of_me(), 0, parentPref)

    #-----------
    # Overloaded ItemModel Write Functions
    @report_thread_error
    def flags(self, index):
        'Returns the item flags for the given index.'
        if index.column() == 0:
            # The First Column is just a label and unchangable
            return Qt.ItemIsEnabled | Qt.ItemIsSelectable
        if not index.isValid():
            return Qt.ItemFlag(0)
        childPref = self.index2Pref(index)
        if childPref:
            if childPref.qt_is_editable():
                return Qt.ItemIsEditable | Qt.ItemIsEnabled | Qt.ItemIsSelectable
        return Qt.ItemFlag(0)

    @report_thread_error
    def setData(self, index, data, role=Qt.EditRole):
        'Sets the role data for the item at index to value.'
        if role != Qt.EditRole:
            return False
        leafPref = self.index2Pref(index)
        result = leafPref.qt_set_leaf_data(data)
        if result is True:
            self.dataChanged.emit(index, index)
        return result

    @report_thread_error
    def headerData(self, section, orientation, role=Qt.DisplayRole):
        if orientation == Qt.Horizontal and role == Qt.DisplayRole:
            if section == 0:
                return QVariant('Config Key')
            if section == 1:
                return QVariant('Config Value')
        return QVariant()

我认为问题可能出在setData或data

                def data(self, index, role=Qt.DisplayRule):
                    '''Returns the data stored under the given role
                    for the item referred to by the index.'''
                    if not index.isValid():
                        return QVariant()
                    if role != Qt.DisplayRole and role != Qt.EditRole:
                        return QVariant()
                    nodePref = self.index2Pref(index)
                    data = nodePref.qt_get_data(index.column())
                    var = QVariant(data)
                    print('---')
                    print('data = %r' % data)
                    print('type(data) = %r' % type(data))
                    #if isinstance(data, float):
                        #var = var.toDouble()[0]
                    print('var= %r' % var)
                    return var

                def setData(self, index, data, role=Qt.EditRole):
                    'Sets the role data for the item at index to value.'
                    if role != Qt.EditRole:
                        return False
                    leafPref = self.index2Pref(index)
                    result = leafPref.qt_set_leaf_data(data)
                    if result is True:
                        self.dataChanged.emit(index, index)
                    return result

我正在搞乱数据,看看将QVariant转变为双倍会做些什么。我也尝试用Qt.UserRole替换Qt.DisplayRole和Qt.EditRole。

我实际上并不确定控制浮动的显示和编辑精度是什么。类型是正确的,但每当我想输入类似.0002的Qt时,Qt就会阻止我在.00

之后输入

1 个答案:

答案 0 :(得分:0)

您可以使用自定义item delegate来调整浮点数的精度,但对于此特定用例可能会过度。

更简单的解决方案是将值作为字符串返回 - 然后您可以以您喜欢的任何方式格式化显示的值。