Disable QDoubleSpinBox validator
Hi.
I' have a delegate for a QTableWidget which is a QDoubleSpinBox. Its purpose is editing the table.
I want to disable the validator for QDoubleSpinBox because i'm conneting the valueChanged() signal from the QDoubleSpinBox to a function this way:
Code:
// connecting signal
connect(editor,SIGNAL(valueChanged(double)),this,SLOT(editing(double)));
Code:
void DoubleSpinDelegate::editing(double value)
{
emit commitData(spin);
}
I'm doing this for enabling a dynamic modification of the table. However, every time I commitData because the value is changed in the spinbox, it looks like a signal is sended to the spinbox for using the validator. For instance, I have in my table the value 1,45. I have a spinbox with 3 decimals so, when I try to edit the spinbox delegate opens with 1,450. Now if I edit any value after the comma the validator runs and just lets me edit 1 digit per time, so, if I wanna write 1,452 and I have 0,000 in the spinbox I have to write the 1, then highlight the first 0 and replace it by a 4, then I get 1,400... then, I have to highlight the second 0 and replace by 5 and so on...
I know that this is because the QDoubleValidator of the QLineEdit present in the QDoubleSpinBox... however I would like to disable it... If anyone knows how to do it I would appreciate it.
Just for having a context this is my class reimplementation of the delegate:
QDynSpinDelegate.h
Code:
#ifndef QDYNSPINDELEGATE_H
#define QDYNSPINDELEGATE_H
#include <QStyledItemDelegate>
#include <QDoubleSpinBox>
#include <QLineEdit>
class QDynSpinDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
private slots:
void editing(double value);
};
#endif // QDYNSPINDELEGATE_H
QDynSpinDelegate.cpp
Code:
#include "QDynSpinDelegate.h"
QDynSpinDelegate
::QDynSpinDelegate(QObject *parent
) : QStyledItemDelegate
(parent
){
}
{
if(index.column() == 0)
{
return editor;
}
else
{
editor->setAccelerated(true);
editor->setFrame(false);
connect(editor,SIGNAL(valueChanged(double)),this,SLOT(editing(double)));
if(index.column() >=1 && index.column() <=3)
{
editor->setMinimum(-1000);
editor->setMaximum(1000);
editor->setDecimals(3);
editor->setSuffix(" m");
editor->setSingleStep(0.05);
}
else
{
editor->setMinimum(-360);
editor->setMaximum(360);
editor->setDecimals(3);
editor->setSuffix(" °");
editor->setSingleStep(0.5);
}
return editor;
}
}
{
if(index.column() == 0)
{
QString value
= index.
model()->data
(index,Qt
::EditRole).
toString();
QLineEdit* line
= static_cast<QLineEdit
*>
(editor
);
line->setText(value);
}
else
{
double value = index.model()->data(index,Qt::EditRole).toDouble();
spin->setValue(value);
}
}
{
if(index.column() == 0)
{
QLineEdit* line
= static_cast<QLineEdit
*>
(editor
);
model->setData(index,value);
}
else
{
double value = spin->value();
model->setData(index,value);
}
}
{
editor->setGeometry(option.rect);
}
void QDynSpinDelegate::editing(double value)
{
emit commitData(spin);
}
Re: Disable QDoubleSpinBox validator
You can subclass QDoubleSpinBox, and then override the virtual function validate()
Code:
{
...
public:
{
}
...
}