gpt4 book ai didi

QTableView : change precision for double values

转载 作者:行者123 更新时间:2023-12-04 12:20:00 24 4
gpt4 key购买 nike

如果模型以 EditRole 形式返回 double 值,那么(据说)QDoubleSpinBox 被 QTableView 用作编辑器。如何更改该控件的精度?

最佳答案

解释了 QTableView 中 QDoubleSpinBox 的精度行为 here ,所以要解决这个问题,需要自己设置QDoubleSpinBox,根据Subclassing QStyledItemDelegate的结尾部分有两种方法可以做到。 :
使用编辑器项目工厂或子类化 QStyledItemDelegate。后一种方式需要你重新实现 QStyledItemDelegate 的四种方法,我觉得它有点冗长,所以我选择了第一种方式,PyQt 中的示例代码如下:

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class ItemEditorFactory(QItemEditorFactory): # http://doc.qt.io/qt-5/qstyleditemdelegate.html#subclassing-qstyleditemdelegate It is possible for a custom delegate to provide editors without the use of an editor item factory. In this case, the following virtual functions must be reimplemented:
def __init__(self):
super().__init__()

def createEditor(self, userType, parent):
if userType == QVariant.Double:
doubleSpinBox = QDoubleSpinBox(parent)
doubleSpinBox.setDecimals(3)
doubleSpinBox.setMaximum(1000) # The default maximum value is 99.99.所以要设置一下
return doubleSpinBox
else:
return super().createEditor(userType, parent)




styledItemDelegate=QStyledItemDelegate()
styledItemDelegate.setItemEditorFactory(ItemEditorFactory())
self.tableView.setItemDelegate(styledItemDelegate)
self.tableView.setModel(self.sqlTableModel)

关于QTableView : change precision for double values,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29930407/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com