Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
QTableView:更改双值的精度_Qt - Fatal编程技术网

QTableView:更改双值的精度

QTableView:更改双值的精度,qt,Qt,如果模型返回一个双精度值作为EditRole,那么(假定)QDoubleSpinBox将被QTableView用作编辑器。如何更改该控件中的精度?根据,可以通过调用小数来更改QDoubleSpinBox的精度。我一直无法找到一个好方法来获得这些Spinbox。QTableView的默认委托是。在Qt::EditRole中创建项时,它使用默认类创建的项,您可以使用QItemEditorFactory::defaultFactory()访问该类。你可以在那里注册你自己的编辑器,但是我看不到一个好的方

如果模型返回一个双精度值作为EditRole,那么(假定)QDoubleSpinBox将被QTableView用作编辑器。如何更改该控件中的精度?

根据,可以通过调用
小数来更改
QDoubleSpinBox
的精度。

我一直无法找到一个好方法来获得这些Spinbox。
QTableView
的默认委托是。在
Qt::EditRole
中创建项时,它使用默认类创建的项,您可以使用
QItemEditorFactory::defaultFactory()
访问该类。你可以在那里注册你自己的编辑器,但是我看不到一个好的方法来编辑那些已经存在的编辑器


相反,您最应该做的是以指定的不同精度实现自己的委托。有一种方法可以使用
QSpinBox
生成委托,您可以将其替换为
QDoubleSpinBox
。然后在
createEditor
中,使用
setDecimals
将旋转框设置为所需的精度。然后使用
setItemDelegate

将该委托应用到表中。下面是一个最小的
QStyledItemDelegate
实现,它修改了
QDoubleSpinBox
精度:

const int DOUBLESP_PRECISION = 6;

class SpinBoxDelegate : public QStyledItemDelegate
{
public:
    QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                          const QModelIndex &index) const Q_DECL_OVERRIDE
        {
            auto w = QStyledItemDelegate::createEditor(
                parent, option, index);

            auto sp = qobject_cast<QDoubleSpinBox*>(w);
            if (sp)
            {
                sp->setDecimals(DOUBLESP_PRECISION);
            }
            return w;
        }
};
const int DOUBLESP_精度=6;
类SpinBoxDelegate:公共QStyledItemDelegate
{
公众:
QWidget*createEditor(QWidget*父项,常量QSTYLEOPTION视图项和选项,
常数QModelIndex和索引)常数Q_DECL_覆盖
{
自动w=QStyledItemDelegate::createEditor(
父项、选项、索引);
自动sp=QoObject_cast(w);
如果(sp)
{
sp->setDecimals(双sp_精度);
}
返回w;
}
};

解释了QDoubleSpinBox在QTableView中的精度行为,因此要解决此问题,您需要设置自己的QDoubleSpinBox,根据QDoubleSpinBox的结尾部分,有两种方法: 使用编辑器项工厂或子类化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)

我认为您需要解释如何访问双旋转框编辑器来更改精度。子类化QStyledItemDelegate的方式要求您根据@iMath的结尾部分重新实现QStyledItemDelegate的四个方法。没有必要实现所有方法。@iMath上面的代码经过测试,并按预期工作。其他方法已经由
QStyledItemDelegate
实现。我只是不需要改变他们的行为。至于使用编辑器项工厂来解决问题(我觉得有点简单!),请看我的答案