In need of a 64-bit unsigned QSpinBox (to fit large numbers like pointers, etc)
-
Hmm, that's one way of doing it, but minimum/maximum values are one of the main reasons I chose a QSpinBox originally, as in the case of pointers they should preferably be constrained to a specific region in address space.
Step buttons aren't so necessary, but it's the scroll wheel behaviour that comes with it that increments/decrements the value which would save a bit of time and make usage a bit better.. -
@boohbah
If you go @Bonnie's route you could add aQDoubleValidator
or derive fromQValidator
for the range.If you look at https://codebrowser.dev/qt5/qtbase/src/widgets/widgets/qspinbox.cpp.html you can see the implementation of
QSpinBox
/QDoubleSpinBox
and derive fromQAbstractSpinBox
for 64-bit. Most of the code there is either comments orQDoubleSpinBox
so it's not so much.The solution at How to subclass QSpinBox so it could have int64 values as maxium and minimum suggests following that approach and that it's not too much work.
-
The steps from the linked post do not seem to give a working spin box (as in, it doesn't even show a value). It seems I might have to reimplement a whole bunch of methods, like
valueFromText
,textFromValue
,fixup
,validate
, the value/textChanged signals, and possibly even more. -
@boohbah
That is true if you re-implement fromQAbstractSpinBox
. But to just make 64-bit if the outline solution at my referenced https://stackoverflow.com/a/15655190/489865 solution is sufficient (maybe with the addition for you of hex values) that does not involve private stuff. -
I don't understand. Doing what that post sent and the information it provides doesn't seem sufficient and I'm not sure how I'd approach the other two steps it mentioned.
This is what I currently have, and it does practically nothing:
class SpinBoxU64 : public QAbstractSpinBox { public: explicit SpinBoxU64(QWidget* parent = nullptr); uint64_t minimum() const; void setMinimum(uint64_t min); uint64_t maximum() const; void setMaximum(uint64_t max); void setRange(uint64_t min, uint64_t max); uint64_t value() const; void stepBy(int steps) override; public slots: void setValue(uint64_t val); signals: void valueChanged(uint64_t); private: uint64_t m_minimum, m_maximum, m_value; }; SpinBoxU64::SpinBoxU64(QWidget* parent) : QAbstractSpinBox(parent), m_minimum(0), m_maximum(100), m_value(0) {} uint64_t SpinBoxU64::minimum() const { return m_minimum; } uint64_t SpinBoxU64::maximum() const { return m_maximum; } uint64_t SpinBoxU64::value() const { return m_value; } void SpinBoxU64::setMinimum(uint64_t min) { m_minimum = min; } void SpinBoxU64::setMaximum(uint64_t max) { m_maximum = max; } void SpinBoxU64::setValue(uint64_t value) { m_value = value; } void SpinBoxU64::stepBy(int steps) { m_value += steps; }
I implemented some of the methods and properties that I need, such as minimum and maximum, I also implemented stepBy (which doesn't even seem to be called by anything at all when I put a
qDebug
call inside it), and derived from QAbstractSpinBox itself. Interacting with this spin box just does nothing.Where would I put the relevant
lineEdit()->setText()
call? Why a validator on the lineEdit itself whenQAbstractSpinBox
has its own virtualvalidate
andfixup
methods themselves, implying those should be reimplemented? What would be a good way to approach such a validator? Things are rather ambiguous and confusing at this point. -
@boohbah
A little extra Googling might have led you to 64bit int Spin Box in QT. The post there https://stackoverflow.com/a/32628421/489865, the See code of my Сustom QSpinBox. class QLongLongSpinBox derived from QAbstractSpinBox one, is 10 years old but looks like a complete implementation to me. Not that big. It's for aqlonglong
/qint64
. You can change it toqulonglong
/quint64
/uint64_t
. When you have it working add your hex/integer base code.Read through qspinbox64 in QT5.15.2 for updates to code/comments/other peoples' changes. The last post there may have an alternative implementation.
-
you may can refer to this code: https://github.com/AlekseyDurachenko/QtLongLongSpinBox,