'C++ - Qt Creator - /How to have DOT and COMMA as decimal separator on a QDoubleSpinBox?
I am building a C++ GUI application on QT Creator. I changed the location to Portuguese/Brazil, now only comma is the decimal separator.
I need the QDoubleSpinBox to get as decimal separator dot and comma. Officialy comma is the separator in Portuguese, but some keyboard only have points in the numeric part.
Please help,
Solution 1:[1]
subClass QDoubleSpinBox and reimplement the virtual method validate
full solution here :
customSpinBox.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QRegExpValidator>
#include <QDoubleSpinBox>
class CustomSpinBox : public QDoubleSpinBox {
Q_OBJECT
public:
explicit CustomSpinBox(QWidget* parent =0);
virtual QValidator::State validate(QString & text, int & pos) const;
private:
QRegExpValidator* validator;
};
#endif // WIDGET_H
customSpinBox.cpp
CustomSpinBox::CustomSpinBox(QWidget *parent):QDoubleSpinBox(parent),
validator(new QRegExpValidator(this))
{
validator->setRegExp(QRegExp("\\d{1,}(?:[,.]{1})\\d*"));
}
QValidator::State CustomSpinBox::validate(QString &text, int &pos) const
{
return validator->validate(text,pos);
}
Solution 2:[2]
I tried to converted the solution from basslo to Qt 6.0 but it failed to accept integer numbers. The following solution works fine for me with Qt 6.0
#pragma once
#include <qtWidgets>
class CRelaxedDoubleSpinBox : public QDoubleSpinBox {
Q_OBJECT
public:
explicit CRelaxedDoubleSpinBox(QWidget* parent =0) : QDoubleSpinBox(parent)
{
}
virtual QValidator::State validate(QString & text, int & pos) const
{
QString s = QString(text).replace(".", ",");
return QDoubleSpinBox::validate(s,pos);
}
double valueFromText(const QString& text) const
{
QString s = QString(text).replace(",", ".");
return s.toDouble();
}
};
Solution 3:[3]
You can subclass QDoubleSpinBox
and reimplement the validate
method to accept both the dot and the comma as the decimal separator. I think you can get by with just adding a special check when the input is a period and allow it to be accepted (provided there are no other periods or commas in the string), but otherwise call the base class implementation. I haven't compiled or tested this, but I think this is pretty close:
MyDoubleSpinBox::validate (QString &input, int &pos)
{
if (input == ".")
{
return (text ().contains (".") || text ().contains (",")) ? QValidator::Invalid : QValidator::Acceptable;
}
return QDoubleSpinBox::validate (input, pos);
}
Solution 4:[4]
I'm using PySide6 and I adapted to the Python language the wonderful solution provided by @RED SOFT ADAIR to Python:
from PySide6.QtWidgets import QDoubleSpinBox,
class CustomDoubleSpinbox(QDoubleSpinBox):
def validate(self, text: str, pos: int) -> object:
text = text.replace(".", ",")
return QDoubleSpinBox.validate(self, text, pos)
def valueFromText(self, text: str) -> float:
text = text.replace(",", ".")
return float(text)
Here it is in case anyone ended up here in a similar situation.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | basslo |
Solution 2 | RED SOFT ADAIR |
Solution 3 | goug |
Solution 4 | Kastakin |