PDA

View Full Version : Disable QDoubleSpinBox validator



daleotar
20th June 2011, 19:44
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:


// connecting signal
connect(editor,SIGNAL(valueChanged(double)),this,S LOT(editing(double)));



void DoubleSpinDelegate::editing(double value)
{
QDoubleSpinBox *spin = static_cast<QDoubleSpinBox*>(sender());
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


#ifndef QDYNSPINDELEGATE_H
#define QDYNSPINDELEGATE_H

#include <QStyledItemDelegate>
#include <QDoubleSpinBox>
#include <QLineEdit>

class QDynSpinDelegate : public QStyledItemDelegate
{
Q_OBJECT

public:
QDynSpinDelegate(QObject* parent);
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;
private slots:
void editing(double value);
};

#endif // QDYNSPINDELEGATE_H


QDynSpinDelegate.cpp


#include "QDynSpinDelegate.h"

QDynSpinDelegate::QDynSpinDelegate(QObject *parent) : QStyledItemDelegate(parent)
{
}

QWidget* QDynSpinDelegate::createEditor(QWidget* parent,const QStyleOptionViewItem &option,const QModelIndex &index) const
{

if(index.column() == 0)
{
QLineEdit* editor = new QLineEdit(parent);
return editor;
}
else
{
QDoubleSpinBox *editor = new QDoubleSpinBox(parent);
editor->setAccelerated(true);
editor->setFrame(false);
connect(editor,SIGNAL(valueChanged(double)),this,S LOT(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;
}

}
void QDynSpinDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
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();
QDoubleSpinBox *spin = static_cast<QDoubleSpinBox*>(editor);
spin->setValue(value);
}
}
void QDynSpinDelegate::setModelData(QWidget* editor,QAbstractItemModel* model,const QModelIndex &index) const
{
if(index.column() == 0)
{
QLineEdit* line = static_cast<QLineEdit*>(editor);
QString value = line->text();
model->setData(index,value);
}
else
{
QDoubleSpinBox* spin = static_cast<QDoubleSpinBox*>(editor);
double value = spin->value();
model->setData(index,value);
}

}
void QDynSpinDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}

void QDynSpinDelegate::editing(double value)
{
QDoubleSpinBox *spin = static_cast<QDoubleSpinBox*>(sender());
emit commitData(spin);
}

Santosh Reddy
20th June 2011, 21:29
You can subclass QDoubleSpinBox, and then override the virtual function validate()



class MyDoubleSpinBox : public QDoubleSpinBox
{
...
public:
virtual QValidator::State validate ( QString & text, int & pos ) const
{
return QValidator::Acceptable;
}
...
}