QT 中的 64 位 int Spin Box

发布于 2024-12-20 01:32:36 字数 342 浏览 1 评论 0原文

我正在构建一个 Windows 程序,该程序应具有 64 位数值的控件。这些控件应可切换为签名或未签名。

我发现了两个控件: “旋转盒”(int32) 和“双旋转盒”(double) 使用 double 我可以覆盖范围,但它无法处理精度。

有没有办法改变这些控件的数据类型?

是否可以创建一个自己的控件来处理有符号和无符号 64 位值? 是否可以创建 128 位 Spin 盒子?

我现在能看到的唯一解决方法是使用字符串控件并手动转换为 INT64 或 UINT64,但我对此解决方案不太满意

还有其他想法吗?

我在 QT 4.7.4 和 VS2010 上使用 C++ 谢谢

I'm building a windows program which shall have controls for 64bit numeric values. these controls shall be switchable to be signed or unsigned.

I found two controls:
"Spin Box"(int32) and "Double Spin Box"(double)
with double I'd be able to cover the range but it can't handle the precision.

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

Is it possible to create an own control which can handle signed and unsigned 64bit values?
Is it possible to create a 128bit Spin box?

The only work around I can see right now is in using a string control and manually convert to an 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

执手闯天涯 2024-12-27 01:32:37

您可以派生 QAbstractSpinBox 并至少重新实现虚拟函数 stepBystepEnabled 以及可能的 validate()fixup() 用于输入验证。

You can derive QAbstractSpinBox and reimplement at least the virtual functions stepBy, stepEnabled and possibly validate() and fixup() for the input validation.

山人契 2024-12-27 01:32:37

我不使用修复功能。请参阅我的 Сustom QSpinBox 的代码。
类 QLongLongSpinBox 派生自 QAbstractSpinBox

不要忘记

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

在创建 QLongLongSpinBox 后调用。

参见qlonglongspinbox.h文件:

#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)
};

I don't use fixup function. See code of my Сustom QSpinBox.
class QLongLongSpinBox derived from QAbstractSpinBox

Don't forget call

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

after creating 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)
};
素罗衫 2024-12-27 01:32:37

要在 Qt Creator 中使用自定义类(小部件):

  • 创建 QWidget 小部件
  • 列表项,
  • 在小部件菜单中选择升级到...
    新晋级班级:
    QWigdet
    提升的类名称 QLongLongSpinBox
    头文件:编写qlonglongspinbox.h
    升级
  • 选择升级到 QLongLongSpinBox
  • 保存

您可以看到

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

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

在 *.ui 文件中

,在 ui_*.h 生成文件中您可以看到您的类:

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

To use you custom class (Widget) it in Qt Creator:

  • create QWidget widget
  • List item
  • select Promote to.. in widget menu
    New Promoted Class:
    QWigdet
    Promoted class name QLongLongSpinBox
    Header file: write qlonglongspinbox.h
    Promote
  • select Promote 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 *.ui file

in ui_*.h generating file you see you class:

#include "qlonglongspinbox.h"
QLongLongSpinBox *object_name;
value_integer = new QLongLongSpinBox(YourWidgetName);
value_integer->setObjectName(QStringLiteral("value_integer"));
verticalLayout->addWidget(value_integer);
掌心的温暖 2024-12-27 01:32:37

只是为了完整起见:每次遇到这样的问题时,请记住(以防万一其他一切都失败)您可以下载 Qt 源代码并制作自己的 QSpinBox64通过修改所需的部分,只需几分钟即可完成 QSpinBox128 类。

Just for the sake of completeness: every time you run into a problem like this, remember that (in case everything else fails) you can just download the Qt source code and make your own QSpinBox64 or QSpinBox128 class in a matter of minutes by modifying the needed parts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文