Can debugger

Add hex spinbox
This commit is contained in:
2026-02-01 19:02:10 +01:00
parent a33d2fecaa
commit 5938645654
3 changed files with 82 additions and 0 deletions
+4
View File
@@ -21,6 +21,8 @@ find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets Svg WebSockets)
add_subdirectory(qwt)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
set(PROJECT_SOURCES
simulators/sim-cvs_i10.h
simulators/sim-cvs_i10.cpp
@@ -43,6 +45,8 @@ set(PROJECT_SOURCES
helpers.h
helpers.cpp
IterableEnum.hpp
hex_spinbox.h
hex_spinbox.cpp
better_radiobutton.h
better_radiobutton.cpp
led.h
+44
View File
@@ -0,0 +1,44 @@
#include "hex_spinbox.h"
HexSpinBox::HexSpinBox(QWidget *parent,
bool showPrefix,
const QString &format) :
QSpinBox(parent),
format(format)
{
// Validates hex strings up to 8 chars with or w/out leading "0x" prefix.
// For arbitrary prefix/suffix, the regex could be built dynamically
// in validate(), or override setPrefix()/setSuffix() methods.
const QRegularExpression rx("(?:0[xX])?[0-9A-Fa-f]{1,8}");
validator = new QRegularExpressionValidator(rx, this);
setShowPrefix(showPrefix);
}
void HexSpinBox::setShowPrefix(bool show)
{
if (show)
setPrefix(QStringLiteral("0x"));
else
setPrefix(QString());
}
void HexSpinBox::setFormat(const QString &text)
{
format = text;
this->lineEdit()->setText(textFromValue(value()));
}
QValidator::State HexSpinBox::validate(QString &text, int &pos) const
{
return validator->validate(text, pos);
}
int HexSpinBox::valueFromText(const QString &text) const
{
return text.toInt(0, 16);
}
QString HexSpinBox::textFromValue(int value) const
{
return QString().asprintf(qPrintable(format), value);
}
+34
View File
@@ -0,0 +1,34 @@
#ifndef _HEXSPINBOX_H_
#define _HEXSPINBOX_H_
#include <QSpinBox>
#include <QLineEdit>
#include <QRegularExpressionValidator>
// NOTE: Since QSpinBox uses int as the storage type, the effective editing range
// is +/- 0x7FFF FFFF, so it can't handle a full unsigned int.
// QDoubleSpinBox would be a more suitable base class if a wider range is needed.
class HexSpinBox : public QSpinBox
{
Q_OBJECT
public:
HexSpinBox(QWidget *parent = nullptr,
bool showPrefix = false,
const QString &format = QStringLiteral("%X"));
public slots:
void setShowPrefix(bool show);
void setFormat(const QString &text);
protected:
QValidator::State validate(QString &text, int &pos) const override;
int valueFromText(const QString &text) const override;
QString textFromValue(int value) const override;
private:
QRegularExpressionValidator *validator;
QString format;
};
#endif // _HEXSPINBOX_H_