【问题标题】:How to always show sign (+ or -) in QDoubleSpinbox?如何始终在 QDoubleSpinbox 中显示符号(+ 或 -)?
【发布时间】:2018-11-01 17:51:01
【问题描述】:

如果 QDoubleSpinbox 中的值为正,则表示无符号。

如果数值改成负数,会自动加上“-”号。

如果前缀强制为“+”,那么正数会显示带符号

doubleSB->setPrefix("+");

但是“+”会一直留在那里,当设置的值为负数时不会自动删除

有没有办法始终显示正确的标志?

  • 如果值为正,则为“+”号
  • 如果值为负数,则使用“-”符号(默认情况下)

【问题讨论】:

    标签: c++ qt qt5 qdoublespinbox


    【解决方案1】:

    一种可能的解决方案是覆盖textFromValue() 方法并在必要时添加该字符:

    #include <QApplication>
    #include <QDoubleSpinBox>
    
    class DoubleSpinBox: public QDoubleSpinBox
    {
    public:
        using QDoubleSpinBox::QDoubleSpinBox;
        QString textFromValue(double value) const override
        {
            QString text = QDoubleSpinBox::textFromValue(value);
            if(value > 0)
                text.prepend(QChar('+'));
            return text;
        }
    };
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        DoubleSpinBox w;
        w.setMinimum(-100);
        w.setSuffix("%");
        w.show();
    
        return a.exec();
    }
    

    【讨论】:

      猜你喜欢
      • 2013-03-24
      • 2012-04-28
      • 2020-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-19
      • 1970-01-01
      相关资源
      最近更新 更多