• 数据库操作QTableView保存小数点的位数


    1.在做项目中遇到,QTableView要求可编辑到小数点后七位,使用自定义委托,重写QStyledItemDelegate类下面的

        // editing
        QWidget *createEditor(QWidget *parent,
                              const QStyleOptionViewItem &option,
                              const QModelIndex &index) const Q_DECL_OVERRIDE;
    
        void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE;
        void setModelData(QWidget *editor,
                          QAbstractItemModel *model,
                          const QModelIndex &index) const Q_DECL_OVERRIDE;
    
        void updateEditorGeometry(QWidget *editor,
                                  const QStyleOptionViewItem &option,
                                  const QModelIndex &index) const Q_DECL_OVERRIDE;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    2.写个小demo
    .h头文件

    //小数点后七位,利用QSpinBox委托进行输入限制
    class DoubleSpinBoxDelegate : public QStyledItemDelegate
    {
    	Q_OBJECT
    public:
    	DoubleSpinBoxDelegate(QObject *parent = 0);
    	QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
    		const QModelIndex &index) const;
    	void setEditorData(QWidget *editor, const QModelIndex &index) const;
    	void setModelData(QWidget *editor, QAbstractItemModel *model,
    		const QModelIndex &index) const;
    	void updateEditorGeometry(QWidget *editor,
    		const QStyleOptionViewItem &option, const QModelIndex &index) const;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    .cpp文件

    DoubleSpinBoxDelegate::DoubleSpinBoxDelegate(QObject *parent)
    	: QStyledItemDelegate(parent)
    {
    }
    QWidget* DoubleSpinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
    	const QModelIndex &index) const
    {
    	if (QVariant::Double == index.model()->data(index, Qt::EditRole).type())
    	{
    		QDoubleSpinBox *pEditor = new QDoubleSpinBox(parent);
    		pEditor->setFrame(false);
    		pEditor->setMinimum(-180.0000000);
    		pEditor->setMaximum(180.0000000);
    		pEditor->setDecimals(7);
    		return pEditor;
    	}
    	else
    	{
    		return QStyledItemDelegate::createEditor(parent, option, index);
    	}
    }
    void DoubleSpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    {
    	if (QVariant::Double == index.model()->data(index, Qt::EditRole).type())
    	{
    		double value = index.model()->data(index, Qt::EditRole).toDouble();
    		QDoubleSpinBox *pSpinBox = static_cast<QDoubleSpinBox*>(editor);
    		pSpinBox->setValue(value);
    	}
    	else
    	{
    		 QStyledItemDelegate::setEditorData(editor, index);
    	}
    }
    void DoubleSpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
    	const QModelIndex &index) const
    {
    	if (QVariant::Double == index.model()->data(index, Qt::EditRole).type())
    	{
    		QDoubleSpinBox *pSpinBox = static_cast<QDoubleSpinBox*>(editor);
    		pSpinBox->interpretText();
    		double value = pSpinBox->value();
    		model->setData(index, value, Qt::EditRole);
    	}
    	else
    	{
    		QStyledItemDelegate::setModelData(editor, model,index);
    	}
    }
    void DoubleSpinBoxDelegate::updateEditorGeometry(QWidget *editor,
    	const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
    	editor->setGeometry(option.rect);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    使用方式

    ui.tableView->setItemDelegateForColumn(1, new DoubleSpinBoxDelegate(this));//第一列使用代理
    
    • 1
  • 相关阅读:
    关于MySQL的30条优化技巧,超实用
    网络安全攻防演练项目介绍
    企业数字化转型,为什么需要企业门户?
    Windows命令行查找并kill进程及常用批处理命令汇总
    C++11 ——— 类的新功能
    二分套二分+贪心:CSPS2023T4
    关于Oracle数据库审计、诊断文件及跟踪文件操作
    Linux系统启动和内核管理及系统救援
    如何在WPF中设置Grid ColumnDefinitions的样式
    Markdowm使用手册
  • 原文地址:https://blog.csdn.net/qq_45893999/article/details/126275985