64-bit int Spin Box in QT

I am creating a Windows program that needs to have controls for 64-bit numeric values. these controls must be switched to sign or unsigned.

I found two controls: “Spin Box” (int32) and “Double Spin Box” (double) with a double I could cover the range, but it could not cope with accuracy.

Is there a way to change the data type of these controls?

Is it possible to create your own control that can process signed and unsigned 64-bit values? Is it possible to create a 128-bit Spin box?

The only work that I see now is to use string control and convert manually to INT64 or UINT64, but I'm not very happy with this solution.

Any other ideas?

I'm on QT 4.7.4 and VS2010 with C ++ THX

+4
source share
3 answers

You can get a QAbstractSpinBox and override at least the virtual functions stepBy , stepEnabled and possibly validate() and fixup() to validate the input.

+5
source

I do not use the fixup function. See my Qustom QSpinBox Code. QLongLongSpinBox class derived from QAbstractSpinBox

Do not forget to call

 setMaximum(std::numeric_limits<qlonglong>::max()); setMinimum(std::numeric_limits<qlonglong>::min()); 

after creating the QLongLongSpinBox.

see qlonglongspinbox.h file:

 #include <QtWidgets/QWidget> #include <QtWidgets/QAbstractSpinBox> #include <QtWidgets/QLineEdit> class QLongLongSpinBoxPrivate; class Q_WIDGETS_EXPORT QLongLongSpinBox : public QAbstractSpinBox { Q_OBJECT Q_PROPERTY(qlonglong minimum READ minimum WRITE setMinimum) Q_PROPERTY(qlonglong maximum READ maximum WRITE setMaximum) Q_PROPERTY(qlonglong value READ value WRITE setValue NOTIFY valueChanged USER true) qlonglong m_minimum; qlonglong m_maximum; qlonglong m_value; public: explicit QLongLongSpinBox(QWidget *parent = 0) { connect(lineEdit(), SIGNAL(textEdited(QString)), this, SLOT(onEditFinished())); }; ~QLongLongSpinBox() {}; qlonglong value() const { return m_value; }; qlonglong minimum() const { return m_minimum; }; void setMinimum(qlonglong min) { m_minimum = min; } qlonglong maximum() const { return m_maximum; }; void setMaximum(qlonglong max) { m_maximum = max; } void setRange(qlonglong min, qlonglong max) { setMinimum(min); setMaximum(max); } virtual void stepBy(int steps) { auto new_value = m_value; if (steps < 0 && new_value + steps > new_value) { new_value = std::numeric_limits<qlonglong>::min(); } else if (steps > 0 && new_value + steps < new_value) { new_value = std::numeric_limits<qlonglong>::max(); } else { new_value += steps; } lineEdit()->setText(textFromValue(new_value)); setValue(new_value); } protected: //bool event(QEvent *event); virtual QValidator::State validate(QString &input, int &pos) const { bool ok; qlonglong val = input.toLongLong(&ok); if (!ok) return QValidator::Invalid; if (val < m_minimum || val > m_maximum) return QValidator::Invalid; return QValidator::Acceptable; } virtual qlonglong valueFromText(const QString &text) const { return text.toLongLong(); } virtual QString textFromValue(qlonglong val) const { return QString::number(val); } //virtual void fixup(QString &str) const; virtual QAbstractSpinBox::StepEnabled stepEnabled() const { return StepUpEnabled | StepDownEnabled; } public Q_SLOTS: void setValue(qlonglong val) { if (m_value != val) { lineEdit()->setText(textFromValue(val)); m_value = val; } } void onEditFinished() { QString input = lineEdit()->text(); int pos = 0; if (QValidator::Acceptable == validate(input, pos)) setValue(valueFromText(input)); else lineEdit()->setText(textFromValue(m_value)); } Q_SIGNALS: void valueChanged(qlonglong v); private: Q_DISABLE_COPY(QLongLongSpinBox) Q_DECLARE_PRIVATE(QLongLongSpinBox) }; 
+2
source

To use a custom class (Widget) in Qt Creator:

  • create a QWidget widget
  • List item
  • select "Help" .. in the widget menu New promoted class: QWigdet
    QLongLongSpinBox advanced class name
    Header File: write qlonglongspinbox.h
    Promotion
  • select "Go to QLongLongSpinBox"
  • save

You can see

 <item> <widget class="QLongLongSpinBox" name="value_integer" native="true"/> </item> 

and

 <customwidgets> <customwidget> <class>QLongLongSpinBox</class> <extends>QWidget</extends> <header>qlonglongspinbox.h</header> <container>1</container> </customwidget> </customwidgets> 

in the * .ui file

in the ui _ * file. h you see the class:

 #include "qlonglongspinbox.h" QLongLongSpinBox *object_name; value_integer = new QLongLongSpinBox(YourWidgetName); value_integer->setObjectName(QStringLiteral("value_integer")); verticalLayout->addWidget(value_integer); 
0
source

Source: https://habr.com/ru/post/1384669/


All Articles