Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Qt5 custom QDoubleSpinBox
#include "numbereditor.hxx"
namespace Schematics::Ui::Widgets
{
NumberEditor::NumberEditor(QWidget *parent)
: QDoubleSpinBox(parent)
{
setSuffix(" mm");
setDecimals(1);
setAlignment(Qt::AlignRight);
setMinimum(0.0);
setMaximum(1000.0);
}
QValidator::State NumberEditor::validate(QString &input, int &pos) const {
auto point = locale().decimalPoint();
auto dot = (point == ',') ? '.' : ',';
// assume dot same comma
input.replace(dot, point);
return QDoubleSpinBox::validate(input, pos);
}
NumberEditor::~NumberEditor() = default;
} // namespace Schematics::Ui::Widgets
#ifndef SCHEMATICS_NUMBEREDITOR_HXX
#define SCHEMATICS_NUMBEREDITOR_HXX
#include <QDoubleSpinBox>
namespace Schematics::Ui::Widgets
{
class NumberEditor: public QDoubleSpinBox
{
public:
explicit NumberEditor(QWidget *parent = nullptr);
~NumberEditor() override;
QValidator::State validate(QString &input, int &pos) const override;
};
} // namespace Schematics::Ui::Widgets
#endif //SCHEMATICS_NUMBEREDITOR_HXX
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment